Reputation: 8865
I need to display 2 JTable
's along x-axis. I am able to display them vertically (Y-axis.) This is what so far I have done:
But I want to display tables like the following,
Here is my code:
tableA = new JTable(data, colNames);
tableB = new JTable(data, colNames);
JLabel labelA = new JLabel("Table-A");
JLabel labelB = new JLabel("Table-B");
JButton bt_copy = new JButton("Copy");
Container c = frame.getContentPane();
c.setLayout(new BoxLayout(c, BoxLayout.Y_AXIS));
c.add(labelA);
c.add(tableA.getTableHeader());
c.add(tableA);
c.add(labelB);
c.add(tableB.getTableHeader());
c.add(tableB);
c.add(bt_copy);
When I changed Y-axis in c.setLayout(new BoxLayout(c, BoxLayout.Y_AXIS));
to X-axis. I really got a bad GUI view.
Upvotes: 1
Views: 832
Reputation: 347214
Something like this ??
JTable leftTable = new JTable();
JTable rightTable = new JTable();
addButton = new JButton("Add >>");
removeButton = new JButton("<< Remove");
setLayout(new GridBagLayout());
// Prepare the buttons panel...
JPanel pnlActions = new JPanel(new GridBagLayout());
pnlActions.setBorder(new LineBorder(Color.RED));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 1;
gbc.anchor = GridBagConstraints.SOUTH;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
pnlActions.add(addButton, gbc);
gbc.weighty = 0;
gbc.gridy++;
pnlActions.add(removeButton, gbc);
// Prepare the main layout
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 0.33;
gbc.fill = GridBagConstraints.BOTH;
gbc.weighty = 1;
add(new JScrollPane(leftTable), gbc);
gbc.gridx = 2;
add(new JScrollPane(rightTable), gbc);
gbc.gridx = 1;
gbc.gridy++;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weighty = 0;
add(pnlActions, gbc);
Upvotes: 2
Reputation: 6465
Try using a Box for the contentPane. You'll need to use a combination of boxes to get the desired layout you want. Here's an example that should get you close:
tableA = new JTable(data, colNames);
tableB = new JTable(data, colNames);
JLabel labelA = new JLabel("Table-A");
JLabel labelB = new JLabel("Table-B");
JButton bt_copy = new JButton("Copy");
Box v = Box.createVerticalBox();
frame.setContentPane(v);
Box c = Box.createHorizontalBox();
v.add(c);
JScrollPane jsp = new JScrollPane(tableA);
c.add(jsp);
jsp = new JScrollPane(tableB);
c.add(jsp);
Box c2 = Box.createHorizontalBox();
c2.add(Box.createHorizontalGlue());
c2.add(bt_copy);
c2.createHorizontalGlue();
v.add(c2);
Upvotes: 0
Reputation: 6783
I would suggest using a GridBagLayout
instead of BoxLayout
. And instead of adding everything to a JFrame
itself, try adding a JPanel
to the frame and adding your tables to the frame. (This would mean that you would set the layout of the panel to GridBagLayout
).
If you are new to Layout Managers, try to read the Visual Guide To Layout manager. It is pretty informative.
Upvotes: 2