質問

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