1

given i have a button that would be added to different panels am i right to say instantiating 1 JButton is not possible?

example: added a "Cancel" Button to exit application and adding it to a Tab Pane with a certain amount of tabs.

can i do

JButton btnCancel = new JButton("Cancel");

and in one of the JPanel for the 1st tab of the JFrame tab1.add(btnCancel);

2nd tab tab2.add(btnCancel);

or must i create a new JButton for each tab pane?

sutoL
  • 1,737
  • 10
  • 27
  • 48

4 Answers4

2

..how can i make it such that the button would not make a new tab?

Use a nested layout. E.G.

import java.awt.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;

class CancelTab {

    public static void main(String[] args) {
        SwingUtilities.invokeLater( new Runnable() {
            public void run() {
                JPanel gui = new JPanel(new BorderLayout(3,3));
                gui.setBorder(new TitledBorder("GUI"));
                JPanel controls = new JPanel(
                    new FlowLayout(FlowLayout.CENTER,5,5));
                controls.add(new JButton("Commit"));
                controls.add(new JButton("Cancel"));

                gui.add(controls,BorderLayout.SOUTH);

                JTabbedPane tabbedPane = new JTabbedPane();

                tabbedPane.addTab("Tab 1", new JLabel("Label 1"));
                tabbedPane.addTab("Tab 2", new JLabel("Label 2"));
                gui.add(tabbedPane, BorderLayout.CENTER);

                JOptionPane.showMessageDialog(null, gui);
            }
        });
    }
}

Screen Shot

enter image description here

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • yes, this is what i am trying to do,but rather i added the buttons to my JFrame with a panel of buttons, that is still the same right – sutoL May 13 '11 at 01:39
  • Essentially yes. I prefer to use a `JPanel` for 2 reasons. 1) You can explicitly set a layout in the constructor (AFAIR the default layout of the content pane changed around 1.4 or 1.5) 2) There is no need to cast to a `JComponent` if you need to add a border. – Andrew Thompson May 13 '11 at 01:47
  • 1
    +1 for `JPanel` in this case; it also gives you double buffering and a suitable UI delegate. – trashgod May 13 '11 at 02:27
  • @trashgod: Apologies for my ignorance, but I do not understand what you mean by 'UI delegate'. Could you expand on 'delegate'? – Andrew Thompson May 13 '11 at 02:42
  • 1
    I'm always glad to pontificate! For [`JPanel`](http://download.oracle.com/javase/6/docs/api/javax/swing/JPanel.html), it's [`PanelUI`](http://download.oracle.com/javase/6/docs/api/javax/swing/plaf/PanelUI.html). For example, mine defaults to `cpm.apple.laf.AquaPanelUI`. More in [A Swing Architecture Overview](http://java.sun.com/products/jfc/tsc/articles/architecture/). – trashgod May 13 '11 at 04:24
1

Put it below the tab pane. So your hierarchy would look like this:

  • JFrame
    • JTabbedPane
      • JPanel for tab 1
      • JPanel for tab 2, etc.
    • JButton cancel
Josh Lee
  • 171,072
  • 38
  • 269
  • 275
  • i just tried it, but it would add a tab on its own with a button, how can i make it such that the button would not make a new tab? – sutoL May 13 '11 at 01:10
  • @kyrogue: your statement is in great need of clarification. – Hovercraft Full Of Eels May 13 '11 at 01:17
  • found the solution to what i was trying to do Container pane = myFrame.getContentPane(); pane.add(buttons,BorderLayout.PAGE_END); myFrame.add(tab); – sutoL May 13 '11 at 01:29
1

jleedev solution is best (1+), but if you absolutely need to add it to each JPanel, then you can either create an AbstractAction that is constructed with the button's text and create new JButtons with this one same AbstractAction, or you could create one single ButtonModel that is shared by all JButtons that have the same name and action.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • +1 for `Action`; see also this [example](http://stackoverflow.com/questions/4038605/swing-link-toggle-buttons-together-with-a-button-group-along-with-corresponding/4039359#4039359). – trashgod May 13 '11 at 02:24
0

If you plan for the buttons to have no function this will work. But if you plan on adding any Listeners to the objects the perform any tasks than this won't be a good option. But it could work if you were to get the parent container of the button when an action is performed and do tasks accordingly.

But either way it is more efficient and logical to create a new JButton for each use.

Jordonias
  • 5,778
  • 2
  • 21
  • 32