943,793 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 699
  • Java RSS
Nov 11th, 2008
0

JFrame & SpringLayout problem.

Expand Post »
Hi all,

I have a simple JFrame windows with two JLabel, two JTextField and two JButton. I use SpringLayout to display controls in on the frame. But when I run the application, the windows is displayed in a very small size. Only the two buttons are displayed. But when I maximize the window all the controls are placed properly.

So, I think, the setSize() is not functioning properly. Can someone please tell me what i am doing wrong? Appreciate your help. Here's the code for this:

java Syntax (Toggle Plain Text)
  1. public class Login extends JFrame implements ActionListener {
  2. public Login () {
  3. super("Title");
  4. this.getContentPane().setLayout(new BorderLayout());
  5.  
  6. JPanel btnPane = new JPanel();
  7. btnPane.setLayout(new FlowLayout());
  8.  
  9. JPanel pane = new JPanel();
  10. SpringLayout sp = new SpringLayout();
  11. pane.setLayout(sp);
  12.  
  13. pane.add(lblUserName);
  14. pane.add(txtUserName);
  15. pane.add(lblPassword);
  16. pane.add(txtPassword);
  17.  
  18. sp.putConstraint(SpringLayout.WEST, lblUserName, 70, SpringLayout.WEST, this);
  19. sp.putConstraint(SpringLayout.NORTH, lblUserName, 20, SpringLayout.NORTH, this);
  20.  
  21. sp.putConstraint(SpringLayout.WEST, txtUserName, 5, SpringLayout.EAST, lblUserName);
  22. sp.putConstraint(SpringLayout.NORTH, txtUserName, 20, SpringLayout.NORTH, this);
  23.  
  24. sp.putConstraint(SpringLayout.WEST, lblPassword, 70, SpringLayout.WEST, this);
  25. sp.putConstraint(SpringLayout.NORTH, lblPassword, 5, SpringLayout.SOUTH, lblUserName);
  26.  
  27. sp.putConstraint(SpringLayout.WEST, txtPassword, 5, SpringLayout.EAST, lblPassword);
  28. sp.putConstraint(SpringLayout.NORTH, txtPassword, 5, SpringLayout.SOUTH, txtUserName);
  29.  
  30. btnLogin.addActionListener(this);
  31. btnCancel.addActionListener(this);
  32.  
  33. btnPane.add(btnLogin);
  34. btnPane.add(btnCancel);
  35.  
  36. this.getContentPane().add("Center", pane);
  37. this.getContentPane().add("South",btnPane);
  38.  
  39. }
  40. public static void main (String[] args) {
  41. Login login = new Login();
  42. login.setSize(10, 20);
  43. login.setLocation(200,200);
  44. login.pack();
  45. login.setVisible(true);
  46. }
  47. public void actionPerformed(ActionEvent e) {
  48. // do some actions
  49. }
  50. private JLabel lblUserName = new JLabel("User name: ");
  51. private JTextField txtUserName = new JTextField(15);
  52. private JLabel lblPassword = new JLabel("Password : ");
  53. private JPasswordField txtPassword = new JPasswordField(15);
  54.  
  55. private JButton btnLogin = new JButton("Login");
  56. private JButton btnCancel = new JButton("Cancel");
  57. }

Thanks in advance...
Reputation Points: 7
Solved Threads: 6
Junior Poster
new_2_java is offline Offline
127 posts
since Apr 2007
Nov 11th, 2008
0

Re: JFrame & SpringLayout problem.

Try using bigger numbers in set size. 10x20 pixels will give you a really small window.
Reputation Points: 10
Solved Threads: 6
Newbie Poster
bionicseraph is offline Offline
19 posts
since Nov 2008
Nov 11th, 2008
0

Re: JFrame & SpringLayout problem.

That's what I thaught. But, no matter which number I try, I get the same size. I tried with setSize(200, 300); still the same size. So, I am thinking, I may be doing something wrong.

Please advise...
Reputation Points: 7
Solved Threads: 6
Junior Poster
new_2_java is offline Offline
127 posts
since Apr 2007
Nov 12th, 2008
0

Re: JFrame & SpringLayout problem.

Get rid of the call to pack(). pack() resizes the components based on the preferred size of each component. So either setPreferredSize() and use pack, or leave your code like it is and do not use pack.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: 2d array problem
Next Thread in Java Forum Timeline: 2d arrayist





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC