Solved

Alternative to using jpanels?

Posted on 2004-09-30
31
730 Views
Last Modified: 2010-03-31
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
Comment
Question by:AntoniRyszard
  • 16
  • 10
  • 2
  • +1
31 Comments
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
0
 

Author Comment

by:AntoniRyszard
Comment Utility
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
 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
No your buttons get the same width by specifying:

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

;JOOP!
0
 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
In general the layout manager does not directly dictate the component's size.
;JOOP!
0
 

Author Comment

by:AntoniRyszard
Comment Utility
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
 

Author Comment

by:AntoniRyszard
Comment Utility
Could I use the BoxLayout?
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
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
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
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
 

Author Comment

by:AntoniRyszard
Comment Utility
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
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
>> 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
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
>> 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
 

Author Comment

by:AntoniRyszard
Comment Utility
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
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
>> and found the width of each button was different.
Told you
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
Replace
         panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
with
         panel.setLayout(new GridLayout(0,1));
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 37

Expert Comment

by:zzynx
Comment Utility
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
 

Author Comment

by:AntoniRyszard
Comment Utility
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
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
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
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
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
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
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
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
So, I think you have enough alternatives to choose from
 ;°)
0
 

Author Comment

by:AntoniRyszard
Comment Utility
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
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
>> I will reply tomorrow, its 10.30pm here:)
OK.

(Here it's midnight ;°)
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
Hi Antoni,
What about this one?
0
 

Author Comment

by:AntoniRyszard
Comment Utility
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
 
LVL 37

Accepted Solution

by:
zzynx earned 80 total points
Comment Utility
>> 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
 

Author Comment

by:AntoniRyszard
Comment Utility
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
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
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
 

Author Comment

by:AntoniRyszard
Comment Utility
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
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
>> 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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
mapAB Challlenge 35 84
HashMap Vs TreeMap 12 47
Systems talking to each other 5 107
JAVA part two 5 39
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

771 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

12 Experts available now in Live!

Get 1:1 Help Now