Solved

JDesktopPane - cant see added components

Posted on 2006-07-13
12
678 Views
Last Modified: 2007-12-19
Can anyone tell me why I cant see the JButton on the JDesktopPane pane?

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

public class TestFrame extends JFrame{

      JDesktopPane desktop;

      public TestFrame(){
            super();
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(600, 600);
            
            desktop = new JDesktopPane();
            desktop.setBackground(new Color(255, 255, 255));
            this.setContentPane(desktop);
            
            JButton button = new JButton("Open Frame");
            desktop.add(button);
            
            button.setVisible(true);
            setVisible(true);
      }
      
      public static void main(String[] args){
            TestFrame frame = new TestFrame();      
      }

}
0
Comment
Question by:colr__
  • 4
  • 4
  • 2
  • +2
12 Comments
 
LVL 25

Expert Comment

by:Tomas Helgi Johannsson
ID: 17100232
Here you have a JDesktopPane demo from www.java2s.com

http://www.java2s.com/Code/Java/Swing-JFC/JDesktopPanedemo.htm
Hope it helps.

Regards,
  Tomas Helgi
0
 
LVL 25

Expert Comment

by:Tomas Helgi Johannsson
ID: 17100242
Try add a layout.

Regards,
  Tomas Helgi
0
 
LVL 8

Author Comment

by:colr__
ID: 17100308
Cheers, Ive had a look but I dont see any difference fom what Ive got above.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

 
LVL 8

Author Comment

by:colr__
ID: 17100583
Still the same problem.
0
 
LVL 25

Expert Comment

by:Tomas Helgi Johannsson
ID: 17100618
Try to change the line
     this.setContentPane(desktop);
to
    this.getContentPane().add(desktop);

Regards,
  Tomas Helgi
0
 
LVL 8

Author Comment

by:colr__
ID: 17100643
I tried that as well, as well as doing that with the layout added. Ive also noticed that I cant run the exemplae given here:

http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/Swing-Tutorial-JInternalFrame.html

When I run this example, Im getting the same problem - nothing seems to get added to the JDekstopPane.
0
 
LVL 25

Assisted Solution

by:Tomas Helgi Johannsson
Tomas Helgi Johannsson earned 100 total points
ID: 17100990
How about adding a JPanel above the JDesktopPane and a JButton on the JPanel like this

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

public class TestFrame extends JFrame{

     JDesktopPane desktop;

     public TestFrame(){
          super();
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          setSize(600, 600);
         
          desktop = new JDesktopPane();
          desktop.setBackground(new Color(255, 255, 255));
          this.setContentPane(desktop);
          JPanel panel = new JPanel();                        
          JButton button = new JButton("Open Frame");
          panel.add(button);
          getContentPane().setLayout(new BorderLayout());
          getContentPane().add("North", panel);
          getContentPane().add("Center", desktop);
         
          button.setVisible(true);
          setVisible(true);
     }
     
     public static void main(String[] args){
          TestFrame frame = new TestFrame();    
     }

}

Does this work ?

Regards,
  Tomas Helgi
0
 
LVL 35

Accepted Solution

by:
girionis earned 400 total points
ID: 17102483
> Can anyone tell me why I cant see the JButton on the JDesktopPane pane?

JDesktopPane is for adding frames into it, you cannot add a button directly to it. You will need to add the button to another container first (JInternalFrame) and then this frame to jdesktoppane.
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 17102960
try this,

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

public class TestFrame extends JFrame{

    JDesktopPane desktop;

    public TestFrame(){
        super();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(600, 600);

        desktop = new JDesktopPane();
        desktop.setBackground(new Color(255, 255, 255));
        JPanel panel = new JPanel();
        JButton button = new JButton("Open Frame");
        panel.add(button);
        getContentPane().add( desktop );
        getContentPane().add( button, BorderLayout.SOUTH );

        setVisible(true);
    }

    public static void main(String[] args){
        TestFrame frame = new TestFrame();
    }
}
0
 
LVL 8

Author Comment

by:colr__
ID: 17106736
TomasHelgi  - tried adding a JPanel first, but still the same problem.
girionis - I think this probably makes sense.

As it turns out I dont need this functionality anymore Ive found another way of solving my problem.

Thanks for the help guys.
0
 
LVL 8

Expert Comment

by:kaliyugkaarjun
ID: 17106795
girionis  is  right !!!!!

Still if u want try this . U can see a square shaped button

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

public class WassanJFrame extends JFrame{
JButton b;
WessanPanel wp;

public WassanJFrame() {
super();
setDefaultCloseOperation(EXIT_ON_CLOSE);

wp = new WessanPanel();
b = new JButton("button");
wp.add(b);

setContentPane(wp);
setSize(400,400);
setVisible(true);
}

class WessanPanel extends JDesktopPane{

public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillRect(100, 100, 100, 100);
}
}

public static void main(String[] args) {
new WassanJFrame();
}
}

OR here is a complete program with many components

http://www.d.umn.edu/~gshute/cs4531/examples/Demo.java.html 
0
 
LVL 35

Expert Comment

by:girionis
ID: 17107213
:)
0

Featured Post

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SHA2 certs for IIS AND Java? 2 113
hibernate example using maven 12 59
What's wrong with this code? 4 28
Java syntax, or is it Selenium 6 30
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
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 …

830 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