sam4ritan
sam4ritan

Reputation: 27

Change an object value from another method

Following problem:

I want to change one value to another, but from a different method. the full code (as requested) is as follows:

import javax.swing.*;
import java.net.*;
import java.awt.event.*;

public class main {

    public static void about() {
        JDialog aboutWindow = new JDialog();
        aboutWindow.setTitle("About kingfisher a0.0.3");
        aboutWindow.setSize(300, 600);
        aboutWindow.setModal(true);
        JMenuBar menubar = new JMenuBar();
        JMenu control = new JMenu("Control");
        JMenuItem quit = new JMenuItem("quit");

        quit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                aboutWindow.setVisible(false);
            }
        });

        control.add(quit);
        menubar.add(control);
        aboutWindow.add(menubar);
        JLabel name = new JLabel("kingfisher");
        aboutWindow.add(name);
    }

    public static void main(String args[]) {
        //Defined window dimensions
        JFrame controlpanel = new JFrame();
        controlpanel.setTitle("kingfisher a0.0.3");
        controlpanel.setSize(500, 400);
        controlpanel.add(new JLabel("The angels have the blue box"));
        JMenuBar menubar = new JMenuBar();
        //defining menu groups
        JMenu windows = new JMenu("Windows");
        JCheckBoxMenuItem chat = new JCheckBoxMenuItem("Chat");
        JCheckBoxMenuItem filetransfer = new JCheckBoxMenuItem("Filetransfer");
        JCheckBoxMenuItem settings = new JCheckBoxMenuItem("Settings");
        windows.add(chat);
        windows.add(filetransfer);
        windows.add(settings);
        JMenu control = new JMenu("Control");
        JMenuItem quit = new JMenuItem("Quit");

        quit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                System.exit(0);
            }
        });
        control.add(quit);
        JMenu help = new JMenu("Help");
        JMenuItem support = new JMenuItem("Support");
        JMenuItem about = new JMenuItem("About");
        about.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                //HERE!
                aboutWindow.setVisible(true);
            }
        }
        );
        help.add(support);
        help.add(about);
        menubar.add(control);
        menubar.add(windows);
        menubar.add(help);
        controlpanel.setJMenuBar(menubar);
        controlpanel.setVisible(true);
    };
};

aboutWindow has been defined in the same class, but not in the main method but in in the about method. How do i address it correctly?

My current solution (not working at all) is in the main method, I have marked it with a comment.

Upvotes: 1

Views: 82

Answers (2)

bruno
bruno

Reputation: 2273

Try making aboutWindow a class variable (it's also a good practice to rename your class to camel case Main):

import javax.swing.*;
import java.net.*;
import java.awt.event.*;

public class Main
{
    public static JDialog aboutWindow;

    public static void about () {
    Main.aboutWindow = new JDialog();
    Main.aboutWindow.setTitle("About kingfisher a0.0.3");
    Main.aboutWindow.setSize(300,600);
    Main.aboutWindow.setModal(true);
        JMenuBar menubar = new JMenuBar();
            JMenu control = new JMenu("Control");
                JMenuItem quit = new JMenuItem("quit");
                    quit.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent event) {
                            Main.aboutWindow.setVisible(false);
                            }
                        }
                    );
                control.add(quit);
            menubar.add(control);
        Main.aboutWindow.add(menubar);
    JLabel name = new JLabel("kingfisher");
    Main.aboutWindow.add(name);
    }

    public static void main (String args[]){
    //Defined window dimensions
    JFrame controlpanel = new JFrame();
    controlpanel.setTitle("kingfisher a0.0.3");
    controlpanel.setSize(500,400);
    controlpanel.add(new JLabel("The angels have the blue box"));
        JMenuBar menubar = new JMenuBar();
        //defining menu groups
            JMenu windows = new JMenu("Windows");
                JCheckBoxMenuItem chat = new JCheckBoxMenuItem("Chat");
                JCheckBoxMenuItem filetransfer = new JCheckBoxMenuItem("Filetransfer");
                JCheckBoxMenuItem settings = new JCheckBoxMenuItem("Settings");
                windows.add(chat);
                windows.add(filetransfer);
                windows.add(settings);
            JMenu control = new JMenu("Control");
                JMenuItem quit = new JMenuItem("Quit");
                quit.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent event) {
                        System.exit(0);
                        }
                    }
                );
                control.add(quit);
            JMenu help = new JMenu("Help");
                JMenuItem support = new JMenuItem("Support");
                JMenuItem about = new JMenuItem("About");
                    about.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent event) {
                            //HERE!
                            Main.aboutWindow.setVisible(true);
                            }
                        }
                    );
                help.add(support);
                help.add(about);
            menubar.add(control);
            menubar.add(windows);
            menubar.add(help);
        controlpanel.setJMenuBar(menubar);
    controlpanel.setVisible(true);
    };
};

Upvotes: 1

C-Otto
C-Otto

Reputation: 5853

I assume that aboutWindow is a local variable which is declared in the method named about. A simple approach to make aboutWindow visible to code from other methods (like the one shown in your question) is to declare it as a field. For that, move the declaration out of the about method.

Upvotes: 1

Related Questions