By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,506 Members | 1,891 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,506 IT Pros & Developers. It's quick & easy.

How to clear Jlabel ?

P: 80
Hi

in this code when i clicked login button...it shows output as login...
But when i clicked each time its repeating .. i want to see that only ones ...
I want to clear the previous login before i clicked...

Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. class LabelClear
  6. {
  7. static JLabel label,clear;
  8. static JFrame frame;
  9. static JButton login;
  10. static JPanel panel;
  11.  
  12. public static void main(String args[])
  13.     {
  14.     frame = new JFrame();
  15.  
  16.     login = new JButton("Login");
  17.  
  18.     panel = new JPanel();
  19.     //panel.add(label);
  20.     panel.add(login);    
  21.  
  22.  
  23.     frame.getContentPane().add(panel);
  24.     frame.setVisible(true);
  25.     frame.setDefaultCloseOperation(3);
  26.     frame.setSize(300,200);    
  27.  
  28.     Listener obj = new Listener();
  29.     login.addActionListener(obj);
  30.     }
  31. static class Listener implements ActionListener
  32.     {
  33.     public void actionPerformed(ActionEvent evt)
  34.         {
  35.         Object obj = evt.getSource();
  36.         if(obj == login)
  37.             {
  38.             label = new JLabel("Login");
  39.             clear = new JLabel(" ");
  40.             panel.add(clear);
  41.             panel.revalidate();
  42.             panel.add(label);
  43.             panel.revalidate();
  44.             }
  45.         }
  46.     }
  47. }
  48.  
Apr 30 '07 #1
Share this Question
Share on Google+
2 Replies


10K+
P: 13,264
Hi

in this code when i clicked login button...it shows output as login...
But when i clicked each time its repeating .. i want to see that only ones ...
I want to clear the previous login before i clicked...

Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. class LabelClear
  6. {
  7. static JLabel label,clear;
  8. static JFrame frame;
  9. static JButton login;
  10. static JPanel panel;
  11.  
  12. public static void main(String args[])
  13.     {
  14.     frame = new JFrame();
  15.  
  16.     login = new JButton("Login");
  17.  
  18.     panel = new JPanel();
  19.     //panel.add(label);
  20.     panel.add(login);    
  21.  
  22.  
  23.     frame.getContentPane().add(panel);
  24.     frame.setVisible(true);
  25.     frame.setDefaultCloseOperation(3);
  26.     frame.setSize(300,200);    
  27.  
  28.     Listener obj = new Listener();
  29.     login.addActionListener(obj);
  30.     }
  31. static class Listener implements ActionListener
  32.     {
  33.     public void actionPerformed(ActionEvent evt)
  34.         {
  35.         Object obj = evt.getSource();
  36.         if(obj == login)
  37.             {
  38.             label = new JLabel("Login");
  39.             clear = new JLabel(" ");
  40.             panel.add(clear);
  41.             panel.revalidate();
  42.             panel.add(label);
  43.             panel.revalidate();
  44.             }
  45.         }
  46.     }
  47. }
  48.  
1.)What is the use of the label called clear in your code?
2.)label.setText(""); clears the value of a label to the empty string.
May 1 '07 #2

P: 1
panel.add(clear) will create another label with its text as "".
SO, you must setText the previous label as"".
If you get problem in showing number of JLabels on refreshing the screen , then you must use panel.removeAllChildComponents().
May 1 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.