Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

JDesktopPane - cant see added components

Posted on 2006-07-13
12
Medium Priority
?
712 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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
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 400 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 1600 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

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 Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.
Suggested Courses

670 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