Solved

problems with layout

Posted on 2004-10-05
9
364 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
  • 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
 
LVL 37

Accepted Solution

by:
zzynx earned 40 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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 20 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now