LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   set standard/default size of swing JPanel (https://www.linuxquestions.org/questions/programming-9/set-standard-default-size-of-swing-jpanel-423477/)

kpachopoulos 03-10-2006 07:33 AM

set standard/default size of swing JPanel
 
Hi,
A JPanel grows bigger as more components are added. That means, that by default, when nothing is contained in it, it is not visible. The "setSize" method does nothing unless a component is contained. How can i set a default size, when no component is contained? Is there something like JPanel.setDefaultSizeAndDontShrink()? :)
Thanks

Mega Man X 03-10-2006 07:57 AM

Quote:

Originally Posted by nocturna_gr
Hi,
A JPanel grows bigger as more components are added. That means, that by default, when nothing is contained in it, it is not visible. The "setSize" method does nothing unless a component is contained. How can i set a default size, when no component is contained? Is there something like JPanel.setDefaultSizeAndDontShrink()? :)
Thanks

Could you show us some code?. The idea with panels is to use them as canvas. You then add components (JButton, JTextArea, etc) to that panel and then you add that panel to a container, such as a JFrame or JApplet. It's up to those containers to take care of how the JPanel will be displayed by the Layout Managers you are using, such as FlowLayout, GridLayout and etc. Take a look in this quick, untested program I did for ya:

Code:

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

public class MainClass extends JFrame {

    private JButton btnOK = new JButton("OK");

    private JPanel pane = new JPanel();

    public MainClass() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(new Dimension(800, 600));
        setLayout(new FlowLayout());

               
        pane.setPreferredSize(new Dimension(320, 240));
        //pane.setMinimumSize(new Dimension(320, 240));
        //pane.setMaximumSize(new Dimension(320, 240));
        pane.setBorder(BorderFactory.createTitledBorder("button"));
        pane.add(btnOK);

        getContentPane().add(pane);
        setVisible(true);
    }

    public static void main(String[] args) {

        MainClass test = new MainClass();

    }

}


Mega Man X 03-10-2006 07:59 AM

Explanation: I placed the JPanel on the Container (A JFrame) and added a border (so you can visualize the size of the JPanel on the Container) and used the setPreferredSize(Dimension d) to a given size. You might want to play with the uncommented methods (setMin and MaxSize).

By default, JPanels use the FlowLayout Manager and JFrames use the BorderLayout Manager. I set the JFrame layout to FlowLayout, otherwise your JPanel would be put in the center of the BorderLayout and take all space available instead of using the prefferedSize. Try commenting out the line:

setLayout(new FlowLayout());

and you will see what happens...

Check the API about Layout Managers and ask if you get stuck :)

kpachopoulos 03-11-2006 05:00 AM

Thanks a lot,
i still need work to do, but i think i am on the right track :)

Mega Man X 03-11-2006 08:08 AM

No problems nocturna_gr :). I don't know if you are using an IDE or a simple text editor, but I highly recommend you to use Eclipse. It will help you a lot to find methods that you are not aware of and it definitely saves a lot of time instead of checking the API all the time, for example:

Code:

JFrame frame = new JFrame();
frame.

As soon as you press the dot (.), Eclipse will show a huge list with options for JFrame, such as setSize, setTitle, setAlwaysOnTop, setEnable, action, bounds, addWindowListener and a hell lot more that no book will teach you or that would otherwise take a huge amount of time to find on the javadocs.

Just a thought though ;)

Good luck!


All times are GMT -5. The time now is 01:58 PM.