Results 1 to 10 of 10
Thread: BorderLayout NORTH small?
- 03-09-2012, 07:40 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 23
- Rep Power
- 0
BorderLayout NORTH small?
Hey I haven't been at java for long and just got a question about borderlayouts. I have two Jpanels in the jframe in borderlayout, the first has multiple textboxes, and buttons in, the second will be used as a drawing canvas. I'm having a problem when I put the button panel as NORTH and the drawing one as center the north panel is really small and you're unable to see all the content. Is there a way to set the size of Jpanels or is that not an option? Thanks.
Screenshot:
-
Re: BorderLayout NORTH small?
Without code, it's very difficult if not impossible to guess what is wrong or how to correct it (which should be pretty much obvious, I would think).
Are you trying to set the size of anything? If so, it's usually a bad idea to do.
Are you using FlowLayout for the JPanel at the top? If so, there are usually better layouts to use.
For more help, you'll probably need to give us more information.
edit: note that I believe that the BorderLayout will try to respect a JPanel's preferredSize, so again something is wrong with your code making this not work well. Again, we need to see the code. How much? hard to say -- enough so we can figure out what's wrong but not so much as to drown us in tons of unrelated code.Last edited by Fubarable; 03-09-2012 at 07:48 PM.
- 03-09-2012, 07:51 PM #3
Member
- Join Date
- Mar 2012
- Posts
- 23
- Rep Power
- 0
Re: BorderLayout NORTH small?
public JPanels(){
setSize(550,600);
setTitle("JPanel example");
setLayout(new BorderLayout());
setResizable(false);
Container content = getContentPane();
JPanel controlArea = new JPanel();
JPanel drawingArea = new JPanel();
controlArea.setBackground(Color.red);
drawingArea.setBackground(Color.blue);
xstartInput = new JTextField(12);
controlArea.add(xstartInput);
content.add(controlArea, BorderLayout.NORTH);
content.add(drawingArea, BorderLayout.CENTER);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
Pretty much that but with more buttons and textboxes.
-
Re: BorderLayout NORTH small?
There's one of your problems:
You don't want to set the size of anything. Instead perhaps set the preferredSize of the central JPanel but let the GUI set the actual sizes of components. Be sure to call pack() on your JFrame after filling it with components and before calling setVisible(true) on it.Java Code:setSize(550,600);
- 03-09-2012, 08:02 PM #5
Member
- Join Date
- Mar 2012
- Posts
- 23
- Rep Power
- 0
Re: BorderLayout NORTH small?
Cheers for the replies. The problem is that I want the overall window to be set to that size to begin with, if I take it out it just goes massive across the screen. I added the pack into it. Also say I put all my textboxes in (they all appear in one long line) but I only wanted 2 on the one line then for the others to continue below them, how would I go about doing that? Thanks.
-
Re: BorderLayout NORTH small?
How many JTextFields does the top JPanel display? Can you show what you want it to look like?
Consider creating and posting a small GUI that compiles and runs for us and shows us your problem. This program adds components to JPanels but doesn't do anything else, something like so...
Java Code:import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import javax.swing.*; public class LayoutEg extends JPanel { public LayoutEg() { int fieldCols = 8; JPanel northPanel = new JPanel(new GridBagLayout()); northPanel.add(new JLabel("Enter Foo:"), createGBC(0, 0)); northPanel.add(new JTextField(fieldCols), createGBC(1, 0)); northPanel.add(new JLabel("Enter Bar:"), createGBC(2, 0)); northPanel.add(new JTextField(fieldCols), createGBC(3, 0)); northPanel.add(new JLabel("Enter Spam:"), createGBC(0, 1)); northPanel.add(new JTextField(fieldCols), createGBC(1, 1)); northPanel.add(new JLabel("Enter Baz:"), createGBC(2, 1)); northPanel.add(new JTextField(fieldCols), createGBC(3, 1)); northPanel.setBackground(Color.red); JPanel centerPanel = new JPanel(); centerPanel.setPreferredSize(new Dimension(500, 400)); centerPanel.setBackground(Color.blue); setLayout(new BorderLayout()); add(northPanel, BorderLayout.NORTH); add(centerPanel, BorderLayout.CENTER); } private GridBagConstraints createGBC(int x, int y) { GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = x; gbc.gridy = y; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.weightx = (x % 2 == 0) ? 0.0 : 1.0; gbc.weighty = 1.0; gbc.anchor = (x % 2 == 0) ? gbc.LINE_START : gbc.LINE_END; gbc.fill = (x % 2 == 0) ? gbc.BOTH : gbc.HORIZONTAL; gbc.insets = new Insets(5, 5, 5, 5); return gbc; } private static void createAndShowGui() { JFrame frame = new JFrame("LayoutEg"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new LayoutEg()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGui(); } }); } }
- 03-09-2012, 09:43 PM #7
Member
- Join Date
- Mar 2012
- Posts
- 23
- Rep Power
- 0
Re: BorderLayout NORTH small?
I don't understand that Gridlayout or what the createGBC(0, 0)); parts are for. I will try and get something up tomorrow to show what I mean, as I'm busy the rest of the night, thanks!!
- 03-10-2012, 12:32 AM #8
- 03-10-2012, 10:03 PM #9
Member
- Join Date
- Mar 2012
- Posts
- 23
- Rep Power
- 0
Re: BorderLayout NORTH small?
hahha thanks, it was a good night out! :P The top part will have 5 jtextboxes with a Jlabel infront of each, followed by two buttons (in flow layout style). The 2nd part should just be blank. I have no idea about what half of your code there does, surely there is a more simple way of just making the north bit bigger?
-
Re: BorderLayout NORTH small?
First of all, don't use FlowLayout, next don't set sizes. Next nest JPanels with simple layouts to achieve your goal. Set the preferredSize of the large center JPanel if you must and pack the GUI to let the JFrame find its most natural size. And read, read, and re-read the layout tutorial.
Similar Threads
-
Problem getting karel to turn north? he turns back east?
By jmorgangrice in forum New To JavaReplies: 6Last Post: 07-24-2011, 08:18 PM -
Senior Java Developer - North Yorkshire - UK
By thecitysecret in forum Jobs OfferedReplies: 0Last Post: 03-17-2011, 01:45 PM -
need help with Java coding for North West Corner algorithm
By ytcheng in forum New To JavaReplies: 1Last Post: 03-15-2010, 10:30 AM -
Developer III - JAVA; North Texas area
By ScottSearch in forum Jobs OfferedReplies: 0Last Post: 09-22-2008, 09:06 PM -
Java/Application Developers Needed - North Carolina
By brown2sl in forum Jobs OfferedReplies: 0Last Post: 03-10-2008, 07:17 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote


Bookmarks