Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 27th, 2006, 11:06 PM   #1
melbolt
Hobbyist Programmer
 
melbolt's Avatar
 
Join Date: Feb 2005
Location: PA, USA
Posts: 237
Rep Power: 4 melbolt is on a distinguished road
Send a message via AIM to melbolt Send a message via Yahoo to melbolt
need help with layouts

I'm using a box layout, but I just can't seem to get my components to line up the way I want them to, been fighting with these for awhile, thought maybe someone here could lend me a hand.

Basically I'm making an extension to this little piece of software that allows sending of email messages, I've already got the back-end built which was easy, I find myself struggling more with the GUI than the actual back-end, which is just wrong lol. I've only been working in Java for 2 days now, but I have experience with other similar languages.

here is what I have for my GUI so far.

java Syntax (Toggle Plain Text)
  1. package networkgui;
  2.  
  3. import javax.swing.*;
  4.  
  5. import java.awt.*;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import javax.swing.border.Border;
  9. import javax.swing.event.*;
  10.  
  11.  
  12. public class EmailForm extends javax.swing.JFrame{
  13.  
  14. // Variables declaration///////////////////
  15. private javax.swing.JButton btnSend;
  16. private javax.swing.JCheckBox cbAuthenticated;
  17. private javax.swing.JPasswordField txtPassword;
  18. private javax.swing.JScrollPane spRecipients;
  19. private javax.swing.JScrollPane spBody;
  20. private javax.swing.JTextArea taRecipients;
  21. private javax.swing.JTextArea taBody;
  22. private javax.swing.JTextField txtSMTP;
  23. private javax.swing.JTextField txtFrom;
  24. private javax.swing.JTextField txtUserName;
  25. private javax.swing.JTextField txtSubject;
  26. private javax.swing.JLabel lblSMTP;
  27. private javax.swing.JLabel lblFrom;
  28. private javax.swing.JLabel lblRecipients;
  29. private javax.swing.JLabel lblSubject;
  30. private javax.swing.JLabel lblUserName;
  31. private javax.swing.JLabel lblPassword;
  32. private JPanel MyPanel = new JPanel();
  33. private JPanel AuthenticationPanel = new JPanel();
  34. private JPanel EmailPanel = new JPanel();
  35. // End of variables declaration/////////////
  36.  
  37.  
  38. //constructor
  39. public EmailForm(){
  40. //init all controls
  41. initComponents();
  42. }
  43.  
  44.  
  45. private void initComponents(){
  46.  
  47. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  48.  
  49.  
  50. //Initialize containers/////////////////////////////
  51. spBody = new javax.swing.JScrollPane();
  52. taBody = new javax.swing.JTextArea();
  53. lblSubject = new javax.swing.JLabel();
  54. lblRecipients = new javax.swing.JLabel();
  55. txtUserName = new javax.swing.JTextField();
  56. txtSMTP = new javax.swing.JTextField();
  57. lblSMTP = new javax.swing.JLabel();
  58. lblFrom = new javax.swing.JLabel();
  59. spRecipients = new javax.swing.JScrollPane();
  60. taRecipients = new javax.swing.JTextArea();
  61. txtFrom = new javax.swing.JTextField();
  62. txtSubject = new javax.swing.JTextField();
  63. lblUserName = new javax.swing.JLabel();
  64. cbAuthenticated = new javax.swing.JCheckBox();
  65. lblPassword = new javax.swing.JLabel();
  66. txtPassword = new javax.swing.JPasswordField();
  67. btnSend = new javax.swing.JButton();
  68. //end initialize containers////////////////////////
  69.  
  70.  
  71. //Set Properties for containers////////////////////////////////////////////////////
  72. spBody.setViewportView(taBody);
  73. lblSubject.setFont(new java.awt.Font("Tahoma", 1, 12));
  74. lblSubject.setText("Subject:");
  75. lblRecipients.setFont(new java.awt.Font("Tahoma", 1, 12));
  76. lblRecipients.setText("Recipients:");
  77. lblSMTP.setFont(new java.awt.Font("Tahoma", 1, 12));
  78. lblSMTP.setText("SMTP:");
  79. lblFrom.setFont(new java.awt.Font("Tahoma", 1, 12));
  80. lblFrom.setText("From:");
  81. taRecipients.setColumns(20);
  82. taRecipients.setRows(5);
  83. spRecipients.setViewportView(taRecipients);
  84. lblUserName.setFont(new java.awt.Font("Tahoma", 1, 12));
  85. lblUserName.setText("User Name:");
  86. cbAuthenticated.setText("Authenticated");
  87. cbAuthenticated.setMargin(new java.awt.Insets(0, 0, 0, 0));
  88.  
  89. lblPassword.setFont(new java.awt.Font("Tahoma", 1, 12));
  90. lblPassword.setText("Password:");
  91. btnSend.setText("Send Message");
  92.  
  93. txtFrom.setPreferredSize(new Dimension(200, 19));
  94. spBody.setPreferredSize(new Dimension(500, 200));
  95. txtUserName.setPreferredSize(new Dimension(11, 19));
  96. txtSMTP.setPreferredSize(new Dimension(200, 19));
  97. spRecipients.setPreferredSize(new Dimension(430, 30));
  98. txtSubject.setPreferredSize(new Dimension(200, 19));
  99. //cbAuthenticated.setPreferredSize(new Dimension(250, 50));
  100. txtPassword.setPreferredSize(new Dimension(200, 19));
  101. btnSend.setPreferredSize(new Dimension(250, 50));
  102. //End Set Properties for containers//////////////////////////////////////////////////
  103.  
  104.  
  105. //set up events//////////////////////////////////////////////////////////////////////
  106. cbAuthenticated.addItemListener(new java.awt.event.ItemListener() {
  107. public void itemStateChanged(java.awt.event.ItemEvent evt) {
  108. cbAuthenticatedStateChanged(evt);
  109. }
  110. });
  111.  
  112.  
  113. Container VerticalBox = Box.createVerticalBox();
  114. Container VerticalBox2 = Box.createVerticalBox();
  115. Container VerticalBox3 = Box.createVerticalBox();
  116.  
  117. Border etched = BorderFactory.createEtchedBorder();
  118.  
  119.  
  120. VerticalBox2.add(cbAuthenticated);
  121. VerticalBox2.add(lblUserName);
  122. VerticalBox2.add(txtUserName);
  123. VerticalBox2.add(lblPassword);
  124. VerticalBox2.add(txtPassword);
  125.  
  126. lblUserName.setEnabled(false);
  127. txtUserName.setEnabled(false);
  128. lblPassword.setEnabled(false);
  129. txtPassword.setEnabled(false);
  130.  
  131. VerticalBox2.add(new JLabel(" "));
  132. AuthenticationPanel.add(VerticalBox2);
  133. AuthenticationPanel.setBorder(BorderFactory.createTitledBorder(etched, ""));
  134.  
  135. VerticalBox.add(AuthenticationPanel);
  136. VerticalBox.add(lblSMTP);
  137. VerticalBox.add(txtSMTP);
  138. VerticalBox.add(lblFrom);
  139. VerticalBox.add(txtFrom);
  140. VerticalBox.add(lblRecipients);
  141. VerticalBox.add(spRecipients);
  142. VerticalBox.add(lblSubject);
  143. VerticalBox.add(txtSubject);
  144. VerticalBox.add(new JLabel(" "));
  145. VerticalBox.add(spBody);
  146. VerticalBox.add(new JLabel(" "));
  147. VerticalBox.add(btnSend);
  148. EmailPanel.add(VerticalBox);
  149. EmailPanel.setBorder(BorderFactory.createTitledBorder(etched, ""));
  150.  
  151.  
  152. //add box layout to panel
  153. VerticalBox3.add(AuthenticationPanel);
  154. VerticalBox3.add(EmailPanel);
  155.  
  156. MyPanel.add(VerticalBox3);
  157.  
  158. //add panel to the frame's contentpane
  159. this.setContentPane(MyPanel);
  160.  
  161. this.pack();
  162.  
  163.  
  164. }
  165.  
  166.  
  167. //Checkbox handler
  168. private void cbAuthenticatedStateChanged(java.awt.event.ItemEvent evt) {
  169. if (cbAuthenticated.isSelected()){
  170. lblUserName.setEnabled(true);
  171. txtUserName.setEnabled(true);
  172. lblPassword.setEnabled(true);
  173. txtPassword.setEnabled(true);
  174. }
  175. else{
  176. lblUserName.setEnabled(false);
  177. txtUserName.setEnabled(false);
  178. lblPassword.setEnabled(false);
  179. txtPassword.setEnabled(false);
  180. }
  181. }
  182. }


ok, so as you can see, I decided to go with the box layoutmanager, and i am adding several panels to this box, each containing multiple components(or controls).

the problem is... the labels want to indent for some reason and the top panel gets centered while the lower panel is fine.

I would like to have everything left aligned..

here is a screenshot of what it looks like when i run it.
http://www.putfile.com/pic.php?img=3823248

the text boxes in the bottom panel are aligned exactly the way i want them, however their corresponding labels are indenting for some reason, also, all of the components in the top panel are centered and i want them aligned the same way as the components in the bottom panel. also, the button at the bottom is indenting as well for some reason.

any idea what's going on with this indentation and how to get these things to the left side of the panel?

thanks alot guys.
__________________
I have never let my schooling interfere with my education. -Mark Twain-

Xbox live gamertag: melbolt
melbolt is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:51 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC