0

Hi this question may seem like a duplicate, but it really isn't since I have already tried the suggestions on other stackoverflow questions like this. Could someone help me debug my code, here is my code:

import java.awt.*;

import javax.swing.*;
import javax.swing.border.MatteBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;

// This is the Panel that contains represents the view of the
// Music Store

public class PosPanel extends JPanel {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    // These are the components
    private JLabel      billNumber;
    private JButton     submitButton;
    private JButton     changeViewButton;
    private JButton     addItemButton;
    private JTable      table;



    public JButton getsubmitButton() {
        return submitButton;
    }

    public JLabel getBillNumber() {
        return billNumber;
    }
    public JButton getAddItemButton() {
        return addItemButton;
    }

    public JButton getChangeViewButton() {
        return changeViewButton;
    }
    public void setChangeViewButton(JButton changeViewButton) {
        this.changeViewButton = changeViewButton;
    }
    public JTable getTable() {
        return table;
    }

    public PosPanel(){
        super();

        // Use a GridBagLayout (lotsa fun)
        GridBagLayout layout = new GridBagLayout();
        GridBagConstraints layoutConstraints = new GridBagConstraints();
        setLayout(layout);

        billNumber = new JLabel("Invoice #1");
        layoutConstraints.gridx = 0;
        layoutConstraints.gridy = 0;
        layoutConstraints.gridwidth = 1;
        layoutConstraints.gridheight = 1;
        layoutConstraints.fill = GridBagConstraints.BOTH;
        layoutConstraints.insets = new Insets(10, 10, 10, 10);
        layoutConstraints.anchor = GridBagConstraints.EAST;
        layoutConstraints.weightx = 1.0;
        layoutConstraints.weighty = 0.05;
        layout.setConstraints(billNumber, layoutConstraints);
        add(billNumber);

        changeViewButton = new JButton("Exchange/Return");
        layoutConstraints.gridx = 1;
        layoutConstraints.gridy = 0;
        layoutConstraints.gridwidth = 1;
        layoutConstraints.gridheight = 1;
        layoutConstraints.fill = GridBagConstraints.BOTH;
        layoutConstraints.insets = new Insets(0, 0, 0, 0);
        layoutConstraints.anchor = GridBagConstraints.EAST;
        layoutConstraints.weightx = 1.0;
        layoutConstraints.weighty = 0.05;
        layout.setConstraints(changeViewButton, layoutConstraints);
        add(changeViewButton);

        addItemButton = new JButton("Add Item");
        layoutConstraints.gridx = 2;
        layoutConstraints.gridy = 0;
        layoutConstraints.gridwidth = 1;
        layoutConstraints.gridheight = 1;
        layoutConstraints.fill = GridBagConstraints.BOTH;
        layoutConstraints.insets = new Insets(0, 0, 0, 0);
        layoutConstraints.anchor = GridBagConstraints.EAST;
        layoutConstraints.weightx = 1.0;
        layoutConstraints.weighty = 0.05;
        layout.setConstraints(addItemButton, layoutConstraints);
        add(addItemButton);

        submitButton = new JButton("Submit");
        layoutConstraints.gridx = 0;
        layoutConstraints.gridy = 8;
        layoutConstraints.gridwidth = 3;
        layoutConstraints.gridheight = 1;
        layoutConstraints.fill = GridBagConstraints.BOTH;
        layoutConstraints.insets = new Insets(0, 0, 0, 0);
        layoutConstraints.anchor = GridBagConstraints.EAST;
        layoutConstraints.weightx = 1.0;
        layoutConstraints.weighty = 0.05;
        layout.setConstraints(submitButton, layoutConstraints);
        add(submitButton);



        table = new JTable(new MyTableModel());
        int[] alignments = new int[] { JLabel.LEFT, JLabel.CENTER, JLabel.RIGHT };
        table.getTableHeader().getColumnModel().getColumn(0)
        .setHeaderRenderer(new HeaderRenderer(table, alignments[0]));
        table.getTableHeader().getColumnModel().getColumn(2)
        .setHeaderRenderer(new HeaderRenderer(table, alignments[1]));

        table.getTableHeader().setFont(new Font("Times New Roman", Font.BOLD, 28));
        table.setFont(new Font("Times New Roman", Font.PLAIN, 20));
        table.getColumnModel().getColumn(0).setMinWidth(350);
        table.getColumnModel().getColumn(0).setMaxWidth(350);
        table.getColumnModel().getColumn(2).setMinWidth(135);
        table.getColumnModel().getColumn(2).setMaxWidth(135);
        table.getColumnModel().getColumn(3).setMinWidth(125);
        table.getColumnModel().getColumn(3).setMaxWidth(125);
        table.getColumnModel().getColumn(4).setMinWidth(155);
        table.getColumnModel().getColumn(4).setMaxWidth(155);
        table.setRowHeight(30);

        DefaultTableCellRenderer rightRenderer = new DefaultTableCellRenderer();
        rightRenderer.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
        table.getColumn("Quantity").setCellRenderer( rightRenderer );       
        table.getColumn("Price").setCellRenderer( rightRenderer );      
        table.getColumn("Item Total").setCellRenderer( rightRenderer );     

        table.setShowGrid(true);
        Color color = Color.black;
        MatteBorder border = new MatteBorder(1, 1, 0, 0, color);
        table.setBorder(border);
        table.setGridColor(color);
        table.setFillsViewportHeight(true);

        layoutConstraints.gridx = 0;
        layoutConstraints.gridy = 3;
        layoutConstraints.gridwidth = 3;
        layoutConstraints.gridheight = 1;
        layoutConstraints.fill = GridBagConstraints.BOTH;
        layoutConstraints.insets = new Insets(0, 0, 0, 0);
        layoutConstraints.anchor = GridBagConstraints.EAST;
        layoutConstraints.weightx = 1.0;
        layoutConstraints.weighty = 1.0;
        layout.setConstraints(table, layoutConstraints);

        layoutConstraints.gridx = 0;
        layoutConstraints.gridy = 2;
        layoutConstraints.gridwidth = 4;
        layoutConstraints.gridheight = 1;
        layoutConstraints.fill = GridBagConstraints.BOTH;
        layoutConstraints.insets = new Insets(0, 0, 0, 0);
        layoutConstraints.anchor = GridBagConstraints.EAST;
        layoutConstraints.weightx = 0;
        layoutConstraints.weighty = 0;
        layout.setConstraints(table.getTableHeader(), layoutConstraints);


        JScrollPane pane = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

        layoutConstraints.gridx = 0;
        layoutConstraints.gridy = 3;
        layoutConstraints.gridwidth = 3;
        layoutConstraints.gridheight = 1;
        layoutConstraints.fill = GridBagConstraints.BOTH;
        layoutConstraints.insets = new Insets(0, 0, 0, 0);
        layoutConstraints.anchor = GridBagConstraints.EAST;
        layoutConstraints.weightx = 1.0;
        layoutConstraints.weighty = 1.0;
        layout.setConstraints(pane, layoutConstraints);


        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

        add(table);     
        add(table.getTableHeader());
        add(pane);    
    }

