CodeMed
CodeMed

Reputation: 9205

Java tabbed panes within internal frame

I am trying to get an internal frame to contain tabbed panes. However, my code does not seem to be loading the panes into the internal frame. I have my code in the java files, called InternalFrame.java and TabbedPaneSample.java. The code for both files is included below. Can anyone show me how to fix the code below so that it loads the tabbed panes when I run InternalFrame.java?

Here is my code:

The code for InternalFrame.java is:

package test;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLayeredPane;

public class InternalFrame extends JFrame {
    JButton openButton;
JLayeredPane desktop;
JInternalFrame internalFrame;
TabbedPaneSample myTabbedPaneSample = new TabbedPaneSample();

public InternalFrame() {
    super("Click button to open internal frame with two panels.");
    setSize(500, 400);
    openButton = new JButton("Open");
    Panel p = new Panel();
    p.add(openButton);
    add(p, BorderLayout.SOUTH);
    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });
    openButton.addActionListener(new OpenListener());
    desktop = new JDesktopPane();
    desktop.setOpaque(true);
    add(desktop, BorderLayout.CENTER);
}
class OpenListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        if ((internalFrame == null) || (internalFrame.isClosed())) {
            internalFrame = new JInternalFrame("Internal Frame", true, true, true, true);
            internalFrame.setBounds(50, 50, 200, 100);
            internalFrame.add(myTabbedPaneSample, BorderLayout.CENTER);
            internalFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            internalFrame.pack();
            internalFrame.setMinimumSize(new Dimension(300, 300));
            desktop.add(internalFrame, new Integer(1));
            internalFrame.setVisible(true);
        }
    }
}
public static void main(String args[]) {
    InternalFrame myInternalFrame = new InternalFrame();
    myInternalFrame.setVisible(true);
}
}

And the code for TabbedPaneSample.java is:

package test;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

public class TabbedPaneSample extends JTabbedPane {
private JTabbedPane tabbedPane = new JTabbedPane();
private ImageIcon closeImage = new ImageIcon("C:/test/shipIcon.gif");
private Dimension closeButtonSize;
private int tabCounter = 0;

public TabbedPaneSample() {
    closeButtonSize = new Dimension(closeImage.getIconWidth() + 2, closeImage.getIconHeight() + 2);
    }
public void add() {
    final JPanel content = new JPanel();
    JPanel tab = new JPanel();
    tab.setOpaque(false);
    JLabel tabLabel = new JLabel("Tab " + (++tabCounter));
    JButton tabCloseButton = new JButton(closeImage);
    tabCloseButton.setPreferredSize(closeButtonSize);
    tabCloseButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int closeTabNumber = tabbedPane.indexOfComponent(content);
            tabbedPane.removeTabAt(closeTabNumber);
        }
    });
    tab.add(tabLabel, BorderLayout.WEST);
    tab.add(tabCloseButton, BorderLayout.EAST);
    this.addTab(null, content);
    this.setTabComponentAt(this.getTabCount() - 1, tab);
}
public static void main(String[] args) {
    TabbedPaneSample main = new TabbedPaneSample();
    main.add();
    main.add();
}
}

Upvotes: 2

Views: 5085

Answers (3)

Konstantin S. Lokhtin
Konstantin S. Lokhtin

Reputation: 11

I preferred to create in my Main Frame class (which extends JFrame) the following function:

private void showIntFrame(Class intFrameClass) {
    JInternalFrame targetFrame = null;
    int xoff = 0, yoff = 0;
    for(JInternalFrame jif : jdp.getAllFrames()) {
        if(jif.getClass().equals(intFrameClass))
            targetFrame = jif;
        if(jif.getLocation().x > xoff)
            xoff = jif.getLocation().x;
        if(jif.getLocation().y > yoff)
            yoff = jif.getLocation().y;
        }
        if(targetFrame == null) {
            try {
                Constructor<JInternalFrame> c = intFrameClass.getConstructor(MainFrame.class);
                targetFrame = c.newInstance(MainFrame.this);
            } catch (Exception ex) {
                System.err.println("Exception in MainFrame.showIntFrame() while creating new JInternalFrame instance. " + ex.getLocalizedMessage());
                ex.printStackTrace();
                return;
            }
            jdp.add(targetFrame);
            targetFrame.setLocation(xoff + 30, yoff + 30);
        }
        targetFrame.setVisible(true);
        try {
            targetFrame.setSelected(true);
        } catch (PropertyVetoException ex) {
            System.err.println("PropertyVetoException in MainFrame.showIntFrame() while activating JInternalFrame instance. " + ex.getLocalizedMessage());
        }
}

Here jdp is instance of JDesktopPane, which previously was set as ContentPane of my main JFrame.

Because my programs often contain numbers of different classes, inherited from JInternalFrame, it is easier to call this function from event handlers to show new subclass of JInternalFrame.

Every subclass of JInternalFrame in my programs have one constructor with one parameter - MainFrame (main JFrame).

Upvotes: 1

trashgod
trashgod

Reputation: 205875

Here's one approach, shown below. A more flexible approach using Action is referenced here.

Addendum: Reviewing your code, you should let the various layout managers and component preferred sizes do more of the work, as shown. In particular, this.setPreferredSize() is done for demonstration purposes. In a real application, you would restore user size and location preferences.

Desktop pane with internal frames

package overflow;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

/** @see https://stackoverflow.com/posts/6514889 */
public class InternalFrame extends JFrame {

    JButton openButton;
    JLayeredPane desktop;
    JInternalFrame internalFrame;

    public InternalFrame() {
        super("Click button to open internal frame with two tabs.");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setPreferredSize(new Dimension(400, 400));
        openButton = new JButton("Open");
        JPanel p = new JPanel();
        p.add(openButton);
        this.add(p, BorderLayout.SOUTH);
        openButton.addActionListener(new OpenListener());
        desktop = new JDesktopPane();
        this.add(desktop, BorderLayout.CENTER);
        this.pack();
        this.setLocationRelativeTo(null);
    }

    class OpenListener implements ActionListener {

        private static final int DELTA = 40;
        private int offset = DELTA;

        public void actionPerformed(ActionEvent e) {
            internalFrame = new JInternalFrame(
                "Internal Frame", true, true, true, true);
            internalFrame.setLocation(offset, offset);
            offset += DELTA;
            internalFrame.add(createTabbedPane());
            desktop.add(internalFrame);
            internalFrame.pack();
            internalFrame.setVisible(true);
        }
    }

    private JTabbedPane createTabbedPane() {
        JTabbedPane jtp = new JTabbedPane();
        createTab(jtp, "One");
        createTab(jtp, "Two");
        return jtp;
    }

    private void createTab(JTabbedPane jtp, String s) {
        jtp.add(s, new JLabel("TabbedPane " + s, JLabel.CENTER));
    }

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                InternalFrame myInternalFrame = new InternalFrame();
                myInternalFrame.setVisible(true);
            }
        });
    }
}

Upvotes: 4

jfpoilpret
jfpoilpret

Reputation: 10519

First of all, I think Finally, I think you shouldn't use desktop.add(internalFrame, new Integer(1)) but rather desktop.add(internalFrame) instead, the reason is that JDesktopPane uses its layers (it is a JLayeredPane subclass) internally, and I don't think you should play with layers yourself.

Then, following this problem I had once with JInternalFrame, I would advise you call pack() after adding the internal frame to the desktop pane.

Hence, you should try with your OpenListener class looking like this:

class OpenListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        if ((internalFrame == null) || (internalFrame.isClosed())) {
            internalFrame = new JInternalFrame("Internal Frame", true, true, true, true);
            internalFrame.setBounds(50, 50, 200, 100);
            internalFrame.add(myTabbedPaneSample, BorderLayout.CENTER);
            internalFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//          internalFrame.pack();
            internalFrame.setMinimumSize(new Dimension(300, 300));
//          desktop.add(internalFrame, new Integer(1));
            desktop.add(internalFrame);
            internalFrame.pack();
            internalFrame.setVisible(true);
        }
    }
}

Besides, I also agree with trashgod comments on Action of course and the simplifying rework he has done on your snippet.

Upvotes: 2

Related Questions