LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-10-2006, 07:33 AM   #1
kpachopoulos
Member
 
Registered: Feb 2004
Location: Athens, Greece
Distribution: Gentoo,FreeBSD, Debian
Posts: 705

Rep: Reputation: 30
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
 
Old 03-10-2006, 07:57 AM   #2
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 64
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();

    }

}

Last edited by Mega Man X; 03-11-2006 at 08:09 AM.
 
Old 03-10-2006, 07:59 AM   #3
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 64
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

Last edited by Mega Man X; 03-10-2006 at 08:23 AM.
 
Old 03-11-2006, 05:00 AM   #4
kpachopoulos
Member
 
Registered: Feb 2004
Location: Athens, Greece
Distribution: Gentoo,FreeBSD, Debian
Posts: 705

Original Poster
Rep: Reputation: 30
Thanks a lot,
i still need work to do, but i think i am on the right track
 
Old 03-11-2006, 08:08 AM   #5
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 64
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!

Last edited by Mega Man X; 03-11-2006 at 08:11 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
newbie java swing question... window size true_atlantis Programming 1 10-07-2004 10:17 AM
[JAVA] JPanel over JPanel ... not being displayed properly linux_ub Programming 0 08-13-2004 11:21 AM
how to set mozilla default font size mfeat Linux - Software 2 05-28-2004 09:55 AM
setting default page size and default tray lived4eva Linux - General 1 11-04-2003 02:39 PM
Osama swing set available! DavidPhillips General 6 04-20-2003 03:03 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration