943,985 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 431
  • Java RSS
Nov 10th, 2009
0

java app layout help

Expand Post »
hey guys, i was wondering if anyone could help, basicly at the moment (for the last few hours) iv been tring to find a way to align an container, to enable me to have my current work on one side (left or right) and a JTextArea on the other.

the project is a java address book, which needs to be able to do all the usual things such as edit and add ect. However it also needs to display read in data within the text area.

Any help would be greatly appreciated.
(btw this code is nowhere near finished, just seting up the GUI atm.)


Java Syntax (Toggle Plain Text)
  1. import java.util.*;
  2. import java.io.*;
  3. import javax.swing.*;
  4. import java.text.*;
  5.  
  6.  
  7.  
  8.  
  9.  
  10. public class Q3 implements ActionListener
  11. {
  12.  
  13. JFrame addressBook;
  14. JPanel panel1;
  15.  
  16.  
  17. JLabel labName, labAddress, labPhone,labMob;
  18. JTextField textName, textAddress, textPhone, textMob;
  19. JButton buttonSave, buttonDelete, buttonImport, buttonSearch, buttonForward, buttonBack, buttonExit;
  20. JTextArea text_area;
  21.  
  22.  
  23.  
  24. String name, address;
  25. int phone, mob;
  26. int recordNumber; // used to naviagate using >> and << buttons
  27. Container container1;
  28.  
  29.  
  30.  
  31.  
  32. public static void main(String args[])
  33.  
  34. {
  35. new Q3();
  36. }
  37.  
  38. public Q3()
  39. {
  40. name = "";
  41. address = "";
  42. phone = -1 ; //Stores 0 to indicate no Phone Number
  43. mob = -1;
  44. recordNumber = -1;
  45.  
  46. createGUI();
  47.  
  48. }
  49.  
  50. public void createGUI()
  51. {
  52.  
  53. /*Create a frame, get its contentpane and set layout*/
  54. addressBook = new JFrame("Address Book");
  55.  
  56. container1 = addressBook.getContentPane();
  57. container1.setLayout(new GridBagLayout());
  58. container1.setBackground(Color.black);
  59. container1.setSize(300, 300);
  60.  
  61.  
  62.  
  63.  
  64. //Arrange components on contentPane and set Action Listeners to each JButton
  65. arrangeComponents();
  66.  
  67. addressBook.setSize(600,300);
  68. addressBook.setResizable(false);
  69. addressBook.setVisible(true);
  70.  
  71.  
  72.  
  73. }
  74.  
  75. public void arrangeComponents()
  76. {
  77. text_area = new JTextArea();
  78.  
  79.  
  80. labName = new JLabel("Name");
  81. labName.setForeground(Color.white);
  82.  
  83. labAddress = new JLabel("Address");
  84. labAddress.setForeground(Color.white);
  85.  
  86. labPhone = new JLabel("Home Number");
  87. labPhone.setForeground(Color.white);
  88.  
  89. labMob = new JLabel("Mobile Number");
  90. labMob.setForeground(Color.white);
  91.  
  92. textName = new JTextField(20);
  93. textAddress = new JTextField(20);
  94. textPhone = new JTextField(20);
  95. textMob = new JTextField(20);
  96.  
  97. buttonSave = new JButton("Save");
  98. buttonDelete = new JButton("Delete");
  99. buttonSearch = new JButton("Search");
  100. buttonImport = new JButton("Import");
  101.  
  102. buttonForward = new JButton("FORWARD");
  103. buttonBack = new JButton("BACK");
  104. buttonExit = new JButton("Exit");
  105.  
  106. /*add all initialized components to the container*/
  107.  
  108.  
  109. GridBagConstraints gridBagConstraintsx17 = new GridBagConstraints();
  110. gridBagConstraintsx17.gridx = 5;
  111. gridBagConstraintsx17.gridy = 6;
  112. gridBagConstraintsx17.insets = new Insets(5,5,5,5);
  113. container1.add(text_area, gridBagConstraintsx17);
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121. GridBagConstraints gridBagConstraintsx01 = new GridBagConstraints();
  122. gridBagConstraintsx01.gridx = 0;
  123. gridBagConstraintsx01.gridy = 0;
  124. gridBagConstraintsx01.insets = new Insets(5,5,5,5);
  125. container1.add(labName, gridBagConstraintsx01);
  126.  
  127. GridBagConstraints gridBagConstraintsx02 = new GridBagConstraints();
  128. gridBagConstraintsx02.gridx = 1;
  129. gridBagConstraintsx02.insets = new Insets(5,5,5,5);
  130. gridBagConstraintsx02.gridy = 0;
  131. gridBagConstraintsx02.gridwidth = 2;
  132. gridBagConstraintsx02.fill = GridBagConstraints.BOTH;
  133. container1.add(textName, gridBagConstraintsx02);
  134.  
  135. GridBagConstraints gridBagConstraintsx03 = new GridBagConstraints();
  136. gridBagConstraintsx03.gridx = 0;
  137. gridBagConstraintsx03.insets = new Insets(5,5,5,5);
  138. gridBagConstraintsx03.gridy = 1;
  139. container1.add(labAddress, gridBagConstraintsx03);
  140.  
  141. GridBagConstraints gridBagConstraintsx04 = new GridBagConstraints();
  142. gridBagConstraintsx04.gridx = 1;
  143. gridBagConstraintsx04.insets = new Insets(5,5,5,5);
  144. gridBagConstraintsx04.gridy = 1;
  145. gridBagConstraintsx04.gridwidth = 2;
  146. gridBagConstraintsx04.fill = GridBagConstraints.BOTH;
  147. container1.add(textAddress, gridBagConstraintsx04);
  148.  
  149. GridBagConstraints gridBagConstraintsx05 = new GridBagConstraints();
  150. gridBagConstraintsx05.gridx = 0;
  151. gridBagConstraintsx05.insets = new Insets(5,5,5,5);
  152. gridBagConstraintsx05.gridy = 2;
  153. container1.add(labPhone, gridBagConstraintsx05);
  154.  
  155. GridBagConstraints gridBagConstraintsx06 = new GridBagConstraints();
  156. gridBagConstraintsx06.gridx = 1;
  157. gridBagConstraintsx06.gridy = 2;
  158. gridBagConstraintsx06.insets = new Insets(5,5,5,5);
  159. gridBagConstraintsx06.gridwidth = 2;
  160. gridBagConstraintsx06.fill = GridBagConstraints.BOTH;
  161. container1.add(textPhone, gridBagConstraintsx06);
  162.  
  163. GridBagConstraints gridBagConstraintsx07 = new GridBagConstraints();
  164. gridBagConstraintsx07.gridx = 0;
  165. gridBagConstraintsx07.insets = new Insets(5,5,5,5);
  166. gridBagConstraintsx07.gridy = 3;
  167. container1.add(labMob, gridBagConstraintsx07);
  168.  
  169. GridBagConstraints gridBagConstraintsx08 = new GridBagConstraints();
  170. gridBagConstraintsx08.gridx = 1;
  171. gridBagConstraintsx08.gridy = 3;
  172. gridBagConstraintsx08.gridwidth = 2;
  173. gridBagConstraintsx08.insets = new Insets(5,5,5,5);
  174. gridBagConstraintsx08.fill = GridBagConstraints.BOTH;
  175. container1.add(textMob, gridBagConstraintsx08);
  176.  
  177. GridBagConstraints gridBagConstraintsx09 = new GridBagConstraints();
  178. gridBagConstraintsx09.gridx = 0;
  179. gridBagConstraintsx09.gridy = 4;
  180. gridBagConstraintsx09.insets = new Insets(5,5,5,5);
  181. container1.add(buttonSave, gridBagConstraintsx09);
  182.  
  183. GridBagConstraints gridBagConstraintsx10 = new GridBagConstraints();
  184. gridBagConstraintsx10.gridx = 1;
  185. gridBagConstraintsx10.gridy = 4;
  186. gridBagConstraintsx10.insets = new Insets(5,5,5,5);
  187. container1.add(buttonDelete, gridBagConstraintsx10);
  188.  
  189. GridBagConstraints gridBagConstraintsx11 = new GridBagConstraints();
  190. gridBagConstraintsx11.gridx = 2;
  191. gridBagConstraintsx11.gridy = 4;
  192. gridBagConstraintsx11.insets = new Insets(5,5,5,5);
  193. container1.add(buttonImport, gridBagConstraintsx11);
  194.  
  195.  
  196.  
  197. GridBagConstraints gridBagConstraintsx12 = new GridBagConstraints();
  198. gridBagConstraintsx12.gridx = 0;
  199. gridBagConstraintsx12.gridy = 5;
  200. gridBagConstraintsx12.insets = new Insets(5,5,5,5);
  201. container1.add(buttonBack, gridBagConstraintsx12);
  202.  
  203. GridBagConstraints gridBagConstraintsx13 = new GridBagConstraints();
  204. gridBagConstraintsx13.gridx = 1;
  205. gridBagConstraintsx13.gridy = 5;
  206. gridBagConstraintsx13.insets = new Insets(5,5,5,5);
  207. container1.add(buttonSearch, gridBagConstraintsx13);
  208.  
  209. GridBagConstraints gridBagConstraintsx14 = new GridBagConstraints();
  210. gridBagConstraintsx14.gridx = 2;
  211. gridBagConstraintsx14.gridy = 5;
  212. gridBagConstraintsx14.insets = new Insets(5,5,5,5);
  213. container1.add(buttonForward, gridBagConstraintsx14);
  214.  
  215.  
  216.  
  217. GridBagConstraints gridBagConstraintsx16 = new GridBagConstraints();
  218. gridBagConstraintsx16.gridx = 1;
  219. gridBagConstraintsx16.gridy = 6;
  220. gridBagConstraintsx16.insets = new Insets(5,5,5,5);
  221. container1.add(buttonExit, gridBagConstraintsx16);
  222.  
  223.  
  224. buttonExit.addActionListener(this);
  225.  
  226.  
  227.  
  228.  
  229.  
  230. }
  231.  
  232. public void actionPerformed (ActionEvent e)
  233. {
  234.  
  235. if (e.getSource() == buttonExit)
  236. {
  237. System.exit(0);
  238. }
  239. }
  240.  
  241. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hanntaa is offline Offline
12 posts
since Feb 2009
Nov 10th, 2009
0
Re: java app layout help
I would look into Borderlayout which allows you to add frames to your application and then cardlayout which allows you to swap content in and out of a specified frame.
Reputation Points: 11
Solved Threads: 2
Light Poster
kinger29 is offline Offline
35 posts
since Mar 2008
Nov 10th, 2009
0
Re: java app layout help
thanks, if this works i cant explain how much i love you!! tearing my hair out.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hanntaa is offline Offline
12 posts
since Feb 2009
Nov 10th, 2009
0
Re: java app layout help
look at the program in my thread as an example it might help with some of your syntax

http://www.daniweb.com/forums/post10...ml#post1043302
Reputation Points: 11
Solved Threads: 2
Light Poster
kinger29 is offline Offline
35 posts
since Mar 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: List.remove(List)
Next Thread in Java Forum Timeline: Java Web Browser





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


Follow us on Twitter


© 2011 DaniWeb® LLC