• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 932
  • Last Modified:

Alternative to using jpanels?

Hello,

I wrote this panel to display a column of buttons, and to make them all the same width.

I tried adding the panel to a frame using the borderlayout.CENTER method. But as I expected this stretches the buttons to fill the frame.

I would like to keep the buttons as a column all the same width, but wondered if to avoid the buttons becomming stretched, if there was any alternative to the jpanel? And I hoped to stop the buttons from stretching when the frame is maximised.

Thank you

      JPanel panel = new JPanel(new GridLayout(0,1));
      panel.add(JButton button1 = new JButton("Ten"));
      button1.addActionListener(this);
      panel.add(JButton button2 = new JButton("Eleven"));
      button2.addActionListener(this);
      panel.add(JButton button3l =  new JButton("Twelve"));
      button3.addActionListener(this);
      panel.add(JButton button4 = new JButton("Thirteen"));
      button4.addActionListener(this);
      panel.add(JButton button5 = new JButton("Fourteen"));
      button5.addActionListener(this);
0
AntoniRyszard
Asked:
AntoniRyszard
  • 16
  • 10
  • 2
  • +1
1 Solution
 
AntoniRyszardAuthor Commented:
Thanks

Would I replace the Gridlayout with boxlayout? And if I used the boxlayout, will all the buttons have the same width as they do currently?

      JPanel panel = new JPanel(new GridLayout(0,1));
      panel.add(JButton button1 = new JButton("Ten"));
      button1.addActionListener(this);
      panel.add(JButton button2 = new JButton("Eleven"));
      button2.addActionListener(this);
      panel.add(JButton button3l =  new JButton("Twelve"));
      button3.addActionListener(this);
      panel.add(JButton button4 = new JButton("Thirteen"));
      button4.addActionListener(this);
      panel.add(JButton button5 = new JButton("Fourteen"));
      button5.addActionListener(this);
0
 
sciuriwareCommented:
No your buttons get the same width by specifying:

      buttonY.setPreferredSize(....................);
sometimes together with:
      buttonY.setMaximumSize(....................);

;JOOP!
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
sciuriwareCommented:
In general the layout manager does not directly dictate the component's size.
;JOOP!
0
 
AntoniRyszardAuthor Commented:
Using the jpanel makes the buttons all the same width.

But I was trying avoid when I use BorderLayout.CENTER, not the panel from filling the screen.

(CEHJ) recommended the boxlayout: http://java.sun.com/docs/books/tutorial/uiswing/layout/box.html

0
 
AntoniRyszardAuthor Commented:
Could I use the BoxLayout?
0
 
zzynxSoftware engineerCommented:
Sorry sciuri, but that setPreferredSize is really not needed.

Antoni,

the code you have is perfectly OK. You just have to place "panel" in another JPanel (panel2) having the default FlowLayout.
Then place panel2 in the panel you have now with BorderLayout.CENTER
0
 
zzynxSoftware engineerCommented:
So,

   1) The first panel having the BorderLayout contains in the CENTER
   2) a second JPanel having the (default) FlowLayout. This pabel contains
   3) the JPanel "panel" from your posted code having the GridLayout(0,1)
0
 
AntoniRyszardAuthor Commented:
Rather than uisng two panels, do you think this could work?

      JPanel panel = new JPanel(new BoxLayout(panel, BoxLayout.Y_AXIS));
      panel.add(JButton button1 = new JButton("Ten"));
      button1.addActionListener(this);
      panel.add(JButton button2 = new JButton("Eleven"));
      button2.addActionListener(this);
      panel.add(JButton button3l =  new JButton("Twelve"));
      button3.addActionListener(this);
      panel.add(JButton button4 = new JButton("Thirteen"));
      button4.addActionListener(this);
      panel.add(JButton button5 = new JButton("Fourteen"));
      button5.addActionListener(this);
0
 
CEHJCommented:
You can do this sort of thing:

import javax.swing.*;
import java.awt.*;


public class F extends JFrame {

      private void setGui() {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            getContentPane().add(new BoxPanel(), BorderLayout.CENTER);
      }

class BoxPanel extends JPanel {

      public BoxPanel() {
            setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
            JButton[] buttons = new JButton[5];
            for(int i = 0;i < buttons.length;i++) {
                  buttons[i] = new JButton("Button " + i);
                  buttons[i].setAlignmentX(Component.CENTER_ALIGNMENT);
                  add(buttons[i]);
            }
      }

}


      public static void main(String[] args) throws Exception {
            F f = new F();
            f.setGui();
            f.pack();
            f.setVisible(true);
      }

}
0
 
zzynxSoftware engineerCommented:
>> Rather than uisng two panels
What's the problem with that?

>>do you think this could work?
I don't think all your buttons will have the same width then


Remark that this

      panel.add(JButton button2 = new JButton("Eleven"));
      button2.addActionListener(this);

doesn't compile. It should be

      JButton button2 = new JButton("Eleven");
      panel.add(button2);
      button2.addActionListener(this);
0
 
zzynxSoftware engineerCommented:
>> I don't think all your buttons will have the same width then
Then you need to use setPreferredSize() on all your buttons.
But, hey that's exactly what GridLayout is made for...
0
 
AntoniRyszardAuthor Commented:
Hello,

I changed the code a little, and found the width of each button was different.

import javax.swing.*;
import java.awt.*;

public class F extends JFrame{

     JButton button1, button2, button3, button4, button5;  

     private void setGui() {
          JPanel panel = new JPanel();
          panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
          panel.add(button1 = new JButton("Ten"));
          panel.add(button2 = new JButton("Eleven"));
          panel.add(button3 =  new JButton("Twelve"));
          panel.add(button4 = new JButton("Thirteen"));
          panel.add(button5 = new JButton("Fourteen"));

          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          getContentPane().add(panel, BorderLayout.CENTER);
     }

     public static void main(String[] args) throws Exception {
          F f = new F();
          f.setGui();
          f.pack();
          f.setVisible(true);
     }

}
0
 
zzynxSoftware engineerCommented:
>> and found the width of each button was different.
Told you
0
 
zzynxSoftware engineerCommented:
Replace
         panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
with
         panel.setLayout(new GridLayout(0,1));
0
 
zzynxSoftware engineerCommented:
Sorry, then the sizing isn't OK.

Try this:

import javax.swing.*;
import java.awt.*;

public class F extends JFrame{

     JButton button1, button2, button3, button4, button5;  

     private void setGui() {
          JPanel panel = new JPanel();
          panel.setLayout(new GridLayout(0,1));
          panel.add(button1 = new JButton("Ten"));
          panel.add(button2 = new JButton("Eleven"));
          panel.add(button3 =  new JButton("Twelve"));
          panel.add(button4 = new JButton("Thirteen"));
          panel.add(button5 = new JButton("Fourteen"));

          JPanel panel2 = new JPanel();
          panel2.add(panel);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          getContentPane().add(panel2, BorderLayout.CENTER);
     }

     public static void main(String[] args) throws Exception {
          F f = new F();
          f.setGui();
          f.pack();
          f.setVisible(true);
     }

}
0
 
AntoniRyszardAuthor Commented:
Yes I can see using 2 panels corrects this.

I was just concerned about from 2 panels each time, rather than 1.

Its a shame I could not use the BoxLayout, I thought this was sun's solution to use 1 panel.

Would you recommand here again to use two panels, to stop textfield and buttons from stretching when the frame is maximised?

JPanel secPanel = new JPanel(new GridLayout(0,2,15,10));
secPanel.add(new JLabel("Some Text"));
secPanel.add(searchTf = new JTextField(20));
secPanel.add(searchBt = new JButton("Search"));
searchBt.addActionListener(this);

Thanks
0
 
zzynxSoftware engineerCommented:
Ho, ho. Wait a minute!

If you don't want that stretching, why do you use the BorderLayout?

This works:

import javax.swing.*;
import java.awt.*;

public class F extends JFrame{

     JButton button1, button2, button3, button4, button5;  

     private void setGui() {
             JPanel secPanel = new JPanel(new GridLayout(0,2,15,10));
             secPanel.add(new JLabel("Some Text"));
             secPanel.add(new JTextField(20));
             secPanel.add(new JButton("Search"));
         
             setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             getContentPane().setLayout(new FlowLayout());              // <<<<<<<< set FlowLayout !!! (instead of the default BorderLayout for JFrame's)
             getContentPane().add(secPanel);
     }

     public static void main(String[] args) throws Exception {
          F f = new F();
          f.setGui();
          f.pack();
          f.setVisible(true);
     }

}
0
 
zzynxSoftware engineerCommented:
And the one panel solution for the previous is:

import javax.swing.*;
import java.awt.*;

public class F extends JFrame{

     JButton button1, button2, button3, button4, button5;  

     private void setGui() {
          JPanel panel = new JPanel();
          panel.setLayout(new GridLayout(0,1));
          panel.add(button1 = new JButton("Ten"));
          panel.add(button2 = new JButton("Eleven"));
          panel.add(button3 =  new JButton("Twelve"));
          panel.add(button4 = new JButton("Thirteen"));
          panel.add(button5 = new JButton("Fourteen"));

          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          getContentPane().setLayout(new FlowLayout());
          getContentPane().add(panel);
     }

     public static void main(String[] args) throws Exception {
          F f = new F();
          f.setGui();
          f.pack();
          f.setVisible(true);
     }
}

Important to remember:
- the default layout of a JPanel is FlowLayout(FlowLayout.CENTER)
- the default layout of a JFrame's content pane is BorderLayout
0
 
zzynxSoftware engineerCommented:
And if you really want to use the BoxLayout (which is somewhat "unnatural" if you want the buttons to have the same size)
apparently you have to set the maximum size:


import javax.swing.*;
import java.awt.*;

public class F extends JFrame{

     JButton button1, button2, button3, button4, button5;  

