-3

I am using java swing and I want to add 3 tabs after the user is choosing "New" from JMenuBar.

The tabs won't be there when the application is started. These will be shown only after choosing "New".

How can I Do it? Do I need to add these to the actionListener of "New"? How it can be added?

SaintLike
  • 9,119
  • 11
  • 39
  • 69
D.L.
  • 169
  • 3
  • 17
  • i was adding insertTab within the actionListener. but it was showing method not found. – D.L. Jul 13 '12 at 07:01

2 Answers2

4

Since you tagged this question with JTabbedPane, I assume that is the component you are using. You can use the addTab or insertTab methods to add a tab.

If you want to do this in reaction of a button press, putting those calls in the ActionListener is indeed a valid solution.

Robin
  • 36,233
  • 5
  • 47
  • 99
1

add an ActionListener to your JButton like this:

final JTabbedPane tabbedPane = new JTabbedPane();
    JButton addButton = new JButton("new");
    addButton.addActionListener(new ActionListener() {
        //will be called if the button gets clicked
        @Override
        public void actionPerformed(ActionEvent e) {
            JPanel panel = new JPanel();//will be displayed in the Tab
            tabbedPane.add("title", panel);
            //.add() is the easier way for tabbedPane.insertTab(many arguments here)
            //add what ever you like(repeat three times)
        }
    });
kleopatra
  • 51,061
  • 28
  • 99
  • 211
Simulant
  • 19,190
  • 8
  • 63
  • 98
  • 1
    +1 for making me look into the source of tabbedPane.add(..) - astonishingly, it really is overridden to delegate to addTab (for components != UIResource :-) – kleopatra Jul 13 '12 at 10:00
  • @kleopatra looking at the documentation would have been sufficient. Javadoc mentions 'Cover method for `insertTab`.' – Robin Jul 13 '12 at 10:23
  • @Robin - true, though I'm used to not fully rely on documentation, being a lazy doc writer myself :-) Plus it doesn't mention the UIResource stuff: not important for application code, but for framework code (one surprise less ...) – kleopatra Jul 13 '12 at 10:49
  • this is not working.. It is not showing any error. But after clicking the button the tabs are not created.. I have tried it in jdk... and for insertTab it is showing "can not find insertTab symbol". – D.L. Jul 14 '12 at 18:47