Re: Layout problem driving me insane - panels, resizing, etc
<jackp@spambob.com> wrote in message
news:1150220142.633907.41210@f6g2000cwb.googlegroups.com...
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:
[snip]
It's been a while since I did Swing coding (been doing mostly SWT these
days). Here's what I got. It does most of what you want, except the
JTextArea doesn't fill the width, and I can't remember how to do that.
Perhaps someone else can chime in.
<SSCCE>
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
public class LayoutProblem extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
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() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buildGUI();
pack();
setVisible(true);
}
private void buildGUI() {
this.setLayout(new GridBagLayout());
{
JTable table = new JTable(dataValues, columnNames);
JScrollPane scrollPane = new JScrollPane(table);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 1;
c.gridheight = 1;
c.weightx = 1.0;
c.weighty = 1.0;
c.anchor = GridBagConstraints.CENTER;
this.add(scrollPane, c);
}
{
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 1;
c.gridheight = 1;
c.weightx = 1.0;
c.weighty = 0.0;
c.anchor = GridBagConstraints.CENTER;
this.add(buildButtonPanel(), c);
}
{
JTextArea logArea = new JTextArea();
logArea
.setText("---------------------------\n---------------------------\n---------------------------\n");
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 1;
c.gridheight = 1;
c.weightx = 1.0;
c.weighty = 1.0;
c.anchor = GridBagConstraints.CENTER;
this.add(new JScrollPane(logArea), c);
}
}
private JPanel buildButtonPanel() {
JPanel buttonPanel = new JPanel();
buttonPanel.setBorder(BorderFactory.createRaisedBevelBorder());
BoxLayout buttonPanelLayout = new BoxLayout(buttonPanel,
BoxLayout.LINE_AXIS);
buttonPanel.setLayout(buttonPanelLayout);
JButton button1, button2, button3, button4;
button1 = new JButton("Button 1");
buttonPanel.add(button1);
button2 = new JButton("Button 2");
buttonPanel.add(button2);
button3 = new JButton("Button 3");
buttonPanel.add(button3);
button4 = new JButton("Button 4");
buttonPanel.add(button4);
return buttonPanel;
}
}
</SSCCE>
- Oliver