Vijay
Vijay

Reputation: 67231

Changing a class variable by occurance of an event

Firstly i am new to java. So i am not good enough for writing complex programs in java. I wrote a small program and am i just trying to understand how the class variables are accessed and changed. I have two issues here.

1)changing a class variable when an event is occured . is not working.

2)calling a method of a another class with its object when an event is occured is also not working.

Below are the steps i followed for writing the code:

below is the code:

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

class a {

    String user = "";

    public void start(String us) {
        user = us;
        System.out.println("user is:" + user);
    }
}

public class test extends JFrame implements ActionListener {

    private JTextArea ta;
    private int count;
    private JMenuBar menuBar;
    private JMenu fileM, editM, viewM;
    private JScrollPane scpane;
    private JMenuItem exitI, cutI, copyI, pasteI, selectI, saveI, loadI, 
            statusI, db1, db2, db3, db4;
    private String pad;
    private JToolBar toolBar;
    private a obj;
    public static String dbname, dbpsw, dbuser, dbconn;

    public test() {
        super("SybaseUI");
        obj = new a();
        obj.start("from constructor");
        setSize(600, 600);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container pane = getContentPane();
        pane.setLayout(new BorderLayout());
        ta = new JTextArea(); //textarea
        menuBar = new JMenuBar(); //menubar
        fileM = new JMenu("File"); //file menu
        scpane = new JScrollPane(ta); //scrollpane  and add textarea to scrollpane
        exitI = new JMenuItem("Exit");
        db1 = new JMenuItem("CNA");
        ta.setLineWrap(true);
        ta.setWrapStyleWord(true);
        setJMenuBar(menuBar);
        menuBar.add(fileM);
        fileM.add(db1);
        pane.add(scpane, BorderLayout.CENTER);
        exitI.addActionListener(this);
        setVisible(true);
        ta.addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent ke) {
                int code = ke.getKeyCode();
                int modifiers = ke.getModifiers();
                if (code == KeyEvent.VK_ENTER && modifiers == KeyEvent.CTRL_MASK) {
                    dbconn = dbuser + " " + dbpsw + " " + dbname;
                    System.out.println("dbconn is:" + dbconn);
                    obj.start("john");
                }
            }
        });
    }

    public void actionPerformed(ActionEvent e) {
        JMenuItem choice = (JMenuItem) e.getSource();
        if (choice == db1) {
            dbname = "cnadb";
            dbpsw = "xxxxxxx";
            dbuser = "sa";
        }
    }

    public static void main(String[] args) {
        new test();
    }
}

Upvotes: 0

Views: 87

Answers (1)

Vinay
Vinay

Reputation: 6881

In the filemenu when i select teh menuitem the event gets called but teh class variables are not changed even after changing them inside the handler.

You actually missed adding a listener to your menuitem so they were not changed. Try adding this.

db1.addActionListener(this);

As you didn't add this your below code didn't work so the class variables were not changed

if (choice == db1) {
     dbname = "cnadb";
     dbpsw = "xxxxxxxx";
     dbuser = "sa";
}

Upvotes: 2

Related Questions