Rhyno_xD
Rhyno_xD

Reputation: 13

issues with JLabel setText()

I'm having some issues with Jlabel and the setText() method. Normally the setText() is supposed to clear previous text and replace with new one on button click but instead the text piles on each other.

    public class JFrameDemo extends JFrame{
    private JButton clearButton;
    private JLabel display;
    private JButton exitButton;
    private JButton go;
    private JTextField nameField;
    private JLabel name;

    public JFrameDemo(){
        doEnglish();
    }

    private void doEnglish(){
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(null);        

        name = new JLabel("Enter Name");
        getContentPane().add(name);
        name.setBounds(5,30,300,30);        
        nameField = new JTextField();
        getContentPane().add(nameField);    
        nameField.setBounds(80,33,150,25);        

        go = new JButton("Go");
        getContentPane().add(go);
        go.setBounds(5, 65, 50, 30);
        go.addActionListener(new ActionListener() {                        
            @Override
            public void actionPerformed(ActionEvent e) {
                goButtonActionPerformed();
            }
        });
    }

    private void goButtonActionPerformed(){
        String na = nameField.getText();
                display = new JLabel();                               
                display.setText(na);
                getContentPane().add(display);
                display.setBounds(85,110,400,20);                
    }

    public static void main(String args[]){
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new JFrameDemo().setVisible(true);
            }
        });
    }
   }

Upvotes: 0

Views: 146

Answers (2)

Jens
Jens

Reputation: 69495

Every Time you press the add Button you add a new JLabel to your contentPane. So change your code to :

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;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

        public class JFrameDemo extends JFrame{
        private JButton clearButton;
        private JLabel display = new JLabel();
        private JButton exitButton;
        private JButton go;
        private JTextField nameField;
        private JLabel name;

        public JFrameDemo(){
            doEnglish();
        }

        private void doEnglish(){
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            getContentPane().setLayout(null);        

            name = new JLabel("Enter Name");
            getContentPane().add(name);
            name.setBounds(5,30,300,30);        
            nameField = new JTextField();
            getContentPane().add(nameField);    
            nameField.setBounds(80,33,150,25);        

            go = new JButton("Go");
            getContentPane().add(go);
            go.setBounds(5, 65, 50, 30);

            getContentPane().add(display);
            display.setBounds(85,110,400,20);                

            go.addActionListener(new ActionListener() {                        
                @Override
                public void actionPerformed(ActionEvent e) {
                    goButtonActionPerformed();
                }
            });
        }

        private void goButtonActionPerformed(){
            String na = nameField.getText();
                    //display = new JLabel();                               
                    display.setText(na);
        }

        public static void main(String args[]){
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new JFrameDemo().setVisible(true);
                }
            });
        }
       }

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347334

  1. You're using null layouts
  2. You're creating a new instance of JLabel and then adding it to the content pane...not surprising really

Avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify

And take a look at What is a Null Pointer Exception, and how do I fix it? for more details...

Instead, you should check the state of the display label and create only when you need to...

private void goButtonActionPerformed(){
    String na = nameField.getText();
    if (display == null) {
        display = new JLabel(); 
        getContentPane().add(display);
        // Get rid of this as soon as you can...
        display.setBounds(85,110,400,20);                
    }
    display.setText(na);
}

Upvotes: 1

Related Questions