I want to update 0X0 when I press of the "AC Milan" to be "1X0"
and the last scorer also to be updated with the winner;

Also, how to fix the alligenment to the center >>!

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

public class TestSwingListeners1 {
	    public static void main(String[] args) {
        JFrame fr1 = new JFrame("Swing Window");
        Container cp;
       	JButton bt1;
		JButton bt2;
  		int cnt1 = 0;
  		int cnt2 = 0;
  		String scr = null;
  		String wnr = null;
        JButton btOK, btCancel;
		fr1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        fr1.setSize(300, 200);
        fr1.setResizable(false);
        cp = fr1.getContentPane();
        cp.setLayout(new GridLayout(5,1));
	    btOK = new JButton("AC Milan");
        btCancel = new JButton("Real Madrid");
        JLabel lbl1 = new JLabel("Result: " + cnt1 + "X" + cnt2);
		JLabel lbl2 = new JLabel("Last Scorer: " + scr);
		JLabel lbl3 = new JLabel("Winner: " + wnr);
		cp.add(btOK);
        cp.add(btCancel);
		cp.add(lbl1);
		cp.add(lbl2);
		cp.add(lbl3);


		btOK.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae) {

			}
		});

		btCancel.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae) {

			}
		});
        fr1.show(); // Deprecated method!



		  }
    }

Recommended Answers

All 18 Replies

In your btOK ActionListener just create a String with the desired text in it and use the label's setText method to update it with that String.

Use setHorizontalAlignment to centre a JLabel's contents inside the JLabel. To centre the JLabel in the window use a Layout Manager (see previous post).

The full documentation is, as always, in the API JavaDoc.

[Re: Updating a JLabel
In your btOK ActionListener just create a String with the desired text in it and use the label's setText method to update it with that String.

Use setHorizontalAlignment to centre a JLabel's contents inside the JLabel. To centre the JLabel in the window use a Layout Manager (see previous post).

The full documentation is, as always, in the API JavaDoc. ]

Sorry sir I didn`t understand you about making string inside the listener,,
I want to change an integer..!!

Sorry sir I didn`t understand you about making string inside the listener,,
I want to change an integer..!!

Yes, yo do want to display an integer, but you want to make it a string

String displayText = "" + number;

and then you want to set that to your JLabel using setText ();

or you can add the integer to a string in the setText();

The listener is there you update your text whenever the desired action is performed.

The purpose of button is to increment the counter,,
your method will make the change static isnt it ?
or it looks like I misunderstand it ^_

you will have to use the static modifier in order to use the variable in the static method. The way I solved this was to move your changing variables out of the main method and instead write this

private static int cnt1;

and then I initialized it in the main. Then you were able to increment the counter using

cnt1 += 1, or cnt1 ++;

Then you set the text and then it updates the label.

btOK = new JButton("AC Milan");
        btCancel = new JButton("Real Madrid");
        JLabel lbl1 = new JLabel("Result: " + cnt1 + "X" + cnt2);
		JLabel lbl2 = new JLabel("Last Scorer: " + scr);
		JLabel lbl3 = new JLabel("Winner: " + wnr);
		
		cp.add(btOK);
        cp.add(btCancel);
		cp.add(lbl1);
		cp.add(lbl2);
		cp.add(lbl3);
		btOK.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae) {
			String displayText = "" + 1;
			
			}
		});

Sorry but I`m confused..
Can you show me how to setText here ??
and Thanks for help ^

lbl2.setText("");

would you please restate steps for me !!
I tried these but it has some conflicst

//when button clicks code then
lbl2.setText(""); // Set Jlabel Title

Please post the errors

It`s fine my friend I did it ^^ thank you all

Here the rules are pretty strict , please mark thread solved so others can get advantage of this. :)

This is the final edit and fixed

btOK.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae) {
			cnt1++;	
			lbl1.setText("Result: " + cnt1 + "X" + cnt2);
			lbl2.setText("Last Scorer: AC Milan");
			
			if(cnt1>cnt2){
				lbl3.setText("Winner: AC Milan");
				}
			else if(cnt1<cnt2){
				lbl3.setText("Winner: Real Madrid");
				}
			else if(cnt1 == cnt2){
				lbl3.setText("Winner: Draw");
				}
			}
		});

Excuse me, but how to mark ir as solved ?

At the bottom of the thread you should see a heading
Has this thread been answered?
just under that is a link for "Mark this Thread as Solved". Just click it.

Move your changing variables outside of your main method

from this:

public static void main (String [] args){
     int cnt1 = 0;
}

to

private static int cnt1; // makes it only visible in this class, and makes it static
public static void main (String [] args){
     cnt1 = 0; // adds value to variable
}

*note: it has to be static in order to use it in a static context

then you want to increment it in the listener

just add this code in for the correct listener

cnt1++; or cnt1 += 1;

this will change the value of the variable by 1.

Then in the listener you want to "refresh" the text by reprinting it using the setText(); on the JLabel. you will then insert the text you want printed into the brackets.

Didn't notice the second page lol. Please mark it as solved as the previous poster suggested.

This should do what you asked, but without the alignment

import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class TheMain {
    	
    	private static JButton btOK;
		private static JButton btCancel;
		private static int cnt1 = 0, cnt2 = 0;
    	private static JLabel lbl1, lbl2, lbl3;
    	private static String scr, wnr;
    	private static JPanel results;
		
    public static void main(String[] args) {
    JFrame fr1 = new JFrame("Swing Window");
    Container cp;
    
    JButton bt1; // NEVER USED
    JButton bt2; // NEVER USED
    
    scr = null;
    wnr = null;
    
    fr1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    fr1.setSize(300, 200);
    fr1.setResizable(false);
    cp = fr1.getContentPane();
    cp.setLayout(new GridLayout(5,1));
    
    btOK = new JButton("AC Milan");
    btCancel = new JButton("Real Madrid");
    lbl1 = new JLabel("Result: " + cnt1 + "X" + cnt2);
    lbl2 = new JLabel("Last Scorer: " + scr);
    lbl3 = new JLabel("Winner: " + wnr);
    
    cp.add(btOK);
    cp.add(btCancel);
    cp.add(lbl1);
    cp.add(lbl2);
    cp.add(lbl3);
     
     
    btOK.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent ae) {
    	
     if(ae.getSource() == btOK){
    	 cnt1++;
    	 lbl1.setText("Result: " + cnt1 + "X" + cnt2);
    	 
    	 scr = btOK.getText();
    	 lbl2.setText("Last Scorer: " + scr);
     }
    }
    });
     
    btCancel.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent ae) {
     
    }
    });
    fr1.show(); // Deprecated method!
     
     
     
    }
    }

setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.