Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / July 2005

Tip: Looking for answers? Try searching our database.

MyPanel extends JPanel is not displayed

Thread view: 
pete - 19 Jul 2005 12:01 GMT
Hello,

I want to write a buttonbar which inherits from JPanel.
Unfortunately my panel is not displayed.

This is how I try:

[code]

public class WidgetPanel extends JPanel{

JPanel widget = null;
JButton back = null;
JButton forward = null;
JButton stop = null;
JTextField url = null;
JButton go  = null;

private static final long serialVersionUID = 12342113;

public WidgetPanel(){

 widget = new JPanel(new GridLayout(2, 10));
 widget.setBackground(Color.blue);
 this.installFields();
}

private void installFields(){

 back = new JButton("<- BACK");
 widget.add(back);

 forward = new JButton("FORWARD ->");
 widget.add(forward);

 stop = new JButton("STOP");
 widget.add(stop);

 url = new JTextField();
 widget.add(url);

 go = new JButton("GO!");
 widget.add(go);
}
}

public class TestMyPanel {

public static void main(String[] args) {

 JFrame.setDefaultLookAndFeelDecorated(true);

 JFrame temp = new JFrame();
 WidgetPanel mypanel = new WidgetPanel();

 temp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 temp.getContentPane().add(mypanel, BorderLayout.CENTER);

 temp.pack();
 temp.setSize(new Dimension(640, 100));
 temp.setVisible(true);
}
}
}

[/code]

What is the problem?

Thanks!
pete
Andrew Thompson - 19 Jul 2005 13:01 GMT
> I want to write a buttonbar which inherits from JPanel.
> Unfortunately my panel is not displayed.
>
> This is how I try:
..
Pretty good example, but there are a few things you
can do with the code to make it even easier to help you,
see the altered source for further comments.  See also [1].

> What is the problem?

The problem is alluded to in my comments, see if
they make something 'click' for you.

<sscce>
// adding generic imports for classes assists
// people to see your GUI quickly
import java.awt.*;
import javax.swing.*;

public class WidgetPanel extends JPanel{

// Why are you keeping this reference to a JPanel?
// WidgetPanel *is* a JPanel!
// JPanel widget = null;

JButton back = null;
JButton forward = null;
JButton stop = null;
JTextField url = null;
JButton go  = null;

private static final long serialVersionUID = 12342113;

public WidgetPanel(){

//  widget = new JPanel(new GridLayout(2, 10));
 // just call 'super'
 super(new GridLayout(2, 10));
 setBackground(Color.blue);
 this.installFields();
}

private void installFields(){

 back = new JButton("<- BACK");
 // now add the components directly to the panel.
 add(back);

 forward = new JButton("FORWARD ->");
 add(forward);

 stop = new JButton("STOP");
 add(stop);

 url = new JTextField();
 add(url);

 go = new JButton("GO!");
 add(go);
}

// by adding the 'main' to WidgetPanel, the class and it's
// test are one and the same - bery portable.
public static void main(String[] args) {

 JFrame.setDefaultLookAndFeelDecorated(true);

 JFrame temp = new JFrame();
 WidgetPanel mypanel = new WidgetPanel();

 temp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 temp.getContentPane().add(mypanel, BorderLayout.CENTER);

 temp.pack();
 // most well behaved GUI's do not need to be
 // sized, it is better to let the GUI find its
 // own size
 //temp.setSize(new Dimension(640, 100));
 temp.setVisible(true);
}
}
</sscce>

[1] Being such a fan of the SSCCE, I wrote this
document on them for further tips.
<http://www.physci.org/codes/sscce.jsp>

HTH

Signature

Andrew Thompson
physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
Controlling You Through A Chip In Your Butt Since 1999

Thomas Weidenfeller - 19 Jul 2005 13:56 GMT
> Hello,
>
> I want to write a buttonbar which inherits from JPanel.
> Unfortunately my panel is not displayed.

Funny, just yesterday someone came with some code which had almost the
same architectural problem. Have a look at the thread "Subpnel problem"
[sic].

Andrew already pointed out the main issues, I just have a few things:

Why do you do your own bar? Why not just use a JToolbar? They are made
for this.

>  private static final long serialVersionUID = 12342113;

Do you really plan to serialize this? Serialization of GUI (Swing)
components is usually not worth the effort and anyhow not guaranteed to
be compatible between Java releases.

>  public WidgetPanel(){
>
>   widget = new JPanel(new GridLayout(2, 10));

See Andrew's remarks.

>   back = new JButton("<- BACK");
>   widget.add(back);
>
>   forward = new JButton("FORWARD ->");
>   widget.add(forward);

You might want to consider using icons for the arrows, instead of the
"ASCII art". You can mix an icon and text on a button, so that should
work nicely. If you need icons, the FAQ points to a number of icon
collections.

You might also want to consider to not use all caps (uppercase). No need
to shout at your users.

>   stop = new JButton("STOP");
>   widget.add(stop);
[quoted text clipped - 19 lines]
>
>   temp.getContentPane().add(mypanel, BorderLayout.CENTER);

Sun has changed the recommended way to start a GUI. It is no longer
advisable to do it from the main thread. Instead you should do it from
the EDT. See Q4.3 of the FAQ for details.

>   temp.pack();
>   temp.setSize(new Dimension(640, 100));

pack() and then setting a fixed size don't work to well together. With
pack you tell layout management to calculate all the sizes and
positions. The setSize() works against this.

/Thomas

Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/

pete - 19 Jul 2005 14:15 GMT
Thank you very much!
Now it works...

Sorry for my stupid mistakes, but I am not familiar with Java Swing. It is
my first attempt...

Thanks!
pete


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.