Thread: JdesktopPane

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    23
    Rep Power
    0

    JdesktopPane


    I have a situation where i would like to have an internal frame inside of a jdesktoppane. Currently that works but i would like to have buttons and labels and other components showing on the jdesktoppane (not related to the internal frame). So far the buttons that i made don't show on the the desktoppane. Please help me figure out why it is not displaying. Here's the code...
    Code:
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    
    public class InternalFrame extends JFrame
    {
    	JDesktopPane desktop;
    	private JPanel background;
    
    	public InternalFrame()
    	{
    		super("Internal Frames");
    		this.setExtendedState(Frame.MAXIMIZED_BOTH);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		//this.setLayout(null);
    
    		background = new JPanel();
    		background.setLocation(0,0);
    		background.setLayout(new FlowLayout());
    
    
    	//	setLayout(new FlowLayout());
    
    		JButton button1 = new JButton("Button 1");
    		JButton button2 = new JButton("Button 2");
    		JButton button3 = new JButton("Button 3");
    
    		//button1.setBounds(10,200,200,200);
    
    		background.add(button1);
    		add(button2);
    		add(button3);
    
    		setVisible(true);
    
    		desktop = new JDesktopPane();
    		createFrame();
    		setContentPane(desktop);
    		desktop.add(background);
    	}
    
    
    
    
    	protected void createFrame()
    	{
    		MyInternalFrame frame = new MyInternalFrame();
    		MyInternalFrame frame2 = new MyInternalFrame();
    		frame.setVisible(true);
    		frame2.setVisible(true);
    		desktop.add(frame);
    		desktop.add(frame2);
    	/*	try
    		{
    			frame.setSelected(true);
    		}catch (java.beans.PropertyVetoException e) {} */
    	}
    
    
    		protected void quit()
    		{
    			System.exit(0);
    		}
    
    		private static void createAndShowGUI()
    		{
    			JFrame.setDefaultLookAndFeelDecorated(true);
    
    			InternalFrame frame = new InternalFrame();
    			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    			frame.setVisible(true);
    
    			JFrame.setDefaultLookAndFeelDecorated(true);
    
    			InternalFrame frame2 = new InternalFrame();
    			frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    			frame2.setVisible(true);
    		}
    		public static void main(String[] args)
    		{
    			javax.swing.SwingUtilities.invokeLater(new Runnable()
    			{
    
    			public void run()
    			{
    				createAndShowGUI();
    			}
    		});
    		}
    	}
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    23
    Rep Power
    0
    Problem solved the buttons wasn't displaying because i wasn't adding the buttons to the desktop. Here's a copy of the working code, (PS, i know the code format is sloppy and may be confusing but i'm currently in the process of figuring out which layout manager i want to use.)
    Code:
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    
    public class InternalFrame extends JFrame
    {
    	JDesktopPane desktop;
    	private JPanel background;
    
    	public InternalFrame()
    	{
    		super("Internal Frames");
    		this.setExtendedState(Frame.MAXIMIZED_BOTH);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		//this.setLayout(null);
    
    		//background = new JPanel();
    		//background.setLocation(300,300);
    		//background.setLayout(new FlowLayout());
    
    
    	//	setLayout(new FlowLayout());
    
    		JButton button1 = new JButton("Button 1");
    		JButton button2 = new JButton("Button 2");
    		JButton button3 = new JButton("Button 3");
    
    
    		//background.add(button1);
    		//add(button2);
    		//add(button3);
    
    
    
    		setLayout(null);
    
    		/* JPanel panel1 = new JPanel();
    		JPanel panel2 = new JPanel();
    		JPanel panel3 = new JPanel();
    
    		panel1.add(button1);
    		panel2.add(button2);
    		panel3.add(button3);
    
    		add(panel1, BorderLayout.NORTH);
    		add(panel2, BorderLayout.SOUTH);
    		add(panel3, BorderLayout.CENTER); */
    
    	/*	add(button1, BorderLayout.NORTH);
    		add(button2, BorderLayout.SOUTH);
    		add(button3, BorderLayout.EAST); */
    		pack();
    
    		setVisible(true);
    
    		desktop = new JDesktopPane();
    		createFrame();
    		setContentPane(desktop);
    		//desktop.add(background);
    		desktop.add(button1);
    		desktop.add(button2);
    
    		button1.setBounds(100,100,200,200);
    	}
    
    
    
    
    	protected void createFrame()
    	{
    		MyInternalFrame frame = new MyInternalFrame();
    		MyInternalFrame frame2 = new MyInternalFrame();
    		frame.setVisible(true);
    		frame2.setVisible(true);
    		desktop.add(frame);
    		desktop.add(frame2);
    	/*	try
    		{
    			frame.setSelected(true);
    		}catch (java.beans.PropertyVetoException e) {} */
    	}
    
    
    		protected void quit()
    		{
    			System.exit(0);
    		}
    
    		private static void createAndShowGUI()
    		{
    			JFrame.setDefaultLookAndFeelDecorated(true);
    
    			InternalFrame frame = new InternalFrame();
    			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    			frame.setVisible(true);
    
    			JFrame.setDefaultLookAndFeelDecorated(true);
    
    			InternalFrame frame2 = new InternalFrame();
    			frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    			frame2.setVisible(true);
    		}
    		public static void main(String[] args)
    		{
    			javax.swing.SwingUtilities.invokeLater(new Runnable()
    			{
    
    			public void run()
    			{
    				createAndShowGUI();
    			}
    		});
    		}
    	}

IMN logo majestic logo threadwatch logo seochat tools logo