Solved

JDesktopPane - cant see added components

Posted on 2006-07-13
12
643 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 24

Expert Comment

by:Tomas Helgi Johannsson
Comment Utility
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 24

Expert Comment

by:Tomas Helgi Johannsson
Comment Utility
Try add a layout.

Regards,
  Tomas Helgi
0
 
LVL 8

Author Comment

by:colr__
Comment Utility
Cheers, Ive had a look but I dont see any difference fom what Ive got above.
0
 
LVL 8

Author Comment

by:colr__
Comment Utility
Still the same problem.
0
 
LVL 24

Expert Comment

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

Regards,
  Tomas Helgi
0
 
LVL 8

Author Comment

by:colr__
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 24

Assisted Solution

by:Tomas Helgi Johannsson
Tomas Helgi Johannsson earned 100 total points
Comment Utility
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
Comment Utility
> 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
Comment Utility
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__
Comment Utility
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
Comment Utility
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
Comment Utility
:)
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
copyEndy  challenge 15 55
triangle challenge 4 75
countPairs challenge 7 57
count11 challenge 6 47
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

744 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

17 Experts available now in Live!

Get 1:1 Help Now