简体   繁体   中英

Jlabel not updating when setText is called

While building a simple currency converter app setText is not setting the value in JLabel.(I am using eclipse ide in windows).I have called Action Listener for setting button and also had converted the getText to int with the help of parseInt.

My code is given below.

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;


public class CurrencyConverter implements ActionListener {
    JButton button1,button2;
    JLabel display2,display3;
    JTextField display;
    
    public CurrencyConverter() {
    var frame=new JFrame();
    frame.setLayout(null);
    frame.setBounds(300, 300, 400, 300);
    frame.getContentPane().setBackground(Color.BLACK);

    
    
    display=new JTextField();
    display.setBounds(50, 30, 300, 50);
    display.setBackground(Color.yellow);
    display.setForeground(Color.black);
    frame.add(display);
    
    
    
    button1=new JButton("TO INR");
    button1.setBounds(50, 100, 135, 50);
    frame.add(button1);
    
    button2=new JButton("TO USD");
    button2.setBounds(215, 100, 135, 50);
    frame.add(button2);
    
    display2=new JLabel();
    display2.setBounds(50, 170, 300, 50);
    display2.setBackground(Color.GREEN);
    display2.setForeground(Color.BLACK);
    display2.setOpaque(true);
    frame.add(display2);
    
    
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
public static void main(String[] args) {
    new CurrencyConverter();
}
@Override
public void actionPerformed(ActionEvent e) {
    if(e.getSource()==button1) {
        int noInDisplay=Integer.parseInt(display.getText())*70;
        display2.setText(""+noInDisplay);
    }
    else if(e.getSource()==button2) {
        int noInDisplay=Integer.parseInt(display.getText())/70;
        display2.setText(""+noInDisplay);
        }
}
}

The main issue why you are not seeing any visible updates on your JLabel is that the ActionListener is never added on the buttons, hence the actionPerformed() is never called.

To fix this, you should add these two lines in the CurrencyConverter() constructor, to add the ActionListener on your buttons:

button1.addActionListener(this);
button2.addActionListener(this);

Some sidenotes on your code:

  • It is generally not recommended to use null layout in swing. Have a look at the Visual Guide to Layout Managers for an overview on swing layout managers. Using a layout manager will do the work of sizing and laying out the components for you. So you don't have to manually set the size and bounds for each component yourself.

  • Instead of checking for the source of the action in actionPerformed() you could implement an anonymous ActionListener for each JButton . This way, you have a clear mapping between component and action. Eg

    button1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int noInDisplay = Integer.parseInt(display.getText()) * 70; display2.setText("" + noInDisplay); } });
  • Some kind of input validation should be considered in the next step.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM