Solved

JDesktopPane - cant see added components

Posted on 2006-07-13
12
656 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java array passed to SQL where clause 23 62
eclipse formatting 6 72
Windows 10 IE Certificate Issue 10 42
java jdbc batch example 8 22
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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 …

920 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

13 Experts available now in Live!

Get 1:1 Help Now