5 Replies - 3123 Views - Last Post: 16 January 2011 - 09:45 AM Rate Topic: -----

#1 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,689
  • Joined: 13-March 10

JFrame pack() method and ContentPane validate()

Posted 16 January 2011 - 08:17 AM

Hi i have a question. I have been trying to solve it for the past 2 hours and cannot get through it. So bascicallt i have a JFrame - lets call it "frame" - and a JButton - "button". When the user presses button i want to add a JLabel - "label" - to the contentPane. So i do the following.

//method called when button pressed
public void actionPerformed(ActionEvent event)
{
addLabel();
login();
}

public void addLabel()
{
JLabel labelToAdd = new JLabel("Please wait");
contentPane.add(labelToAdd,BorderLayout.SOUTH);
contentPane.validate();
frame.pack();
//JOptionPane.showMessageDialog(null,"Wait");
}

public void login()
{
//some login code
}



So from the code above addLabel() method should be preformed first. And it is perfoemd first, however the frame is being resized by the pack() method but the label does not appear in the SOUTH area since it goes straight to the login() code where it logins to the server. Howver, when i add showMessageDialog() method that is commented out everything works fine! The JLabel does appear where its supposed but i do not want to have to press Enter to get rid of JOptionPane so would rather have a way of omitting it.
So to make it clear, my problem is: pack() method works - frame getting larger due to the fact that JLabel to be added in SOUTH region BUT JLabel does not appear there as it goes quickly to login() method and the frame "freezes" since its talking to the server. When JOptionPAne used eveything is fine. Any way of solving it? Thanks for help.

Is This A Good Question/Topic? 0
  • +

Replies To: JFrame pack() method and ContentPane validate()

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2724
  • View blog
  • Posts: 11,456
  • Joined: 20-September 08

Re: JFrame pack() method and ContentPane validate()

Posted 16 January 2011 - 09:06 AM

That's not really the way you should handle GUIs. You should either set the caption on a label that was always there or show a different pane until you've logged in
Was This Post Helpful? 0
  • +
  • -

#3 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,689
  • Joined: 13-March 10

Re: JFrame pack() method and ContentPane validate()

Posted 16 January 2011 - 09:33 AM

I am showing a diffrenet JFrame when user logs in but logging in takes couple of seconds so when button is pressed i want to put a JLabel on THIS JFrame saying "Please wait". But label does not appear.
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2724
  • View blog
  • Posts: 11,456
  • Joined: 20-September 08

Re: JFrame pack() method and ContentPane validate()

Posted 16 January 2011 - 09:39 AM

What is this logging in? To what? Can you post the code for that?
Was This Post Helpful? 0
  • +
  • -

#5 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,689
  • Joined: 13-March 10

Re: JFrame pack() method and ContentPane validate()

Posted 16 January 2011 - 09:42 AM

Its loggin to the server. I would post it but its an assigment so would rather not. I will try to solve it on my own . Thanks for help anyway man.
Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2724
  • View blog
  • Posts: 11,456
  • Joined: 20-September 08

Re: JFrame pack() method and ContentPane validate()

Posted 16 January 2011 - 09:45 AM

I would use a SwingWorker:

a. Set the label to 'Logging in...'
b. Call the execute method of the SwingWorker to DO the logging in
c. Reset the label caption in SwingWorker.done
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1