Calling a jFrame from another class in java click event

Asked

Viewed 139 times

0

I have a Jframe with two buttons, and a second class that has a Jframe with other features... I wanted to make the second window open by clicking on one of the buttons in the first window, but I can’t make the call inside the first class click event. Any suggestions?

//window start

    public class JanelaPrincipal {  

    public static void main (String[] args){    
      JFrame frame = new JFrame("Test");
      frame.setVisible(true);
      frame.setSize(500,500);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      JPanel panel = new JPanel();
      frame.add(panel);
      JButton montar = new JButton("Montar Grade");
      panel.add(montar);
      montar.addActionListener((ActionListener) new Action1());

      JButton analisar = new JButton("Analisar");
      panel.add(analisar);
      analisar.addActionListener (new Action2()); 
    }

    static class Action1 implements ActionListener {        
      public void actionPerformed (ActionEvent e) {
          //abrir primeira janela aqui
      }
    }   

    static class Action2 implements ActionListener {        
      public void actionPerformed (ActionEvent e) {     
        //abrir segunda janela aqui
      }
    }   

}

//window to open with click public class Extends Thread window{

    private JFrame frame;
    private JTextField textField;

    private DefaultComboBoxModel<Grafo.Vertice> origem = new DefaultComboBoxModel<>();
    private DefaultComboBoxModel<Grafo.Vertice> destino = new DefaultComboBoxModel<>();
    private DefaultComboBoxModel<Grafo.Vertice> vertice = new DefaultComboBoxModel<>();
    private DefaultComboBoxModel<Grafo.Vertice> vertice2 = new DefaultComboBoxModel<>();



    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Janela window = new Janela();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Janela() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */

     Grafo g = new Grafo();
     Grafo.Vertice s = g.addVertice("Arq e org computadores");
     Grafo.Vertice t = g.addVertice("Sistemas lógicos e digitais");
     Grafo.Vertice y = g.addVertice("Matemática Discreta");
     Grafo.Aresta st = g.addAresta(s, t);
     Grafo.Aresta sy = g.addAresta(s, y);
     Grafo.Aresta ty = g.addAresta(t, y);
     private JTextField tfAlt;


    protected void initialize() {
        frame = new JFrame();

        origem.addElement(s);
        destino.addElement(s);
        vertice.addElement(s);
        vertice2.addElement(s);

        origem.addElement(t);
        destino.addElement(t);
        vertice.addElement(t);
        vertice2.addElement(t);

        origem.addElement(y);
        destino.addElement(y);
        vertice.addElement(y);
        vertice2.addElement(y);

.......
  • already tried Filename window = new Filename(); window.setVisible(true); ?

  • Simple, and it’s been answered here. One of the questions I suggest a better approach than using 2 jframes in the same application, this may give you problem in the future if you do not know what you are doing.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.