Hello all,

I m a newbie in java,
I m writing code to change the text of the JLabel on the mouse over to the JButton

I m facing some problem in Updating

Initial Label:

After setText:

label.setText();

It doesn't update properly
New text gets overwritten on old text
I tried repaint, validate methods but not working on it


But when i minimize the application and again restore itAfter minimize and restore:

the text displays correctly on the screen

Is there any mistake or I m going wrong with JLabel???

Thanks,
Kaushil Rakhasiya
Think Digital

That's very strange.
Do you overide any of the paint methods for any of your graphical objects?
What version of Java are you using?

That's very strange.

Do you overide any of the paint methods for any of your graphical objects?

I am using paintComponent(Graphics g) method for JPanel

What version of Java are you using?

I am using JAVA 1.6 UPDATE 16

Here is the output of JAVA -version

java version "1.6.0_16"
Java(TM) SE Runtime Environment (build 1.6.0_16-b01)
Java HotSpot(TM) Client VM (build 14.2-b01, mixed mode, sharing)

Showing your code might help. Mainly for the overridden JPanel (which I do not know why you are doing that if you are simply adding elements to it), and for the "text change" on the button.

Better would be if you took the time write a very small, compilable, and executable, program that duplicates the problem. Successfully doing this will, many times, will present the solution to you in and of itself.

Better would be if you took the time write a very small, compilable, and executable, program that duplicates the problem. Successfully doing this will, many times, will present the solution to you in and of itself.

Thank u for the reply
I will try this an come back !

Better would be if you took the time write a very small, compilable, and executable, program that duplicates the problem. Successfully doing this will, many times, will present the solution to you in and of itself.

I have done this
And I found no problem in that

And then after I have tried to run without paintComponent

It worked fine !!
So the Problem is with the Overridden paintComponent

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

public class BottomBar extends JPanel{
	
	myButton sketches = new myButton("Sketches");
	myButton docs = new myButton("Documents");
	myButton chatBt = new myButton("Hide Chat");
	JLabel Desc = new JLabel("Description Here");
	Dimension size = new Dimension(120,30);

	BottomBar(){

		sketches.setPreferredSize(size);
		docs.setPreferredSize(size);
		chatBt.setPreferredSize(size);
		this.setLayout(new BorderLayout());
		Desc.setHorizontalAlignment(JLabel.LEFT);
		Box horizontalBox,endBox;
		horizontalBox = Box.createHorizontalBox();
		horizontalBox.add(sketches);
		horizontalBox.add(docs);
		horizontalBox.add(Box.createRigidArea(new Dimension(20,0)));
		horizontalBox.add(Desc);
		horizontalBox.add(Box.createGlue());
		endBox = Box.createHorizontalBox();
		endBox.add(Box.createGlue());
		endBox.add(chatBt);


		this.add(horizontalBox,BorderLayout.LINE_START);
		this.add(endBox,BorderLayout.LINE_END);
		//this.add(chatBt,BorderLayout.LINE_END);
	}
	public void paintComponent(Graphics g) {
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		g.drawLine(1,1,screenSize.width,1);
	}

}

This is the class file on which description is displayed
When I remove the paintComponent method it works fine

What should i do now ?

What are you trying to do with that override? Simply draw a line along the top of the panel? If that is the case I would write my own border class. The easiest way to do that one would be to copy LineBorder (and name it differently, of course) out of src.zip and modify that to simply draw a single line on a single side rather than drawing a rectangle.

If you want to keep your paiintComponent then it should start by clearing its whole visible region to the background color. I think your problem is that you don't clear it, so the old contents are not erased.

What are you trying to do with that override? Simply draw a line along the top of the panel? If that is the case I would write my own border class. The easiest way to do that one would be to copy LineBorder (and name it differently, of course) out of src.zip and modify that to simply draw a single line on a single side rather than drawing a rectangle.

Thanks
You gave me new Idea of border

I was unaware of that

i m using

this.setBorder(BorderFactory.createLineBorder(Color.black));
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.