简体   繁体   中英

Why will my JTable display on a new JFrame but not on my existing JPanel?

I am working with JDBC. My Class has a JFrame with a JTabbedPane to display my JPanels with the UI's for my different methods. On this panel I want to display a result set in a JTable with to additional columns of buttons. This is all currently working when I display it on a new JFrame but not when I try to display it on the existing one. Could somebody please explain why.

JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
    contentPane.add(tabbedPane, BorderLayout.CENTER);

JPanel panel = new JPanel();
    tabbedPane.addTab("Main", null, panel, null);
    panel.setLayout(null);


JTable table = new JTable(model);

    DisplayButtonColumn testWithButtons1 = new DisplayButtonColumn(table,
            displayHandler.getColCount());
    DisplayButtonColumn testWithButtons2 = new DisplayButtonColumn(table,
            displayHandler.getColCount() + 1);

    panel.add(new JScrollPane(table));

//      JFrame f = new JFrame();
//      f.setSize(1000, 500);
//      f.getContentPane().add(new JScrollPane(table));
//      f.setVisible(true);
panel.setLayout(null);

The problem is the null layout. Don't do that . Swing is designed to be used with layout managers and not doing that will lead to trouble almost without exception. Using absolute layout would require you to manually manage the bounds to the components, and you're not doing that. If you need something else than the default FlowLayout , just create one that matches your needs:

panel.setLayout(new BorderLayout());

(It would be also possible to add to JTabbedPane without the helper panel, but I suppose you want more components in the tab).

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