I have a JTable which shows up fine. If I place the Table in a JScrollPane it does not show anymore. Why not? I would like to add both tables each in an own JScrollPane. Here my code in which I am only trying to add a JScrollPane to the first table:

private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 1000, 800);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JProgressBar progressBar = new JProgressBar();
    progressBar.setBounds(50, 68, 700, 14);
    frame.getContentPane().add(progressBar);

    JTextPane txtpnAutomotiveHmi = new JTextPane();
    txtpnAutomotiveHmi.setText("Automotive HMI");
    txtpnAutomotiveHmi.setBounds(362, 21, 205, 20);
    frame.getContentPane().add(txtpnAutomotiveHmi);


    testcase_table = new JTable();
    testcase_table.setBounds(50, 125, 350, 426);
    JScrollPane scroll_testcase = new JScrollPane(testcase_table);
    frame.getContentPane().add(scroll_testcase);

    teststep_table = new JTable();
    teststep_table.setBounds(399, 125, 350, 426);
    frame.getContentPane().add(teststep_table);
}

Thanks.

有帮助吗?

解决方案

Do not set the null layout. It is not the "default layout" and is not "the simplest to implement". It is actually very difficult to get right.

JFrame uses BorderLayout by default. Add JProgressBar with BorderLayout.NORTH, JTextPane with BorderLayout.SOUTH and JScrollPane with BorderLayout.CENTER. Remove all setBounds. The components will appear. If you need to position them in some sophisticated way instead, read about GridBagLayout.

其他提示

Define the size of the table and set the same size to scroll pane.

scrollPane.setSize(table.getSize());
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top