     private void setGui() {
          JPanel panel = new JPanel();
          panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
          panel.add(button1 = new JButton("Ten"));
          panel.add(button2 = new JButton("Eleven"));
          panel.add(button3 =  new JButton("Twelve"));
          panel.add(button4 = new JButton("Thirteen"));
          panel.add(button5 = new JButton("Fourteen"));
          Dimension d = new Dimension(100, 25);
          button1.setMaximumSize(d);
          button2.setMaximumSize(d);
          button3.setMaximumSize(d);
          button4.setMaximumSize(d);
          button5.setMaximumSize(d);

          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          getContentPane().setLayout(new FlowLayout());
          getContentPane().add(panel);
     }

     public static void main(String[] args) throws Exception {
          F f = new F();
          f.setGui();
          f.pack();
          f.setVisible(true);
     }

}
0
 
zzynxSoftware engineerCommented:
So, I think you have enough alternatives to choose from
 ;°)
0
 
AntoniRyszardAuthor Commented:
Thanks

I will look through all the examples, I feel happy using the 2 panels to create a constant width in the column of buttons.

I will just need to look over the other code.

I will reply tomorrow, its 10.30pm here:)
0
 
zzynxSoftware engineerCommented:
>> I will reply tomorrow, its 10.30pm here:)
OK.

(Here it's midnight ;°)
0
 
zzynxSoftware engineerCommented:
Hi Antoni,
What about this one?
0
 
AntoniRyszardAuthor Commented:
Hello,

I tried this code, which displays the column of buttons with even widths, which is great.

The problem is with this secPanel, I was hoping to display the column of buttons of the left and this panel on the right. With these two next to each other. And when the frame is maximised the buttons, textfields and labels do not stretch.

I wondered if I should be using the Box manager Box.createHorizontalBox(); to display the button panel on the left and secPanel on the right?

Thanks                

JPanel secPanel = new JPanel(new GridLayout(0,2,15,10));
secPanel.add(new JLabel("Some Text"));
secPanel.add(new JTextField(15));
secPanel.add(new JButton("Search"));

import com.sun.java.swing.*;
import java.awt.*;

public class F extends JFrame{

     JButton button1, button2, button3, button4, button5;  

     private void setGui() {
          JPanel panel = new JPanel();
          panel.setLayout(new GridLayout(0,1));
          panel.add(button1 = new JButton("Ten"));
          panel.add(button2 = new JButton("Eleven"));
          panel.add(button3 =  new JButton("Twelve"));
          panel.add(button4 = new JButton("Thirteen"));
          panel.add(button5 = new JButton("Fourteen"));
         
          getContentPane().setLayout(new FlowLayout());
          getContentPane().add(panel);
     }

     public static void main(String[] args) throws Exception {
          F f = new F();
          f.setGui();
          f.pack();
          f.setVisible(true);
     }
}
0
 
zzynxSoftware engineerCommented:
>> I was hoping to display the column of buttons of the left and this panel on the right.
>> With these two next to each other.
>> And when the frame is maximised the buttons, textfields and labels do not stretch.
And what happens with the extra available space? It just expands to the right of both?

Like this?

     private void setGui() {
          JPanel panel = new JPanel();
          panel.setLayout(new GridLayout(0,1));
          panel.add(button1 = new JButton("Ten"));
          panel.add(button2 = new JButton("Eleven"));
          panel.add(button3 =  new JButton("Twelve"));
          panel.add(button4 = new JButton("Thirteen"));
          panel.add(button5 = new JButton("Fourteen"));
         
          JPanel secPanel = new JPanel(new GridLayout(0,2,15,10));
          secPanel.add(new JLabel("Some Text"));
          secPanel.add(new JTextField(15));
          secPanel.add(new JButton("Search"));          

          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
          getContentPane().add(panel);
          getContentPane().add(secPanel);
     }
0
 
AntoniRyszardAuthor Commented:
Thanks again,

To display a JTextArea under the two panels, and a medium sizeed label above the panels could I also use the flowLayout?

Or would your recommend using the gridbaglayout?

0
 
zzynxSoftware engineerCommented:
Don't think so.
What layout you use depends on the behaviour you want when resizing.
What do you want the text area to do? Fill all the available space under the two panels?
What do you mean by a "medium sized label"?
0
 
AntoniRyszardAuthor Commented:
Hello,

The jtextarea is declared as JTextArea text = new JTextArea(2,30);

At the moment I have not added the textarea to a jpanel, I thought this was the correct approach.

I was hoping the textarea would be displayed under the two panels at its defined size, even when the frame is maximised.

And display above the two jpanels a label, about the size of the textarea, so not that big.

I have used the gridbaylayout before, I found it quite difficult to use and wondered if the panels, textarea would stretch when the frame is maximised?

Thanks
0
 
zzynxSoftware engineerCommented:
>> I have used the gridbaylayout before, I found it quite difficult to use
Once you got it, it isn't. Imo the clue is: just consider it as a matrix.
>> and wondered if the panels, textarea would stretch when the frame is maximised?
No. In a GBL all components take their preferred sizes.

I feel like reading http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html would be a good thing for you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 16
  • 10
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now