    public void tableReset() {

        table.setModel(new MyTableModel());

        int[] alignments = new int[] { JLabel.LEFT, JLabel.CENTER, JLabel.RIGHT };
        table.getTableHeader().getColumnModel().getColumn(0)
        .setHeaderRenderer(new HeaderRenderer(table, alignments[0]));

        table.getTableHeader().getColumnModel().getColumn(2)
        .setHeaderRenderer(new HeaderRenderer(table, alignments[1]));

        table.getTableHeader().setFont(new Font("Times New Roman", Font.BOLD, 28));
        table.setFont(new Font("Times New Roman", Font.PLAIN, 20));
        table.getColumnModel().getColumn(0).setMinWidth(350);
        table.getColumnModel().getColumn(0).setMaxWidth(350);
        table.getColumnModel().getColumn(2).setMinWidth(135);
        table.getColumnModel().getColumn(2).setMaxWidth(135);
        table.getColumnModel().getColumn(3).setMinWidth(125);
        table.getColumnModel().getColumn(3).setMaxWidth(125);
        table.getColumnModel().getColumn(4).setMinWidth(155);
        table.getColumnModel().getColumn(4).setMaxWidth(155);
        table.setRowHeight(30);

        DefaultTableCellRenderer rightRenderer = new DefaultTableCellRenderer();
        rightRenderer.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
        table.getColumn("Quantity").setCellRenderer( rightRenderer );       
        table.getColumn("Price").setCellRenderer( rightRenderer );      
        table.getColumn("Item Total").setCellRenderer( rightRenderer );     
    }

