2

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();
}
}
CodeMed
  • 9,527
  • 70
  • 212
  • 364
  • 1
    Is there any reason why you are using `java.awt.Panel` where you could use `javax.swing.JPanel`? If you don't have very good reasons to do so, you should avoid mixing AWT and Swing components. – jfpoilpret Jun 29 '11 at 04:28
  • Thank you each so much for your help. I will review this code in detail tomorrow and reply specifically at that point. – CodeMed Jun 29 '11 at 09:32

3 Answers3

4

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);
            }
        });
    }
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 1
    I don't see any reason to extend `JTabbedPane`. Of course, there's no reason to extend `JFrame`, either. :-) – trashgod Jun 29 '11 at 01:38
  • Thank you again. I tried this, and it mainly works, but the size of each internal frame is too small. I tried adding this.setMinimumSize(new Dimension(300,300)); and this.setSize(300,300); immediately after your this.setPreferredSize(new Dimension(400, 400)); . However, the size of the internal frame was not changed. Do you have any suggestions for how to fix this so that the size of the internal frame defaults to some defined minimum, such as (300,300)? I should note that I am using Windows 7, though of course this needs to be cross-platform. – CodeMed Jun 29 '11 at 21:48
  • I think it's better to let the components' preferred sizes govern, but you can replace `internalFrame.pack()` with `internalFrame.setSize(320, 240)`, for example. – trashgod Jun 29 '11 at 23:15
  • I just wrote `internalFrame.pack();` right before `internalFrame.setSize(new Dimension(600,300));` . Do you think this ordering will retain some of the goodness of `internalFrame.pack()` ? When I do not use `setSize`, my screen outputs something that is so tiny that it mangles the intended content on my screen. Yet, `setMinimumSize` does nothing to improve the situation. – CodeMed Jun 29 '11 at 23:45
  • Yes, `pack()` is valuable; I meant to suggest "replace" as an experiment. See also the [article](http://jfpoilpret.blogspot.com/2009/02/jinternalframe-lesson-learned.html) cited by @jfpoilpret. – trashgod Jun 29 '11 at 23:56
  • Thanks. And yes, I went to every link people suggested in this posting. I am going to mark this as answered now. Thank you very much for your help. – CodeMed Jun 30 '11 at 00:54
2

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.

jfpoilpret
  • 10,449
  • 2
  • 28
  • 32
  • great country (blog) +1 for Java – mKorbel Jun 29 '11 at 06:55
  • +1 Thanks for expanding on this; `add()` followed by `pack()` made sense empirically, but I didn't know why. – trashgod Jun 29 '11 at 11:38
  • @mKorbel: Your +1 appears to have been lost. :-) – trashgod Jun 29 '11 at 11:40
  • @trashgod yes thanks for, throught working hours I behind *** and proxy and ***, I can't see icon, pictures, tags and etc.., joke is that +1 for you received SO server, maybe I need to stop drinking – mKorbel Jun 29 '11 at 11:47
  • @trashgod bump and when did you sleep (US time zone) – mKorbel Jun 29 '11 at 11:51
  • @mKorbel regarding the country in my blog, actually that's not true anymore (I should be less lazy and update it now;-)) Now I'm in Switzerland, which is also a great country, although dramatically different from Vietnam. – jfpoilpret Jun 29 '11 at 20:56
1

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).