-
Components in Gui, how to place?
Hello,
I have a problem placing a label above a textArea, i tried allot of things but i dont know how to do it, the current gui places the label next to the text area and not above.
code:
public Container createContentPane() {
//Create the content-pane-to-be.
JPanel contentPane = new JPanel(new BorderLayout());
contentPane.setOpaque(true);
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
//Create a scrolled text area.
output = new JTextArea("",5, 30);
output.setEditable(false);
sp1 = new JScrollPane(output);
//Create a scrolled text area.
char_info = new JTextArea("Character info",30, 15);
char_info.setEditable(false);
sp2 = new JScrollPane(char_info);
//Add the text area to the content pane.
contentPane.add(sp1, BorderLayout.CENTER);
contentPane.add(p1, BorderLayout.EAST);
p1.add(info, BorderLayout.NORTH);
p1.add(sp2, BorderLayout.SOUTH);
contentPane.add(p2, BorderLayout.SOUTH);
return contentPane;
}
-
try using more then one JPanel.
To make this work you'll prolly need 3 JPanels, all with different layouts.
The top panel will have a border layout. On this you place the two other panels. One containing the label and one containing the text area. The panel containing the label will be placed on the root panel to the NORTH. The text are will be located in the CENTER.
This way, the text area will encompass the remaining area of the root panel.
Now you can place the label to the left of the top panel easy.
-
If you don't care about resizing, e.g. the components will stay in the same place
with the same size when the container (frame) is resized, you could specify a null
layout and use the setBounds(x,y,width,height) method for each component you
add to the container (xy-layout).
eschew obfuscation
-
Honestly, BorderLayout()s are great when you are just starting out and using multiple panels or only want to have components ordered is a simplistic manner. On the other hand, I have found them to be very much a pain. Although I'm sure that I am just missing some capable layout that will do what I want, here is my solution (it is basically the same as the above post):
Code:
// import statements here
public class someClass extends JFrame {
private JTextField tField;
private JButton b1, b2;
public someClass() {
//code to initiate frame and other vars needed
createComponents();
this.show();
}
public void createComponents() {
Container c = getContentPane();
c.setLayout(null); //key code, a null layout, this means that you will
//expected to set (by pixel) the location and dimensions
//of each component.
tField = new JTextField("Some initial text");
tField.setBounds(10,10,300,20); //seting tField by (x,y,width,height)
b1 = new JButton("Button Text");
b1.setBounds(10,40,135,20); //set by same values
b2 = new JButton("Button Text2");
b2.setBounds(155,40,135,20); //set by same values
c.add(tField, null);
c.add(b1, null);
c.add(b2, null);
}
} // class someClass
I hope this was helpful, and if I missed any important code, or you would like to to elaborate, please feel free to comment.
Last edited by Zodoz; 07-25-2005 at 10:50 PM.
Reason: c.add(component); might not have worked right.
Similar Threads
-
Replies: 0
Last Post: 05-29-2005, 08:54 AM
-
Replies: 1
Last Post: 11-27-2001, 06:53 AM
-
By Zane Thomas in forum dotnet.announcements
Replies: 0
Last Post: 10-17-2001, 10:51 PM
-
By Zane Thomas in forum .NET
Replies: 0
Last Post: 10-17-2001, 02:37 AM
-
By Rob Teixeira in forum .NET
Replies: 3
Last Post: 03-17-2001, 03:17 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks