Marian Pazioch
Marian Pazioch

Reputation: 83

Modification of variables/other swing objects on button click

Ok so basically im struggling to perform some actions with Java Swing JButtons. I have a feeling that my problem is an ease for skilled programmers so bare with my "elementary" problems:

What i'd like to do is modify content of other JComponent and possibly modify some variables on button click. It seems relatively easy but i would like to perform it on "already declared" variables (or already created JComponents)

Wherever i search i always get examples of cases where everything is declared from scratch inside implementation of ActionPerformed function related to specific JButton, just like here:

http://zetcode.com/tutorials/javaswingtutorial/swingevents/ //above you can observe that in "actionPerformed" function they define new variables such as: string/date/locale/stringbuffer

Such solution does not satisfy my needs by any means. I want to modify/show variable that was previously defined due to user click with use of function that belongs to different class.

Ok maybe it will be more clear if i'll show you what i'd like to do on a exemplary sourcecode:

class Number_String {

    public String change_add_MOD (String sss){        
        String str_modified = sss + "_modified";        
        return str_modified;
    }
    public int change_plus1 (int z){        
        int z_modified = z + 1;
        return z_modified;
    }
    public static void main(String[] args) {
        // TODO code application logic here
    }
}

public class probTESTswing extends javax.swing.JFrame {

    public probTESTswing() {
        initComponents();
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        jLabel1.setText("done");         
        jLabel2.setText(mystring2);        
        jLabel3.setText(ns.change_add_MOD(mystring3));
    }

    public static void main(String args[]) {        

        Number_String ns=null;
        String first_str="myFIRSTstring";        
        String mystring2 = ns.change_add_MOD(first_str);
        String mystring3 = "third_str";

        probTESTswing testing= new probTESTswing();
        testing.setVisible(true);
    }
}

Greetings to everyone who made effort to get here;)

So lemme briefly explain:

Main class is the "swing gui" where in main() function i initialize some variables using second class, now i want to show result of "processing" those variables with my Number_String class, however Java refuses to cooperate as my programming skills lack some fundamental knowledge as you probably noticed.

You can observe that i want to do "it" in both ways: where inside jButton1ActionPerformed i want to access function from my Number_String class (with use of 'mystring3'), and also the other way where i'd like to access just previously defined variable(mystring2)

ANY HELP GREATLY APPRECIATED.

please enlight me, i'd like to solve my problem "efficiently" in a good programming manner, in order to learn on efficient, optimal, proper and fast solutions, however im desperate for help so any advice is greatly appreciated.

Greetings

Upvotes: 1

Views: 5206

Answers (2)

MirroredFate
MirroredFate

Reputation: 12826

JButtons, like almost all swing components, have a whole lot of things called "ActionListeners". These ActionListeners are classes that implement an interface called... ActionListener! They have a method called actionPerformed. Every time the button is clicked, he will tell each of his ActionListeners to call their actionPerformed functions. So, you give your button an ActionListener, and then make its actionPerformed method make all the changes you want.

Its like if every time your doorbell rang, you (as the JButton) would go make phone calls to all those despairing relatives who think no one ever goes to visit you. In fact, they tell you they will pay you money every time your doorbell rings. So the doorbell rings (clicking on the button), and you (the JButton) call your relatives (the ActionListeners) who then each pay you money (actionPerformed method).

Example in Code:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;


public class Test implements ActionListener{
    int count = 0;
    public static void main(String[] args){
        JFrame frame = new JFrame();
        JButton button = new JButton();

        button.addActionListener(new Test());
        button.setText("I have been clicked 0 times");

        frame.setSize(200, 50);
        frame.add(button);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        JButton button = (JButton) (arg0.getSource());
        count++;
        button.setText("I have been clicked "+count+" times");
    }


}

Upvotes: 0

trashgod
trashgod

Reputation: 205785

One approach is to separate the data model from the view using the Model–View–Controller pattern, discussed here.

Addendum: More simply, you may just need to give class ProbTestSwing a suitable member variable:

private Number_String ns = new Number_String();

Upvotes: 2

Related Questions