Solved

JDesktopPane - cant see added components

Posted on 2006-07-13
12
692 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__
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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 …
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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

752 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