playing with swing

Incinerator

Limp Gawd
Joined
Aug 31, 2005
Messages
252
I started playing with swing today and I have a small problem. I have a JFrame with an internal frame and I want to be able to close that which I can, but then I want to go to the menu bar and choose File->New which should open up a new internal frame. But when I click on New it makes the new internal frame but its invisible, you have to click on where you know it will show up and then it will be visible. Anyone know the problem?

Here's the code
Code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.*;

public class DesktopContainer implements ActionListener, ItemListener {
	
	JFrame frame;
	JPanel panel;
	JInternalFrame iframe;
	JButton button1, button2;
	JDesktopPane desk;

	public DesktopContainer()
	{
		
		//menuItem.addActionListener(this);
		frame = new JFrame("Creating a JDesktopPane Container");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JMenuBar menubar = new JMenuBar();
		JMenu filemenu = new JMenu("File");
		filemenu.add(new JSeparator());
		JMenu editmenu = new JMenu("Edit");
		editmenu.add(new JSeparator());
		
		JMenuItem fileItem1 = new JMenuItem("New");
		fileItem1.addActionListener(this);
		JMenuItem fileItem2 = new JMenuItem("Open");
		JMenuItem fileItem3 = new JMenuItem("Close");
		fileItem3.add(new JSeparator());
		JMenuItem fileItem4 = new JMenuItem("Save");
		JMenuItem editItem1 = new JMenuItem("Cut");
		JMenuItem editItem2 = new JMenuItem("Copy");
		editItem2.add(new JSeparator());
		JMenuItem editItem3 = new JMenuItem("Paste");
		JMenuItem editItem4 = new JMenuItem("Insert");
		
		filemenu.add(fileItem1);
		filemenu.add(fileItem2);
		filemenu.add(fileItem3);
		filemenu.add(fileItem4);
		editmenu.add(editItem1);
		editmenu.add(editItem2);
		editmenu.add(editItem3);
		editmenu.add(editItem4);
		menubar.add(filemenu);
		menubar.add(editmenu);
		
		frame.setJMenuBar(menubar);
		frame.setSize(400,400);
		frame.setVisible(true);
		
		
	    
		iframe = new JInternalFrame("Internal frame", true,true,true,true);
		iframe.setToolTipText("This is internal frame");
		panel = new JPanel();
		button1 = new JButton("Ok");
		button1.setToolTipText("This is Ok button of internal frame");
		panel.add(button1);
		button2 = new JButton("Cancel");
		button2.setToolTipText("This is cancel button of internal frame");
		panel.add(button2);
		iframe.add(panel);
		iframe.setSize(250,300);
		iframe.setVisible(true);
		desk = new JDesktopPane();
		desk.add(iframe);
		frame.add(desk);
		frame.setSize(400,400);
		frame.setVisible(true);
  }
	
	public void addiFrame()
	{
		iframe = new JInternalFrame("Internal frame2", true,true,true,true);
		iframe.setToolTipText("This is internal frame");
		panel = new JPanel();
		button1 = new JButton("Ok");
		button1.setToolTipText("This is Ok button of internal frame2");
		panel.add(button1);
		button2 = new JButton("Cancel");
		button2.setToolTipText("This is cancel button of internal frame2");
		panel.add(button2);
		iframe.add(panel);
		iframe.setSize(250,300);
		iframe.setVisible(true);
		desk = new JDesktopPane();
		desk.add(iframe);
		frame.add(desk);
		frame.setSize(400,400);
		frame.setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {
		addiFrame();
	}

	public void itemStateChanged(ItemEvent e) {
		// TODO Auto-generated method stub
		
	}
	
	public static void main(String[] args) 
	{
		new DesktopContainer();
	}
}
 
You are creating a new JDesktopPane for some reason, when i dont think you should be. Adding the iframe to the current JDesktopPane cures the internal frame not showing up. Now it just shows up behind everything, adding a setSelected to the iframe fixes that. It throws some exception which you can probably ignore. And i dont know what you are trying to do with the frame afterwords so i got rid of that too.

Code:
	public void addiFrame()
	{
	...SNIP...
		iframe.setVisible(true);
		//desk = new JDesktopPane();
		desk.add(iframe);
		try {
			iframe.setSelected(true);
		} catch (PropertyVetoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//frame.add(desk);
		//frame.setSize(400,400);
		//frame.setVisible(true);
	}
 
Back
Top