Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JPanel.validate() repositions DnD Components  RSS feed

 
Praveen B
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all..
I have a piece of code that adds JLabels to a JPanel dynamically.
That is:
The Panel is a Drop Target and dnd handles "moving the JLabels around"
1. I Click a button a JLabel is created
2. JPanel.add(JLabel) is called and the JLabel is added to the JPanel, but the JLabel still doesnot show.
3. I call JPanel.validate() and the JLabel shows up on the panel.
3. I drag the label to a new position. The JLabel is moved to the new position using JLabel.setBounds(newX, newY, width, height)

So far so good.
Now, I add another JLabel by clicking the button again. I call the JPanel.validate() method so that the second JLabel also shows up. The second JLabel DOES show up - but the first JLabel is repositioned to its original location. (if I dont call the JPanel.validate() method the second time, the second JLabel doesnot show up, and the first JLabel is NOT repositioned either)

I tried JLabel.setLocation(newX, newY) instead of JLabel.setBounds(newX, newY, width, height) also. But it doesnt seem to make things any better.
Any help on this will be greatly appreciated. please feel free to contact me directly at:
praveen.balaji@iflexsolutions.com

here's a code snip, if that helps. If you need more code (example, how I do my Dnd etc, please do ask):
private int prntRunningCounter=1;
====================
private void loadComponent()
{
String lstrTitle = �Untitled�+ (prntRunningCounter++);
JLabel lObjTask = new JLabel(lstrTitle);
this.add(lObjTask, new XYConstraints(100, 100, 100, 100));
//If I put the condition that I call this.validate() only
//if the title is �Untitled1� and not when it is �Untitled2�
//the second Jlabel(�Untitled2�) doesnot show up. If I don�t
//put this restriction, �Untitled2� shows up but �Untitled1�
//is reset to its original position
this.validate();
}
=======================

Thanks for the help,
praveen.
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Praveen,
You must use a null layout to make your stuff work correctly. With a null layout you are required to call setBounds to specify size and location in order for the component to show up in the container. By your explanation, it sounds like you are not using a null layout.
When your button is first created call setBounds and then call validate. Do this for all your buttons and it should work good.
Regards,
Manfred.
 
Praveen B
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply Manfred. I figured that out (as you can see from the duplicate post). Indeed, I have been using an XYLayout (jbcl). I shifted to null layout and everything works well.
However, there is one uery, if you could guide me on: I learnt that using a null layout on swing (or otherwise) has undesirable results when the screen resolution changes. How far is this true and how can I avoid this?
Thanks for the help on this. That was very kind of you.

Praveen.
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Praveen,
The only way to really avoid it is to listen for componentResize event and resize all the visible components according to the new size. The resizing issue is not really a very big problem for your program (as you have described it).
Regards,
Manfred.
 
inam paki
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Praveen
To avoid resizing effect in Your code You should
take care of Screen resolution and create your components relative to The current Screen size with the help of Toolkit().
get the Toolkit Object n then use its ScreenSize()
method to determine the current Screen Size.
Now by dividing Screen width and screen height attributes you can create your GUI very Flexible for any Screen resolution.
Regards
 
Praveen B
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Inam and Manfred,
Sorry bout the delay responding. For a short while, I was "forced" to forget about this query of mine.
Thanks for the solutions.
I find Inam's solution making my problem, particularly, easy to handle. I need to qualify my applets on several browsers and on different resolution. I shall keep both your suggestions in mind.
Thanks and kind Regards,
praveen.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!