Domanda

I just get stuck with app. The thing is in second actionListener, I want to get object or more specifically access to methods in my JDialog class.

I got few dialogs created, but not visible. In first actionListener I get to them by getDialog function which is returning JDialog. So I can each one of them visible. 2nd actionListener which I need help with, is showing JOptionPane and if user pick the YES_OPTION I want to run my method from specific dialog.

I it's not clear I'd try to fix my explanations so you can understand it.

modifyButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            JButton thisButton = (JButton) e.getSource();
            JPanel parentPanel = (JPanel) thisButton.getParent();
            Container topLevel = parentPanel.getTopLevelAncestor();
            MainFrame mainFrame = (MainFrame) topLevel;

            mainFrame.getDialog(TABLECOUNTER).setVisible(true);

        }
    });

    abortButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            Object[] options = {"Tak", "Nie"};
            int userReply = JOptionPane.showOptionDialog(null, "Czy na pewno chcesz anulować rachunek?", "Probujesz anulować rachunek!", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]);
            if (userReply == JOptionPane.YES_OPTION) {
                JButton thisButton = (JButton) e.getSource();
                JPanel parentPanel = (JPanel) thisButton.getParent();
                Container topLevel = parentPanel.getTopLevelAncestor();
                MainFrame mainFrame = (MainFrame) topLevel;

                mainFrame.getDialog(TABLECOUNTER).myMethod(); //here
            }
        }
    });
È stato utile?

Soluzione

"I want to get object or more specifically access to methods in my JDialog class"

If the method getDialog returns a standard JDialog,

public JDialog getDialog(...) {}

Then you're stuck with the methods of JDialog, without proper casting, or changing the return type. That would explain why you are able to setVisible in the first method, because JDialog does have a method setVisible. So to access the method myMethod you'll want to do some casting.

((MyDialog)mainFrame.getDialog(TABLECOUNTER)).myMethod();

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top