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.createHorizo ntalGlue() );
dummy.add(new JLabel("Refresh"));
dummy.add(Refresh); //Refresh is a JButton
dummy.add(Box.createHorizo ntalGlue() );
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.setHorizontalSc rollBarPol icy(JScrol lPane.HORI ZONTAL_SCR OLLBAR_ALW AYS);
scrollPane.setVerticalScro llBarPolic y(JScrollP ane.VERTIC AL_SCROLLB AR_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.setHorizontalScrollBar Policy(JSc rollPane.H ORIZONTAL_ SCROLLBAR_ ALWAYS);
jsp.setVerticalScrollBarPo licy(JScro llPane.VER TICAL_SCRO LLBAR_ALWA YS);
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
i'm using the following layout code
------------
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
dummy = new JPanel();
dummy.setLayout(new BoxLayout(dummy,BoxLayout.
dummy.add(Box.createHorizo
dummy.add(new JLabel("Refresh"));
dummy.add(Refresh); //Refresh is a JButton
dummy.add(Box.createHorizo
add(dummy);
add(Box.createVerticalGlue
dummy = new JPanel();
dummy.setLayout(new BoxLayout(dummy,BoxLayout.
table = new JTable(null);
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setHorizontalSc
scrollPane.setVerticalScro
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.setHorizontalScrollBar
jsp.setVerticalScrollBarPo
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
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.createHorizo ntalGlue() );
dummy.add(new JLabel("Refresh"));
dummy.add(Refresh); //Refresh is a JButton
dummy.add(Box.createHorizo ntalGlue() );
mainPanel.add(dummy); // <<<<<<<<<
mainPanel.add(Box.createVe rticalGlue ()); // <<<<<<<<<
dummy = new JPanel();
dummy.setLayout(new BoxLayout(dummy,BoxLayout. X_AXIS));
table = new JTable(null);
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setHorizontalSc rollBarPol icy(JScrol lPane.HORI ZONTAL_SCR OLLBAR_ALW AYS);
scrollPane.setVerticalScro llBarPolic y(JScrollP ane.VERTIC AL_SCROLLB AR_ALWAYS) ;
dummy.add(scrollPane);
mainPanel.add(dummy); // <<<<<<<<<
mainPanel.add(Box.createVe rticalGlue ()); // <<<<<<<<<
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.setHorizontalScrollBar Policy(JSc rollPane.H ORIZONTAL_ SCROLLBAR_ ALWAYS);
jsp.setVerticalScrollBarPo licy(JScro llPane.VER TICAL_SCRO LLBAR_ALWA YS);
dummy.add(jsp);
add(dummy, BorderLayout.CENTER); // <<<<<<<<<
//add(Box.createVerticalGl ue()); // <<<<<<<<<
setLayout( new BorderLayout() ); // <<<<<<<<<
JPanel mainPanel = new JPanel(); // <<<<<<<<<
mainPanel.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); // <<<<<<<<<
dummy = new JPanel();
dummy.setLayout(new BoxLayout(dummy,BoxLayout.
dummy.add(Box.createHorizo
dummy.add(new JLabel("Refresh"));
dummy.add(Refresh); //Refresh is a JButton
dummy.add(Box.createHorizo
mainPanel.add(dummy); // <<<<<<<<<
mainPanel.add(Box.createVe
dummy = new JPanel();
dummy.setLayout(new BoxLayout(dummy,BoxLayout.
table = new JTable(null);
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setHorizontalSc
scrollPane.setVerticalScro
dummy.add(scrollPane);
mainPanel.add(dummy); // <<<<<<<<<
mainPanel.add(Box.createVe
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.setHorizontalScrollBar
jsp.setVerticalScrollBarPo
dummy.add(jsp);
add(dummy, BorderLayout.CENTER); // <<<<<<<<<
//add(Box.createVerticalGl
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.NOR TH);
dummy = new JPanel();
dummy.setLayout(new BoxLayout(dummy,BoxLayout. X_AXIS));
table = new JTable(null);
table.getSelectionModel(). addListSel ectionList ener(this) ;
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setHorizontalSc rollBarPol icy(JScrol lPane.HORI ZONTAL_SCR OLLBAR_ALW AYS);
scrollPane.setVerticalScro llBarPolic y(JScrollP ane.VERTIC AL_SCROLLB AR_ALWAYS) ;
dummy.add(scrollPane);
add(dummy,BorderLayout.WES T);
dummy = new JPanel();
dummy.setLayout(new BoxLayout(dummy,BoxLayout. X_AXIS));
Info = new JTextArea();
jsp = new JScrollPane(Info);
jsp.setHorizontalScrollBar Policy(JSc rollPane.H ORIZONTAL_ SCROLLBAR_ ALWAYS);
jsp.setVerticalScrollBarPo licy(JScro llPane.VER TICAL_SCRO LLBAR_ALWA YS);
dummy.add(jsp);
add(dummy,BorderLayout.EAS T);
dummy = new JPanel();
Messages = new JTextArea();
Messages.setText("Errors (if any):\n");
Messages.setEditable(false );
jsp = new JScrollPane(Messages);
jsp.setHorizontalScrollBar Policy(JSc rollPane.H ORIZONTAL_ SCROLLBAR_ ALWAYS);
jsp.setVerticalScrollBarPo licy(JScro llPane.VER TICAL_SCRO LLBAR_ALWA YS);
dummy.add(jsp);
add(dummy,BorderLayout.SOU TH);
-------------------------- ---------- ---------- ---------- ---------- -----
(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
@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.NOR
dummy = new JPanel();
dummy.setLayout(new BoxLayout(dummy,BoxLayout.
table = new JTable(null);
table.getSelectionModel().
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setHorizontalSc
scrollPane.setVerticalScro
dummy.add(scrollPane);
add(dummy,BorderLayout.WES
dummy = new JPanel();
dummy.setLayout(new BoxLayout(dummy,BoxLayout.
Info = new JTextArea();
jsp = new JScrollPane(Info);
jsp.setHorizontalScrollBar
jsp.setVerticalScrollBarPo
dummy.add(jsp);
add(dummy,BorderLayout.EAS
dummy = new JPanel();
Messages = new JTextArea();
Messages.setText("Errors (if any):\n");
Messages.setEditable(false
jsp = new JScrollPane(Messages);
jsp.setHorizontalScrollBar
jsp.setVerticalScrollBarPo
dummy.add(jsp);
add(dummy,BorderLayout.SOU
--------------------------
(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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Think, you'll have to rethink the layout
ASKER
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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 ..
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
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