    private static class HeaderRenderer implements TableCellRenderer {
        DefaultTableCellRenderer renderer;
        int horAlignment;
        public HeaderRenderer(JTable table, int horizontalAlignment) {
            horAlignment = horizontalAlignment;
            renderer = (DefaultTableCellRenderer)table.getTableHeader()
                    .getDefaultRenderer();
        }
        public Component getTableCellRendererComponent(JTable table, Object value,
                boolean isSelected, boolean hasFocus, int row, int col) {
            Component c = renderer.getTableCellRendererComponent(table, value,
                    isSelected, hasFocus, row, col);
            JLabel label = (JLabel)c;
            label.setHorizontalAlignment(horAlignment);
            return label;
        }
    }
}

[Update] As requested suggested code from other question

Vertical scrollbar in jTable swing does not appear

Horizontal Scrollbar is not working with JTable in Java Swing

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.io.*;

public class TableCreate extends JFrame
{
    JPanel mainPanel;
    TableCreate() throws IOException
    {

        mainPanel=new JPanel(new BorderLayout());
        String InputFile1 = "TableCreate.java";
        BufferedReader breader1 = new BufferedReader(new FileReader(InputFile1));
        String line1 = "";
        line1 = breader1.readLine();

        DefaultTableModel model1 = new DefaultTableModel();
        model1.addColumn("line");

        while((line1=breader1.readLine()) != null)
         {
             System.out.println(line1);
             model1.addRow(new Object[]{line1});
         }
         breader1.close();

        JTable tab=new JTable(model1);

        tab.setPreferredScrollableViewportSize(new Dimension(300, 200));
        tab.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        TableColumnAdjuster tca = new TableColumnAdjuster(tab);
        tca.adjustColumns();

        JScrollPane js = new JScrollPane(tab);
        add(js);
    }

    public static void main(String[] args) throws IOException
    {
        TableCreate tc=new TableCreate();
        tc.pack();
        tc.setVisible(true);
        tc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
Community
  • 1
  • 1
Noin
  • 1
  • 2
  • 1
    *"..have already tried the suggestions on other stackoverflow questions like this."* Which other questions, list (links to) the top 3. Why did those 3 not help fix this problem? For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). – Andrew Thompson Jul 01 '15 at 16:45
  • `new Font("Times New Roman", ..)` would better be `new Font(Font.SERIF, ..)` for both compile time checking and cross platform robustness. – Andrew Thompson Jul 01 '15 at 16:47
  • What is your problem actually? Because your code is not complete and can not be run. – STaefi Jul 01 '15 at 16:56
  • I can see my JTable but no JScrollPane, I have updated the code, it's just a JPanel that I add to a JFrame – Noin Jul 01 '15 at 16:59
  • @Noin please see my answer. – STaefi Jul 01 '15 at 17:08

1 Answers1

0

Some Obvious problem is in your code. The biggest is:

In the last lines, you are adding your table explicitly to the underlying container. You should change the following line in your PosPanel class:

add(table);                  // remove this line
add(table.getTableHeader()); // remove this line
add(pane); // keep this line 

Typically a JTable should be added to the view-port of a JScrollPane to show it's header. If you add a JTable separately to a container the header would be invisible.

Then the JScrollPane should be added to the container. Do not add that JTable again to the container. It will work. If this doesn't help post your code here for debugging.

Good Luck.

STaefi
  • 4,297
  • 1
  • 25
  • 43
  • Thanks that sort of worked, but had to remove setConstraints for table, table.getTableHeader() and pane. How can set setConstraints without losing the JScrollPane. It also got rid of my custom column widths – Noin Jul 01 '15 at 17:15
  • You should add those constraint related to the gridbaglayout to the JScrollPane instead of the JTable. Because scrollpane is being added to the panel and it contains the JTable inside it's view port. Also I remember to work with table header and it was behaving just as I expected. I can not debug your incomplete code to solve the problem of your column sizes. – STaefi Jul 01 '15 at 18:58