Re: Layout problem driving me insane - panels, resizing, etc
It should be
Container with BorderLayout that contains
JScrollPane with JTable as NORTH
JPanel (FlowLayout or GridLayout or even BoxLayout) with JButton(s) as
CENTER
JTextArea as SOUTH
something like:
JTable tbl = new JTable(5, 8);
JScrollPane scroll = new JScrollPane(tbl);
// 3 to compensate gap between JTable and JTableHeader and Border
int h = tbl.getPreferredSize().height + 3 +
tbl.getTableHeader().getPreferredSize().height;
// --- default preferred size for JScrollPane is about 400x400
scroll.setPreferredSize(new Dimension(scroll.getPreferredSize().width,
h));
JFrame f = new JFrame();
int btnNum = 6;
JPanel btnPanel = new JPanel(new GridLayout(1, btnNum));
for (int i = 0; i < btnNum; i++) {
btnPanel.add(new JButton("Button " + (i + 1)));
}
JTextArea txtArea = new JTextArea(5, 0);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(scroll, BorderLayout.NORTH);
f.getContentPane().add(btnPanel, BorderLayout.CENTER);
f.getContentPane().add(txtArea, BorderLayout.SOUTH);
f.pack();
f.setVisible(true);
jackp@spambob.com wrote:
OK, so I'd like to think I understand enough about all the different
layout managers (and have gotten each of them to work, on its own), but
trying to actually get them to do what I want for my "real" application
is proving to be an exercise in frustration.
I think there are a couple of very basic things I don't understand
about layouts, but I can't for the life of me figure out what they
are... so I hope you can teach a man to fish, as well as give a man a
fish, like the supposedly chinese proverb goes.
My plan is to have an app consisting of a JTable, a row of JButtons
under it, and a JTextArea under that. The table and text area should be
as wide as the window. Now, as for the height, the row of buttons takes
a certain amount, but the table and text area should somehow share
whatever height is available (that's why I thought I should use
GridBagLayout - now I'm not so sure any more).
Ideally, I'd like to specify a row number for the text area, so that
anything more makes a scrollbar appear, but not a number of columns, so
that the area uses all the available width.
I've stripped all of the app-specific stuff from the code, so that
under a hundred lines remain. The resulting code doesn't display the
buttons at all, there is unclaimed space at the bottom of the app (and
between the components?), the text area is only as wide as the text it
is initially displays, and most puzzling: the table doesn't display
initially, but only when the window is resized beyond a certain width.
I'm that close to using absolute positioning, calculating the sizes
manually whenever the window's resized, but what's the right way? Feel
free to point out my stupid mistakes, and/or suggest sweeping changes,
As long as the app behaves the way it should (or close to it) that's
really enough for me.
Without further ado, here's the offending code:
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
public class LayoutProblem extends JFrame {
public static void main(String[] args) {
new LayoutProblem();
}
String columnNames[] = { "Column 1", "Column 2", "Column 3" };
String dataValues[][] = {
{ "0aa","bbb","ccc"}, { "0dd","eee","fff"}, { "0gg","hhh","iii"},
{ "1aa","bbb","ccc"}, { "1dd","eee","fff"}, { "1gg","hhh","iii"},
{ "2aa","bbb","ccc"}, { "2dd","eee","fff"}, { "2gg","hhh","iii"},
{ "3aa","bbb","ccc"}, { "3dd","eee","fff"}, { "3gg","hhh","iii"},
{ "4aa","bbb","ccc"}, { "4dd","eee","fff"}, { "4gg","hhh","iii"},
{ "5aa","bbb","ccc"}, { "5dd","eee","fff"}, { "5gg","hhh","iii"},
{ "6aa","bbb","ccc"}, { "6dd","eee","fff"}, { "6gg","hhh","iii"},
{ "7aa","bbb","ccc"}, { "7dd","eee","fff"}, { "7gg","hhh","iii"},
{ "8aa","bbb","ccc"}, { "8dd","eee","fff"}, { "8gg","hhh","iii"},
{ "9aa","bbb","ccc"}, { "9dd","eee","fff"}, { "9gg","hhh","iii"},
{ "aaa","bbb","ccc"}, { "ddd","eee","fff"}, { "ggg","hhh","iii"},
};
public LayoutProblem() {
JTable table;
JButton button1, button2, button3, button4;
JTextArea logArea;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container cont = getContentPane();
cont.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
table = new JTable(dataValues, columnNames);
JScrollPane scrollPane = new JScrollPane(table);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
c.gridx = 0; c.gridy = 0; c.gridwidth = 1; c.gridheight= 1; c.weightx
= 1.0; c.weighty=1.0;
c.anchor = GridBagConstraints.CENTER;
cont.add(scrollPane, c);
JPanel buttonPanel = new JPanel();
buttonPanel.setBorder( BorderFactory.createRaisedBevelBorder() );
SpringLayout buttonPanelLayout = new SpringLayout();
buttonPanel.setLayout(buttonPanelLayout);
final int PADDING = 6;
button1 = new JButton("Button 1");
buttonPanelLayout.putConstraint(SpringLayout.WEST, button1, PADDING,
SpringLayout.WEST, buttonPanel);
buttonPanelLayout.putConstraint(SpringLayout.NORTH, button1, PADDING,
SpringLayout.NORTH, buttonPanel);
buttonPanel.add(button1);
button2 = new JButton("Button 2");
buttonPanelLayout.putConstraint(SpringLayout.WEST, button2, PADDING,
SpringLayout.EAST, button1);
buttonPanelLayout.putConstraint(SpringLayout.NORTH, button2, PADDING,
SpringLayout.NORTH, buttonPanel);
buttonPanel.add(button2);
button3 = new JButton("Button 3");
buttonPanelLayout.putConstraint(SpringLayout.WEST, button3, PADDING,
SpringLayout.EAST, button2);
buttonPanelLayout.putConstraint(SpringLayout.NORTH, button3, PADDING,
SpringLayout.NORTH, buttonPanel);
buttonPanel.add(button3);
button4 = new JButton("Button 4");
buttonPanelLayout.putConstraint(SpringLayout.WEST, button4, PADDING,
SpringLayout.EAST, button3);
buttonPanelLayout.putConstraint(SpringLayout.NORTH, button4, PADDING,
SpringLayout.NORTH, buttonPanel);
buttonPanel.add(button4);
buttonPanel.validate(); // is this needed? does it do anything?
c.gridx = 0; c.gridy = 1; c.gridwidth = 1; c.gridheight= 1; c.weightx
= 1.0; c.weighty=0.0;
c.anchor = GridBagConstraints.NORTHWEST;
cont.add(buttonPanel, c);
logArea = new JTextArea();
c.gridx = 0; c.gridy = 2; c.gridwidth = 1; c.gridheight= 1; c.weightx
= 1.0; c.weighty=1.0;
c.anchor = GridBagConstraints.NORTHWEST;
logArea.setText("---------------------------\n---------------------------\n---------------------------\n");
cont.add(logArea, c);
pack();
setSize(450,600); // initial app size
setVisible(true);
}
}
// thanks for making it this far.