473,385 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to clear Jlabel ?

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
2 25401
r035198x
13,262 8TB
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
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

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

Similar topics

1
by: prabhat | last post by:
Hi, I am getting this weird behavior. I have couple of gif and jpg files that I would like to display in JLabel. It displays one jpg image but it does not display other gifs and jpgs. Any idea why...
1
by: Phil... | last post by:
I have the following code to put a label on a tabbed panel. It does not set the background though. If I put in a "slabel.getBackground()" it returns the color I chose for the set. It is like the...
0
by: Mike | last post by:
Hello, Is there a way of setting text for a JLabel after a certain amount of empty characters. For example, I want to avoid doing this JLabel label = new JLabel(); label.setText(" ...
5
Shinzon
by: Shinzon | last post by:
ok so far I have got: JFrame frame = new JFrame( "Matt's DVD's" ); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); frame.setSize( 420, 170 ); // set frame size ...
5
Shinzon
by: Shinzon | last post by:
Ok so I have been working on this for a moment now and wondering how to add multiple jlabels to a jframe. the code looks like this: JLabel jl = new JLabel("DVD ID #= " + dvd); //Displays DVD...
3
nanhiPari
by: nanhiPari | last post by:
hello everyone i need to create DYNAMIC JLABElS(say 5)..yes i know it sounds easy but i also have to give them Events n then want to refer each of them when ever Mouse is Clicked on a Specific...
1
by: tommyny04 | last post by:
I have a method addPlayer that's supposed to add a JLabel to a JPanel and update the JPanel. The JLabel is just a label with a player's name in it. The code I've wrote doesn't work and I'm not sure...
6
by: thesti | last post by:
hi, i have a JLabel inside a JPanel of which layout manager is BoxLayout. i will display an image with the JLabel at runtime, when the application first start, it will appear as an empty box...
2
by: Coreyja | last post by:
Im trying to display a png image by setting it as a ImageIcon and then putting that in a JLabel. I cant get it to display the image. I am using a null layout as it is the simplest way for me to get...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.