When I click a button to open a window in another window thats not the main window, it doesn´t open the window

30 Views Asked by At

Im doing a project in window builder in eclipse, when adding action perform it doesn't perform the action.

I have this code for the second window that does open

/**
     * Launch the application.
     */
    public static void main(String[] args) {
        try {
            WhatsAJmenu dialog = new WhatsAJmenu();
            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dialog.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Create the dialog.
     */
    public WhatsAJmenu() {
        setBounds(100, 100, 450, 300);
        getContentPane().setLayout(new BorderLayout());
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        contentPanel.setLayout(null);
        
        JTextPane txtpnAJmenuIs = new JTextPane();
        txtpnAJmenuIs.setBounds(10, 11, 414, 206);
        txtpnAJmenuIs.setText("First you need a JMenu bar to add the items into the menu.\r\nSecondly you can add a JMenu, so when clicked on this element the JMenuItem performs an Action, in this case the action was opening the tab with the text you're reading.\r\n\r\n(Close this tab if you have finished reading)");
        contentPanel.add(txtpnAJmenuIs);
        
        JLayeredPane layeredPane = new JLayeredPane();
        layeredPane.setBounds(0, 0, 1, 1);
        contentPanel.add(layeredPane);
        {
            JPanel buttonPane = new JPanel();
            buttonPane.setBackground(new Color(216, 191, 216));
            buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
            getContentPane().add(buttonPane, BorderLayout.SOUTH);
            {
                JButton cancelButton = new JButton("I have finished reding this.");
                cancelButton.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        System.exit (0);
                    }
                });
                cancelButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                    }
                });
                
                JButton btnNewButton = new JButton("I haven't finished reading this.");
                btnNewButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        NotReading window = new NotReading();
                        window.setVisible(true);
                    }
                });
                
                buttonPane.add(btnNewButton);
                cancelButton.setActionCommand("Cancel");
                buttonPane.add(cancelButton);
            }
        }
    }
}

Then this is the other window that I want to open but doesn't

package tabGame;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import java.awt.Color;
import javax.swing.JLayeredPane;

public class NotReading extends JFrame {

    private static final long serialVersionUID = 1L;
    private JPanel contentPane;
    private JTextField textField;

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

    /**
     * Create the frame.
     */
    public NotReading() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBackground(new Color(255, 255, 51));
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

        setContentPane(contentPane);
        contentPane.setLayout(null);
        
        textField = new JTextField();
        textField.setBackground(new Color(255, 255, 51));
        textField.setBounds(0, 0, 434, 261);
        contentPane.add(textField);
        textField.setColumns(10);
        
        JLayeredPane layeredPane = new JLayeredPane();
        layeredPane.setBounds(0, 0, 1, 1);
        contentPane.add(layeredPane);
    }
}

I tried changing the action in here (it's now as it was)

JButton btnNewButton = new JButton("I haven't finished reading this.");
                btnNewButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        NotReading window = new NotReading();
                        window.setVisible(true);
                    }
                });
                
                buttonPane.add(btnNewButton);
                cancelButton.setActionCommand("Cancel");
                buttonPane.add(cancelButton);

But it still doesn´t work, I tried adding a dialog in the window that does open, but I'm slow or it doesn´t work, but the window still doesn't open. I dont know what else to do.

0

There are 0 best solutions below