I want tabbed JInternalFrames? What's the combination

Hello people!

I tried to modify the InternalFrameDemo.java of the java Tutorial, but I get exception when I try to add a JTabbedPane Object to a JDesktopPane Object. I had previously "addTab"ed a JInternalFrame Object to the JTabbedPane Object . Is it not possible after all to have tabbed JInternalFrames?

Thank you in advance!

[347 byte] By [M.M.a] at [2007-10-3 9:15:20]
# 1
Please post all of your exception message text. And create a Short,Self Contained, Compilable and Executable, Example Program or at least the code that the exception is being throw from.
zadoka at 2007-7-15 4:27:58 > top of Java-index,Desktop,Core GUI APIs...
# 2

public class InternalFrameDemo extends JFrame

implements ActionListener {

JDesktopPane desktop;

JTabbedPane graphTab;

public InternalFrameDemo() {

super("InternalFrameDemo");

graphTab = new JTabbedPane(JTabbedPane.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);

graphTab.setBackground(Color.cyan);

//...

}

//Create a new internal frame.

protected void createFrame() {

MyInternalFrame frame = new MyInternalFrame();

frame.setVisible(true); //necessary as of 1.3

//Don't mind the comments. I tried various things

graphTab.addTab("New Graph " + 2/*frame.getOpenFrameCount()*/, null, new JInternalFrame()/*new JScrollPane(frame, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS)*/, "New Graph " + 2/*frame.getOpenFrameCount()*/ + " tab");

desktop.add(graphTab); //Here is where I get the exception. I inserted this...

//desktop.add(frame); //...Instead of this

try {

frame.setSelected(true);

} catch (java.beans.PropertyVetoException e) {}

}

and the exception is

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: illegal component position

at java.awt.Container.addImpl(Unknown Source)

at javax.swing.JLayeredPane.addImpl(Unknown Source)

at java.awt.Container.add(Unknown Source)

at InternalFrameDemo.createFrame(InternalFrameDemo.java:95)

at InternalFrameDemo.actionPerformed(InternalFrameDemo.java:82)

at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)

at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)

at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)

at javax.swing.DefaultButtonModel.setPressed(Unknown Source)

at javax.swing.AbstractButton.doClick(Unknown Source)

at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)

at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)

at java.awt.Component.processMouseEvent(Unknown Source)

at javax.swing.JComponent.processMouseEvent(Unknown Source)

at java.awt.Component.processEvent(Unknown Source)

at java.awt.Container.processEvent(Unknown Source)

at java.awt.Component.dispatchEventImpl(Unknown Source)

at java.awt.Container.dispatchEventImpl(Unknown Source)

at java.awt.Component.dispatchEvent(Unknown Source)

at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)

at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)

at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)

at java.awt.Container.dispatchEventImpl(Unknown Source)

at java.awt.Window.dispatchEventImpl(Unknown Source)

at java.awt.Component.dispatchEvent(Unknown Source)

at java.awt.EventQueue.dispatchEvent(Unknown Source)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.run(Unknown Source)

M.M.a at 2007-7-15 4:27:58 > top of Java-index,Desktop,Core GUI APIs...
# 3
The comments really clutter things up. It makes it hard to tell what you are doing and what you did do?Please post the code that doesn't work. The version that is giving the exception. And remove all the comment that are not needed.
zadoka at 2007-7-15 4:27:58 > top of Java-index,Desktop,Core GUI APIs...
# 4

import javax.swing.JInternalFrame;

import javax.swing.JDesktopPane;

import javax.swing.JMenu;

import javax.swing.JMenuItem;

import javax.swing.JMenuBar;

import javax.swing.JFrame;

import javax.swing.KeyStroke;

import javax.swing.JTabbedPane;

import javax.swing.JScrollPane;

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

/*

* InternalFrameDemo.java requires:

*MyInternalFrame.java

*/

public class InternalFrameDemo extends JFrame

implements ActionListener {

JDesktopPane desktop;

JTabbedPane graphTab;

public InternalFrameDemo() {

super("InternalFrameDemo");

graphTab = new JTabbedPane(JTabbedPane.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);

graphTab.setBackground(Color.cyan);

//Make the big window be indented 50 pixels from each edge

//of the screen.

int inset = 50;

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

setBounds(inset, inset,

screenSize.width - inset*2,

screenSize.height - inset*2);

//Set up the GUI.

desktop = new JDesktopPane(); //a specialized layered pane

createFrame(); //create first "window"

setContentPane(desktop);

setJMenuBar(createMenuBar());

//Make dragging a little faster but perhaps uglier.

desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);

}

protected JMenuBar createMenuBar() {

JMenuBar menuBar = new JMenuBar();

//Set up the lone menu.

JMenu menu = new JMenu("Document");

menu.setMnemonic(KeyEvent.VK_D);

menuBar.add(menu);

//Set up the first menu item.

JMenuItem menuItem = new JMenuItem("New");

menuItem.setMnemonic(KeyEvent.VK_N);

menuItem.setAccelerator(KeyStroke.getKeyStroke(

KeyEvent.VK_N, ActionEvent.ALT_MASK));

menuItem.setActionCommand("new");

menuItem.addActionListener(this);

menu.add(menuItem);

//Set up the second menu item.

menuItem = new JMenuItem("Quit");

menuItem.setMnemonic(KeyEvent.VK_Q);

menuItem.setAccelerator(KeyStroke.getKeyStroke(

KeyEvent.VK_Q, ActionEvent.ALT_MASK));

menuItem.setActionCommand("quit");

menuItem.addActionListener(this);

menu.add(menuItem);

return menuBar;

}

//React to menu selections.

public void actionPerformed(ActionEvent e) {

if ("new".equals(e.getActionCommand())) { //new

createFrame();

} else { //quit

quit();

}

}

//Create a new internal frame.

protected void createFrame() {

MyInternalFrame frame = new MyInternalFrame();

frame.setVisible(true); //necessary as of 1.3

graphTab.addTab("New Graph", null, new JInternalFrame(), "New Graph tab");

desktop.add(graphTab); //<-Exception

try {

frame.setSelected(true);

} catch (java.beans.PropertyVetoException e) {}

}

//Quit the application.

protected void quit() {

System.exit(0);

}

/**

* Create the GUI and show it. For thread safety,

* this method should be invoked from the

* event-dispatching thread.

*/

private static void createAndShowGUI() {

//Make sure we have nice window decorations.

//JFrame.setDefaultLookAndFeelDecorated(true);

//Create and set up the window.

InternalFrameDemo frame = new InternalFrameDemo();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Display the window.

frame.setVisible(true);

}

public static void main(String[] args) {

//Schedule a job for the event-dispatching thread:

//creating and showing this application's GUI.

javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {

createAndShowGUI();

}

});

}

}

Note that this is a modified code from the "How to Use Internal Frames" Java tutorial at

http://java.sun.com/docs/books/tutorial/uiswing/components/internalframe.html

M.M.a at 2007-7-15 4:27:58 > top of Java-index,Desktop,Core GUI APIs...
# 5

Alright, not sure what you are trying to do but your problem is you call the create frame multiple times which executes this code multiple times:

desktop.add(graphTab); //<-Exception

This only needs to be done once. (Otherwise you are adding the same component twice and it causes and exception). Just move this code to the constructor.

zadoka at 2007-7-15 4:27:58 > top of Java-index,Desktop,Core GUI APIs...