Link to home
Start Free TrialLog in
Avatar of muskad202
muskad202

asked on

problems with layout

hi !!

i'm using the following layout code
------------
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

dummy = new JPanel();
dummy.setLayout(new BoxLayout(dummy,BoxLayout.X_AXIS));
dummy.add(Box.createHorizontalGlue());
dummy.add(new JLabel("Refresh"));
dummy.add(Refresh); //Refresh is a JButton
dummy.add(Box.createHorizontalGlue());
add(dummy);

add(Box.createVerticalGlue());
dummy = new JPanel();
dummy.setLayout(new BoxLayout(dummy,BoxLayout.X_AXIS));
table = new JTable(null);
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
dummy.add(scrollPane);
add(dummy);
add(Box.createVerticalGlue());

dummy = new JPanel();
Messages = new JTextArea();
Messages.setText("Errors (if any):\n");
Messages.setColumns(60);
Messages.setRows(10);
Messages.setEditable(false);
JScrollPane jsp = new JScrollPane(Messages);
jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
dummy.add(jsp);
add(dummy);
add(Box.createVerticalGlue());
-----------------------

what's happening is, even when the window is maximized, the horizontal scrollbar of the textarea at the bottom isn't shown .. i.e, the horizontal scrollbar lies outside the window, and i cannot see it. how do i fix this ?? (what should happen, is that the textarea should resize automatically so that the bottom scrollbar is shown, and the user can then use the vertical scrollbar to scroll through the 10 rows.)

also, can i do away with the "Messages.setColumns(60)" line ?? i want the textarea to take up all the horizontal space that is available ..

thanks :)
muskad202
Avatar of zzynx
zzynx
Flag of Belgium image

>> i want the textarea to take up all the horizontal space that is available
Use a BorderLayout for this behaviour.

Main panel: BorderLayout with

In the north: your current BoxLayout (having the two parts)
In the center: your text area
Something like  (changed lines marked with // <<<<<<<<<)

setLayout( new BorderLayout() );       // <<<<<<<<<

JPanel mainPanel = new JPanel();      // <<<<<<<<<
mainPanel.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));  // <<<<<<<<<

dummy = new JPanel();
dummy.setLayout(new BoxLayout(dummy,BoxLayout.X_AXIS));
dummy.add(Box.createHorizontalGlue());
dummy.add(new JLabel("Refresh"));
dummy.add(Refresh); //Refresh is a JButton
dummy.add(Box.createHorizontalGlue());
mainPanel.add(dummy); // <<<<<<<<<

mainPanel.add(Box.createVerticalGlue()); // <<<<<<<<<
dummy = new JPanel();
dummy.setLayout(new BoxLayout(dummy,BoxLayout.X_AXIS));
table = new JTable(null);
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
dummy.add(scrollPane);
mainPanel.add(dummy); // <<<<<<<<<
mainPanel.add(Box.createVerticalGlue()); // <<<<<<<<<

add(mainPanel, BorderLayout.NORTH); // <<<<<<<<<

dummy = new JPanel();
Messages = new JTextArea();
Messages.setText("Errors (if any):\n");
//Messages.setColumns(60);   // <<<<<<<<<
//Messages.setRows(10);        // <<<<<<<<<
Messages.setEditable(false);
JScrollPane jsp = new JScrollPane(Messages);
jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
dummy.add(jsp);
add(dummy, BorderLayout.CENTER); // <<<<<<<<<
//add(Box.createVerticalGlue()); // <<<<<<<<<
Avatar of muskad202
muskad202

ASKER

hi !!

@zzynx : the code didn't work all that well .. the textarea at the bottom became really small, and didn't take up all the horizontal area ..

also, i needed to change the layout a bit (had to add another textarea), so what i am trying to get is something like :

========================|
             JLabel - JButton                  |
-------------------------------------------|
                          |                          |
                          |                          |
         JTable        |       TextArea1    |
                          |                          |
                          |                          |
-------------------------------------------|
                   TextArea2                    |
========================|

i'm using the code you gave me, but modified a little
----------------------------------

setLayout(new BorderLayout());

dummy = new JPanel();
dummy.add(new JLabel("Refresh"));
dummy.add(Refresh);
add(dummy,BorderLayout.NORTH);

dummy = new JPanel();
dummy.setLayout(new BoxLayout(dummy,BoxLayout.X_AXIS));
table = new JTable(null);
table.getSelectionModel().addListSelectionListener(this);
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
dummy.add(scrollPane);
add(dummy,BorderLayout.WEST);

dummy = new JPanel();
dummy.setLayout(new BoxLayout(dummy,BoxLayout.X_AXIS));
Info = new JTextArea();
jsp = new JScrollPane(Info);
jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
dummy.add(jsp);
add(dummy,BorderLayout.EAST);

dummy = new JPanel();
Messages = new JTextArea();
Messages.setText("Errors (if any):\n");
Messages.setEditable(false);
jsp = new JScrollPane(Messages);
jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
dummy.add(jsp);
add(dummy,BorderLayout.SOUTH);
-----------------------------------------------------------------------

(1) The reason i'm yet using the 2 BoxLayouts, is that if i don't, the height of the table extends to behind TextArea2, while height of TextArea1 is nearly 0 (since there is no text in it)
(2) However, the above is yet causing a problem. :
  (a) thewidth of text area1 is nearly 0 (the thing is, its not that jtable is taking up all the horizontal area; it is just taking up about 60% , but even then, textarea2 is not grabbing the remaining 40% area). NOTE - the jtable is actually a jtable inside a jscrollpane, as can be seen from the code
  (b) the width of textarea2 is just enough so that the words "Errors if any" fit in
  (c) height of textarea2 is just 2 rows. (i guess it is 2 rows becoz only 2 rows are required to show the scrollbars)

(2)(c) isn't much of a problem, becoz i can just use Messages.setRows(6) to maintain a height of atleast 6 rows.

can you help me with (2)(b) and (2)(a) ??

thanks :)
muskad202
ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Think, you'll have to rethink the layout
aaaaaaaaaaaaarrrrggggh ... i hate these layouts :) .. i've spent an entire day on this s*** ....

how would you go about laying out the above stuff ??

(assuming textarea2 should take atleast 6 rows ..(Messages.setRows(6) )
(the jtable and textarea1 should divide the horizontal area among them selves)
(they should also grab all the vertical area they can, after leaving 6 rows for textarea2 and the amount required for the jlabel/jbutton on top)

thanks :)
muskad202
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
the dumb thing yet didn't work !!! :) :) same problem as before, the textarea1 has a width of 0. however, this time around, when i add some text to it at runtime, it expands and even stretches over the jtable !!! when i click on the area where the jtable is supposed to be (i can't see it, because the textarea1 is now on top), the textarea1 then shrinks back to the appropriate size (i.e., it now is the proper dimension that i wanted it to be), and the table is visible again. however, same problem if i click anywhere in the textarea1.

i think the easiest thing is too fix the window size (specifying pixels), and then use setbounds for all the components ..

what a drag ...

anywayz, thankz for your help ..
Thanks