2

I have One JFrame and two JPanels. Jframe contains JTabbedPane, MenuBar and one menuItem. When I click on the menuitem, a panel is added to the JTabbedPane. The code is this -

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        NewJPanel jp = new NewJPanel();
        jTabbedPane1.add("Panel",jp);
    }

It is working fine. This panel contains a button. I want that on clicking the button a new panel to be added in JTabbedPane. For the actionPerformed method of button I am having this code -

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        NewJFrame1 jf = new NewJFrame1();
        jf.addPanel();
    }

addPanel is a method I have defined in JFrame. The code of addPanel() is

public void addPanel()
    {
        System.out.println("Method Called");
        NewJPanel1 jp1 = new NewJPanel1();
        jTabbedPane1.add("Panel1",jp1);
    }

But when I am clicking on the button I am getting "Method Called". It means that the method is being called, but the panel is not added to the JTabbedPane. Please help.

I have also tried this in the code of actionPerformed Method of button after making JTabbedPane1 public in JFrame-

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        NewJFrame1 jf = new NewJFrame1();
        NewJPanel1 pane = new NewJPanel1();        
        jf.jTabbedPane1.add("Panel1",pane);
    }

But this is also not adding the Panel. What should I do?

Ajay Sainy
  • 279
  • 1
  • 9
  • 21
  • 1
    Your question, although well scoped, is hard to follow due to the disorganization of the high amount of detail. Please consider revising your question in a less complicated manner. – Patrick Sebastien Jul 02 '13 at 13:07
  • @Allan In short Iwant to add a new tab(panel) to jtabbedpane on a button click. – Ajay Sainy Jul 02 '13 at 13:39
  • Ok, one more thing I want to clarify: Do you want this to tab to be something that can be added an infinite number of times, or do you have a specific tab that you want to display only once? An example of the former situation could be the "add new tab" feature in web browsers. An example of the latter situation could be a "show/hide" type of button. – Patrick Sebastien Jul 02 '13 at 14:04
  • @Allan First one(like browser new tabs) – Ajay Sainy Jul 02 '13 at 14:23
  • 1
    does the new tab has it own actionlistener? if not, I could help you with this. – A-SM Jul 02 '13 at 15:04
  • @A-SM New Tab is just a panel, it does not have actionlistener. – Ajay Sainy Jul 04 '13 at 04:43
  • Umm, not the panel, but elements inside it, like textfield/button iside that panel will react when you click it – A-SM Jul 05 '13 at 02:03

2 Answers2

2

Your code contains errors, in these two lines :

NewJFrame1 jf = new NewJFrame1(); jf.addPanel();

You have to work on the same JFrame, actually you are creating the pannel on a new hidden JFrame.

I adapted your example and it is working:

public void addPanel()
{
    System.out.println("Method Called");
    JPanel jp1 =  new JPanel();
    jTabbedPane1.add("Panel1",jp1);
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

    this.addPanel();  

} 
AMA1123
  • 21
  • 7
0
public void createPage1() {
        panel1 = new JPanel();
        panel1.setLayout(null);

        JLabel label1 = new JLabel("Username:");
        label1.setBounds(10, 15, 150, 20);
        panel1.add(label1);

        JTextField field = new JTextField();
        field.setBounds(10, 35, 150, 20);
        panel1.add(field);

        JLabel label2 = new JLabel("Password:");
        label2.setBounds(10, 60, 150, 20);
        panel1.add(label2);

        JPasswordField fieldPass = new JPasswordField();
        fieldPass.setBounds(10, 80, 150, 20);
        panel1.add(fieldPass);

         JButton login=new JButton("login");
         login.setBounds(10, 120, 80, 25);
         panel1.add(login);

         login.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

            panel4=new JPanel();    
            //panel4.setLayout(null);
            panel4.add(new JLabel("welcome buddy"));
            tabbedPane.addTab("Page 4", panel4);
            }
        });
    }