简体   繁体   中英

Why does not table data show?

Can any one tell me why my table is not displaying, the frame appears although the data, columns and rows. I have removed any unnecessary field and methods.


public class DDPSPLandscaper extends JFrame {

private static final long serialVersionUID = -49372444918464235883L;
private JPanel contentPane;
private JTable table = new JTable(new TableData());

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                DDPSPLandscaper frame = new DDPSPLandscaper();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public DDPSPLandscaper() {
    super("DDPSPLandscaping Tool");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 780, 494);

    JMenuBar menuBar = new JMenuBar();
    setJMenuBar(menuBar);

    JTextArea textArea_4 = new JTextArea();
    menuBar.add(textArea_4);
    textArea_4.setEditable(false);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    GridBagLayout gbl_contentPane = new GridBagLayout();
    gbl_contentPane.columnWidths = new int[]{0, 0, 0, 145, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0};
    gbl_contentPane.rowHeights = new int[]{33, 0, 0, 0, 0, 0, 0, 0, 0, -7, 0, 0, 0};
    gbl_contentPane.columnWeights = new double[]{1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, Double.MIN_VALUE};
    gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
    contentPane.setLayout(gbl_contentPane);

    ...

    table = new JTable();
    GridBagConstraints gbc_table = new GridBagConstraints();
    gbc_table.gridwidth = 5;
    gbc_table.gridheight = 3;
    gbc_table.insets = new Insets(0, 0, 5, 5);
    gbc_table.fill = GridBagConstraints.BOTH;
    gbc_table.gridx = 3;
    gbc_table.gridy = 5;
    contentPane.add(table, gbc_table);
    JScrollPane scrollPane = new JScrollPane(table);
    table.setFillsViewportHeight(true);
    contentPane.add(scrollPane, gbc_table);

}

class TableData extends AbstractTableModel {

    private static final long serialVersionUID = 1L;

    private String[] columnNames = {"Item", "Name", "whight/size", "selct"};

    private Object[][] data = {
        { "Campione", "Snowboarding", new Integer(5),
            new Boolean(false) },
        { "Huml", "Rowing", new Integer(3), new Boolean(true) },
        { "Kathy", "Walrath", "Knitting", new Integer(2),
            new Boolean(false) },
        { "Zakhour", "Speed reading", new Integer(20),
            new Boolean(true) },
        { "Milne", "Pool", new Integer(10),
            new Boolean(false) } };

    public int getColumnCount() {
      return columnNames.length;
    }

    public int getRowCount() {
      return data.length;
    }

    public String getColumnName(int col) {
      return columnNames[col];
    }

    public Object getValueAt(int row, int col) {
      return data[row][col];
    }

}

}

Try removing the line

contentPane.add(table, gbc_table);

The table is the view component of your JScrollPane ; it probably shouldn't also be added to the JFrame's content pane.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM