1

Can we use a JInternalFame with a button in the main frame? The frame contains a JDesktopPane, of course. The button should open up the JInternalFrame How?

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Eddinho
  • 1,279
  • 5
  • 19
  • 29

2 Answers2

6

I don't know a way to put a JButton directly on a JDesktopPane, but you can use menu items to create and select a JInternalFrame. In this example, each menu item uses an Action defined in the JInternalFrame to select the corresponding frame.

class MyFrame extends JInternalFrame {

    private Action action;

    MyFrame(JDesktopPane desktop, String name, int offset) {
        …
        action = new AbstractAction(name) {
            @Override
            public void actionPerformed(ActionEvent ae) {
                try {
                    MyFrame.this.setSelected(true);
                } catch (PropertyVetoException e) {
                    e.printStackTrace();
                }
            }
        };
    }

    public Action getAction() { return action; }
}

Addendum: as @camickr suggests, it is technically possible to put a JButton directly on a JDesktopPane, but it might prove difficult to use in practice.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • A JDesktopPane is just like any panel that uses a "null layout". You can add any component to it as long as you set the bounds of the component. Of course it doesn't make sense to do this since an internal frame can be moved over the button, so a menu, or "toolbar" that sits outside the desktop pane is definitely a better choice. – camickr Apr 30 '10 at 01:51
2

I don't really understand the question so I will just make some observations.

a) a JInternalFrme is like a frame in that you can add any component to it that you want

b) A JButton works the same whether it is added to an internal frame or a frame

I suggest you start by reading the Swing tutorial for working examples. You might start with the sections on "How to Use Internal Frames" and "How to Use Buttons".

If you still have problems then post your SSCCE that shows what you have tried.

camickr
  • 321,443
  • 19
  • 166
  • 288