?
Solved

problems with layout

Posted on 2004-10-05
9
Medium Priority
?
379 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:muskad202
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 12225816
>> 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
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12225843
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()); // <<<<<<<<<
0
 
LVL 2

Author Comment

by:muskad202
ID: 12226120
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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 37

Accepted Solution

by:
zzynx earned 160 total points
ID: 12226207
>> (b) the width of textarea2 is just enough so that the words "Errors if any" fit in
Of course, you added the last panel in the SOUTH. Only stuff in the center stretches.
>> (a) ... textarea2 is not grabbing the remaining 40% area)
No, since it is on the EAST. Again: Only stuff placed in the center of a border layout stretches.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12226219
Think, you'll have to rethink the layout
0
 
LVL 2

Author Comment

by:muskad202
ID: 12226357
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
0
 
LVL 16

Assisted Solution

by:krakatoa
krakatoa earned 80 total points
ID: 12230155
Try:

Primary box - vertical, from Box.createVerticalBox() static call;

then

three more boxes, all horizontal.

Add your first button to the first of these four, your table and textarea1 to the next one and the final textarea to the third.

(Encase your table in a scrollpane if necessary).

then all everything to the primary box.

good luck.
0
 
LVL 2

Author Comment

by:muskad202
ID: 12232059
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 ..
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12239118
Thanks
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month12 days, 20 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question