Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: BorderLayout NORTH small?

  1. #1
    Join Date
    Mar 2012
    Posts
    23
    Rep Power
    0

    Default 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:
    BorderLayout NORTH small?-borderlayouteg.png

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default 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.

  3. #3
    Join Date
    Mar 2012
    Posts
    23
    Rep Power
    0

    Default 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.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: BorderLayout NORTH small?

    There's one of your problems:
    Java Code:
    setSize(550,600);
    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.

  5. #5
    Join Date
    Mar 2012
    Posts
    23
    Rep Power
    0

    Default 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.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default 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();
             }
          });
       }
    }

  7. #7
    Join Date
    Mar 2012
    Posts
    23
    Rep Power
    0

    Default 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!!

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: BorderLayout NORTH small?

    Quote Originally Posted by burgergetsbored View Post
    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!!
    Enjoy the party

    db
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    Join Date
    Mar 2012
    Posts
    23
    Rep Power
    0

    Default 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?

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default 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

  1. Problem getting karel to turn north? he turns back east?
    By jmorgangrice in forum New To Java
    Replies: 6
    Last Post: 07-24-2011, 08:18 PM
  2. Senior Java Developer - North Yorkshire - UK
    By thecitysecret in forum Jobs Offered
    Replies: 0
    Last Post: 03-17-2011, 01:45 PM
  3. Replies: 1
    Last Post: 03-15-2010, 10:30 AM
  4. Developer III - JAVA; North Texas area
    By ScottSearch in forum Jobs Offered
    Replies: 0
    Last Post: 09-22-2008, 09:06 PM
  5. Java/Application Developers Needed - North Carolina
    By brown2sl in forum Jobs Offered
    Replies: 0
    Last Post: 03-10-2008, 07: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
  •