Click to See Complete Forum and Search --> : Tearoff Frame and JTabbedPane


shridhars
March 7th, 2000, 09:45 AM
hi!

Has anyone implemented a tearoff frame? In my application, i have to
remove a tab from a JTabbedPane, and show it as a separate frame.

I am able to remove the tab and add it to a new frame, but i am not
able to display the contents on the frame.

Attached is a small example of what i want to achieve. Can anyone
suggest a solution?

On clicking the "Tear off Button", i need to be able to remove the
tab and show it as a part of a separate frame.

Thanks
Shridhar

<javacode>
import javax.swing.*;
import javax.swing.event.*;

import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class Test extends JPanel
{
public static int i = 0;
public Test()
{
super();

JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.LEFT);
JButton button = new JButton("Tear off this panel");
button.addActionListener(new MyActionListener(this,tabbedPane));
add(button);
tabbedPane.addTab("Tearoff",this);

JButton button2 = new JButton("Dummy Button");
tabbedPane.addTab("Dummy",button2);

JFrame frame1;
frame1 = new JFrame();
frame1.getContentPane().add(tabbedPane);
frame1.setTitle("Title " + i++);
frame1.pack();
frame1.setSize(300,300);
frame1.setVisible(true);
frame1.validate();
frame1.repaint();
}

public static void main(String args[])
{
new Test();
}
}

class MyActionListener implements ActionListener
{
JPanel mainPanel;
JTabbedPane tabbedPane;
public MyActionListener(JPanel mainPanel,JTabbedPane tabbedPane)
{
this.mainPanel = mainPanel;
this.tabbedPane = tabbedPane;
}
public void actionPerformed(ActionEvent e)
{
Container oldParent = mainPanel.getParent();
int index = tabbedPane.getSelectedIndex();
tabbedPane.removeTabAt(index);
JFrame frame = new JFrame();
frame.setTitle("Title " + Test.i++);
//JDesktopPane desktop = new JDesktopPane();
//JInternalFrame internalFrame = new JInternalFrame();
//internalFrame.getContentPane().add(mainPanel);
//internalFrame.setVisible(true);
//desktop.add(internalFrame);
//frame.setContentPane(desktop);

frame.getContentPane().add(mainPanel);
frame.pack();
frame.setSize(300,300);
frame.setVisible(true);
frame.validate();
frame.repaint();
oldParent.repaint();
}
}
</javacode>