ProAnswers.org

How to add JTable in JPanel?

You can make use of the following code. To add JTable to JPanel.

JPanel panel = new JPanel();

this.setContentPane(panel);

panel.setLayout(null);

String data[][] = {{“1.”, “ABC”}, {“2.”, “DEF”}, {“3.”, “GHI” }};

String col[] = {“Sr. No”, “Name”};

JTable table = new JTable(data,col);

table.setBounds(100, 100, 100, 80);

panel.add(table);

setVisible(true);

setSize(300,300);

JTable should be added into the JScrollPane which actually should be added into the JPanel.

The JPanel should have some layout manager.

If you don’t care about the precision of components size you can use pure BorderLayout and combine it with FlowLayout and GridLayout. if you need precision - use jgoodies FormLayout.

The FormLayout is really tricky one, but you can play a little with WindowBuilder (which is embedded into Eclipse) and a look at the code it generates. It may look complicated but it is just an ignorance.