-1

I have a problem. I have 2 classes(MainFrame and MainContacts) I want to get the panel which is declared in MainContacts class and show it when I click action in the menu.

When i click right now nothing is happening, just like before click. I don't know where the problem is

My code:

public class MainFrame {

JFrame mainFrame = new JFrame("Team Helper");
JPanel mainPanel = new JPanel();

JMenuBar mainMenuBar = new JMenuBar();
    JMenu file = new JMenu("Plik");
        JMenuItem close = new JMenuItem("Zakoncz");

    JMenu tools = new JMenu("Narzedzia");
        JMenuItem contacts = new JMenuItem("Kontakty");

    JMenu help = new JMenu("Pomoc");
        JMenuItem information = new JMenuItem("O programie"); 

public void runContactForm()
{   
    new MainContacts();
}

public MainFrame(){

    mainFrame.setJMenuBar(mainMenuBar);


    mainMenuBar.add(file);
        file.add(close);

        close.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                mainFrame.dispose();
            }
        });

    mainMenuBar.add(tools);
        tools.add(contacts);

        contacts.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                runContactForm();
            }
        });

    mainMenuBar.add(help);  
        help.add(information);

    mainFrame.add(mainPanel);

    mainFrame.setVisible(true);
    mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    mainFrame.setSize(1600, 800);
    mainFrame.setResizable(false);
}

public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            new MainFrame();

        }
    });

}

}

second class:

public class MainContacts extends MainFrame {

JPanel leftPanel = new JPanel();
JPanel rightPanel = new JPanel();

JButton show = new JButton("Pokaż");
JButton addContact = new JButton("Dodaj kontakt");

public MainContacts() {

    leftPanel.add(show);
    leftPanel.add(addContact);

}

public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            new MainContacts();

        }
    });

}

}
ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
Rodenar
  • 1
  • 2
  • Please read [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice) – DavidPostill Sep 22 '14 at 19:43

2 Answers2

0

In your button handler, call MainContacts.main instead of instantiating directly

contacts.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {

        MainContacts.main(new String[] {});
    }
});
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
  • When i use it, its open new frame, so actionListner its work, but i still dont know how to invoke JPanel in my main class. Can you explain how i should do it? – Rodenar Sep 22 '14 at 19:23
  • You should open both Frames in the same class. That way you can access components of both from the same place – ControlAltDel Sep 22 '14 at 19:41
  • I delete main method from "MainContacts", just left constructor hmm but my Class "MainContacts" extends MainFrame so i should have access in "MainContacts" to filds from "MainFrame", and i should be able to add leftPanel to mainPanel insite "MainContact" constructor? – Rodenar Sep 22 '14 at 20:13
  • That's what I'm saying. Or make the class an inner class of class #1. If you don't want 2 classes to be independent, put them in the same file / class – ControlAltDel Sep 22 '14 at 20:27
  • I want do this but in my "MainContacts" class i dont see elements from "MainFrame". Its any special way to invoke this elements? – Rodenar Sep 22 '14 at 20:34
  • I dont want put them in the same file/class. I want to have just 1 main frame and other element i want load from other classes stored in project – Rodenar Sep 22 '14 at 20:52
0

I found solution for invoke JPanel. I change mainPanel to "protected" and write now i see elements from "MainFrame". I add to mainPanel leftPanel and its works.

First task, done.

The next problem, come when i invoke constoructor. Its not work in the same frame, is opening new frame with added leftPanel.

Code below:

public class MainFrame {

JFrame mainFrame = new JFrame("Team Helper");
protected JPanel mainPanel = new JPanel();

JMenuBar mainMenuBar = new JMenuBar();
    JMenu file = new JMenu("Plik");
        JMenuItem close = new JMenuItem("Zakoncz");

    JMenu tools = new JMenu("Narzedzia");
        JMenuItem contacts = new JMenuItem("Kontakty");

    JMenu help = new JMenu("Pomoc");
        JMenuItem information = new JMenuItem("O programie"); 

public void runMainContact()
{
    new MainContacts(); 
}

public MainFrame(){

    mainFrame.setJMenuBar(mainMenuBar);

    mainMenuBar.add(file);
        file.add(close);

        close.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                mainFrame.dispose();
            }
        });

    mainMenuBar.add(tools);
        tools.add(contacts);

        contacts.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub

                runMainContact();
            }
        });

    mainMenuBar.add(help);  
        help.add(information);


    mainFrame.add(mainPanel);

    mainFrame.setVisible(true);
    mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    mainFrame.setSize(1600, 800);
    mainFrame.setResizable(false);
}

public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            new MainFrame();

        }
    });

}


}

Second class:

public class MainContacts extends MainFrame {

protected JPanel leftPanel = new JPanel();
JPanel rightPanel = new JPanel();

JButton show = new JButton("Pokaż");
JButton addContact = new JButton("Dodaj kontakt");

public MainContacts() {

    mainPanel.add(leftPanel);
    leftPanel.add(show);
    leftPanel.add(addContact);

}   

}
Rodenar
  • 1
  • 2