GridBagLayout HELP

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2005
Posts: 22
Reputation: vex is an unknown quantity at this point 
Solved Threads: 0
vex vex is offline Offline
Newbie Poster

GridBagLayout HELP

 
0
  #1
Nov 6th, 2005
Is there something about adding a JList to GridBagLayout that I am missing. I want to place a JList between a JLabel and a JTextArea. No matter what I do the list seems to stay in column 1.

I am only using GridBagLayout in the west pane(wP) I have tried messing with the weights and I am stumped. I must be missing something!!!

I just want everything in column 0


First code block is the layout. Second is the entire project so far.

  1.  
  2. Project6()
  3. {
  4. super("James W Project6");
  5. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  6.  
  7. nP = new JPanel();
  8. nP.setLayout(new FlowLayout());
  9. nP.setBackground(new Color(85,107,47));
  10. npLbl = new JLabel("South Park Photo Album");
  11. npLbl.setFont(fifty);
  12. nP.add(npLbl);
  13. add(nP, BorderLayout.NORTH);
  14.  
  15. sP = new JPanel();
  16. sP.setLayout(new GridLayout(1,2));
  17. sP.setBackground(new Color(85,107,47));
  18. spLbl = new JLabel("Select Font Size");
  19. spLbl.setFont(thirty);
  20. spLbl.setHorizontalTextPosition(SwingConstants.LEFT);
  21. sP.add(spLbl);
  22. jsl.setMajorTickSpacing(2);
  23. jsl.setPaintTicks(true);
  24. jsl.setPaintLabels(true);
  25. jsl.addChangeListener(this);
  26. sP.add(jsl);
  27. add(sP, BorderLayout.SOUTH);
  28.  
  29. // Problem is in here
  30.  
  31. GridBagLayout gridbag = new GridBagLayout();
  32. GridBagConstraints constraints = new GridBagConstraints();
  33. constraints.fill = GridBagConstraints.BOTH;
  34. wP = new JPanel();
  35. wP.setLayout(gridbag);
  36. wP.setBackground(new Color(189,183,107));
  37.  
  38. wpLbl = new JLabel("Sayings");
  39. wpLbl.setFont(twenty);
  40. constraints.ipady = 50;
  41. constraints.weighty = 0.3;
  42. constraints.gridx = 0;
  43. constraints.gridy = 0;
  44. constraints.anchor = GridBagConstraints.FIRST_LINE_START;
  45. gridbag.setConstraints(wpLbl, constraints);
  46. wP.add(wpLbl);
  47.  
  48. sayList = new JList(sayings);
  49. sayList.setVisibleRowCount(3);
  50. sayList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
  51. constraints.weightx = 0.0;
  52. constraints.weighty = 0.0;
  53. constraints.gridx = 0;
  54. constraints.gridy = 1;
  55. constraints.anchor = GridBagConstraints.LINE_START;
  56. gridbag.setConstraints(sayList, constraints);
  57. wP.add(new JScrollPane(sayList));
  58. sayList.addListSelectionListener(this);
  59.  
  60.  
  61. cart = new JTextArea(11,12);
  62. cart.setBackground(new Color(189,183,107));
  63. cart.setBorder(BorderFactory.createTitledBorder
  64. (BorderFactory.createLineBorder(Color.black),"Cart:"));
  65. cart.setEditable(false);
  66. cart.setLineWrap(true);
  67. constraints.gridx = 0;
  68. constraints.gridy = 2;
  69. //constraints.anchor = GridBagConstraints.PAGE_END;
  70. gridbag.setConstraints(cart, constraints);
  71. wP.add(cart);
  72. add(wP, BorderLayout.WEST);
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79. eP = new JPanel();
  80. eP.setLayout(new GridLayout(12,1));
  81. eP.setBackground(new Color(189,183,107));
  82. lbl1 = new JLabel("Available");
  83. lbl1.setFont(twenty);
  84. lbl2 = new JLabel("Photo Sizes");
  85. lbl2.setFont(twenty);
  86. lbl3 = new JLabel("Check Selections");
  87. lbl3.setFont(twenty);
  88. cb3 = new JCheckBox("3X5 $0.50 each");
  89. cb5 = new JCheckBox("5X7 $1.00 each");
  90. cb6 = new JCheckBox("6X8 $1.50 each");
  91. cb8 = new JCheckBox("8X10 $3.00 each");
  92. cb16 = new JCheckBox("16X20 $5.00 each");
  93. completeBtn = new JButton("Complete Order");
  94. clrBtn = new JButton("Clear");
  95. exitBtn = new JButton("Exit");
  96. eP.add(lbl1);
  97. eP.add(lbl2);
  98. eP.add(lbl3);
  99. eP.add(cb3);
  100. eP.add(cb5);
  101. eP.add(cb6);
  102. eP.add(cb8);
  103. eP.add(cb16);
  104. eP.add(completeBtn);
  105. eP.add(clrBtn);
  106. eP.add(exitBtn);
  107. cb3.addItemListener(this);
  108. cb5.addItemListener(this);
  109. cb6.addItemListener(this);
  110. cb8.addItemListener(this);
  111. cb16.addItemListener(this);
  112. completeBtn.addActionListener(this);
  113. clrBtn.addActionListener(this);
  114. exitBtn.addActionListener(this);
  115. add(eP, BorderLayout.EAST);
  116.  
  117.  
  118. /* commented out for posting uses icons
  119.  
  120. cP = new JPanel();
  121. cP.setLayout(new GridLayout(2,2));
  122. cP.setBackground(new Color(240,230,140));
  123. cpLbl1 = new JLabel("Photo Selection");
  124. cpLbl1.setFont(twenty);
  125. cP.add(cpLbl1);
  126. //picList = new JList(icons);
  127. //picList.setVisibleRowCount(2);
  128. //picList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  129. //cP.add(new JScrollPane(picList));
  130. //picList.addListSelectionListener(this);
  131. colorBtn = new JButton("Click to select a matte color and preview");
  132. cP.add(colorBtn);
  133. colorBtn.addActionListener(this);
  134. cP.add(cpLbl2);
  135. add(cP, BorderLayout.CENTER);
  136. */
  137.  
  138. }
  1. import java.awt.*;
  2. import javax.swing.*;
  3. import javax.swing.event.*;
  4. import java.awt.event.*;
  5.  
  6. public class Project6 extends JFrame implements MouseListener, ChangeListener,
  7. ListSelectionListener,
  8. ItemListener, ActionListener
  9. {
  10. private Container c = getContentPane();
  11. private JPanel nP, sP, cP, eP, wP;
  12. private JLabel npLbl,spLbl,wpLbl,epLbl,cpLbl1, lbl1,lbl2,lbl3,lbl4 ,lbl5;
  13. private JLabel cpLbl2 = new JLabel("hey");
  14. private JLabel wpLbl1 = new JLabel("hey");
  15. private JSlider jsl = new JSlider(SwingConstants.HORIZONTAL,8,30,10);
  16. private JButton colorBtn,clrBtn, completeBtn, exitBtn;
  17. private JList sayList, picList;
  18. private JTextArea cart;
  19. private JCheckBox cb3, cb5, cb6, cb8, cb16;
  20. private Font fifty = new Font("Lucida Sans", Font.BOLD, 50);
  21. private Font thirty = new Font("Serif", Font.BOLD, 30);
  22. private Font twenty = new Font("Serif", Font.BOLD, 20);
  23. private Font six = new Font("Serif", Font.PLAIN, 6);
  24. private boolean sayPicked = false;
  25. private boolean sizePicked = false;
  26. private boolean mattePicked = false;
  27. private boolean imagePicked = false;
  28.  
  29. private int size = 10;
  30. private int[] indices;
  31. private float total;
  32. private int qtyCb3, qtyCb5, qtyCb6, qtyCb8, qtyCb16;
  33.  
  34.  
  35. private String[] sayings = {"Honor Student","Varsity Football","Varsity Soccer",
  36. "Varsity Chearleading","Most likely to succeed",
  37. "Class Officer","Debate Team","Robotics Club",
  38. "School paper", "Loser"};
  39.  
  40. /* commented out for posting in forum incase someone wants to copy and paste to run it
  41.  
  42. private String[] pics = {"1a.gif","2a.gif","3a.gif","4a.gif","50a.gif",
  43. "62a.gif","98a.gif","64a.gif","56a.gif","7a.gif",
  44. "57a.gif","49a.gif"};
  45.  
  46. private String[] picNames = {"Eric","Kyle","Stan","Kenny","Starvin'Marvin","Timmy","Jimmy",
  47. "Tweek","Phillip","Mr. Garrison","Terrance","Mr. Mackey"};
  48.  
  49. private Icon icons[] = {new ImageIcon(pics[0]),
  50. new ImageIcon(pics[1]),
  51. new ImageIcon(pics[2]),
  52. new ImageIcon(pics[3]),
  53. new ImageIcon(pics[4]),
  54. new ImageIcon(pics[5]),
  55. new ImageIcon(pics[6]),
  56. new ImageIcon(pics[7]),
  57. new ImageIcon(pics[8]),
  58. new ImageIcon(pics[9]),
  59. new ImageIcon(pics[10]),
  60. new ImageIcon(pics[11])};
  61. */
  62.  
  63.  
  64.  
  65.  
  66.  
  67. public void stateChanged(ChangeEvent ce)// Slider
  68. {
  69. size = jsl.getValue();
  70. System.out.println("Size is now set to " + size);
  71.  
  72. }
  73. public void valueChanged(ListSelectionEvent lse)//List
  74. {
  75. if(lse.getSource() == sayList)
  76. {
  77. indices = sayList.getSelectedIndices();
  78. for(int i = 0; i<indices.length;i++)
  79. {
  80. System.out.println(sayings[indices[i]] + "\n\n");
  81. }
  82. }
  83. if(lse.getSource() == picList)
  84. {
  85. imagePicked = true;
  86. //System.out.println(picNames[picList.getSelectedIndex()]+ " "+imagePicked);
  87. }
  88.  
  89. }
  90. public void itemStateChanged(ItemEvent ie)//Checkboxes
  91. {
  92. if(ie.getSource() == cb3)
  93. {
  94. qtyCb3 = Integer.parseInt(JOptionPane.showInputDialog(
  95. "How many 3X5 pictures?"));
  96.  
  97. }
  98.  
  99. }
  100. public void actionPerformed(ActionEvent ae)//Buttons
  101. {
  102. if(ae.getSource() == colorBtn)
  103. {
  104.  
  105. }
  106. if(ae.getSource() == clrBtn)
  107. {
  108. clearAll();
  109. }
  110. if(ae.getSource() == exitBtn)
  111. {
  112. System.exit(0);
  113. }
  114. if(ae.getSource() == completeBtn)
  115. {
  116.  
  117. }
  118.  
  119. }
  120. public void setSize()
  121. {
  122.  
  123. }
  124. public void clearAll()
  125. {
  126. sayPicked = false;
  127. sizePicked = false;
  128. mattePicked = false;
  129. imagePicked = false;
  130. cb3.setSelected(false);
  131. cb5.setSelected(false);
  132. cb6.setSelected(false);
  133. cb8.setSelected(false);
  134. cb16.setSelected(false);
  135. jsl.setValue(10);
  136. sayList.clearSelection();
  137. }
  138.  
  139.  
  140. public static void main(String[] args)
  141. {
  142. Project6 a = new Project6();
  143. a.pack();
  144. a.setVisible(true);
  145. }
  146.  
  147. Project6()
  148. {
  149. super("James W Project6");
  150. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  151.  
  152. nP = new JPanel();
  153. nP.setLayout(new FlowLayout());
  154. nP.setBackground(new Color(85,107,47));
  155. npLbl = new JLabel("South Park Photo Album");
  156. npLbl.setFont(fifty);
  157. nP.add(npLbl);
  158. add(nP, BorderLayout.NORTH);
  159.  
  160. sP = new JPanel();
  161. sP.setLayout(new GridLayout(1,2));
  162. sP.setBackground(new Color(85,107,47));
  163. spLbl = new JLabel("Select Font Size");
  164. spLbl.setFont(thirty);
  165. spLbl.setHorizontalTextPosition(SwingConstants.LEFT);
  166. sP.add(spLbl);
  167. jsl.setMajorTickSpacing(2);
  168. jsl.setPaintTicks(true);
  169. jsl.setPaintLabels(true);
  170. jsl.addChangeListener(this);
  171. sP.add(jsl);
  172. add(sP, BorderLayout.SOUTH);
  173.  
  174. // Problem is in here
  175.  
  176. GridBagLayout gridbag = new GridBagLayout();
  177. GridBagConstraints constraints = new GridBagConstraints();
  178. constraints.fill = GridBagConstraints.BOTH;
  179. wP = new JPanel();
  180. wP.setLayout(gridbag);
  181. wP.setBackground(new Color(189,183,107));
  182.  
  183. wpLbl = new JLabel("Sayings");
  184. wpLbl.setFont(twenty);
  185. constraints.ipady = 50;
  186. constraints.weighty = 0.3;
  187. constraints.gridx = 0;
  188. constraints.gridy = 0;
  189. constraints.anchor = GridBagConstraints.FIRST_LINE_START;
  190. gridbag.setConstraints(wpLbl, constraints);
  191. wP.add(wpLbl);
  192.  
  193. sayList = new JList(sayings);
  194. sayList.setVisibleRowCount(3);
  195. sayList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
  196. constraints.weightx = 0.0;
  197. constraints.weighty = 0.0;
  198. constraints.gridx = 0;
  199. constraints.gridy = 1;
  200. constraints.anchor = GridBagConstraints.LINE_START;
  201. gridbag.setConstraints(sayList, constraints);
  202. wP.add(new JScrollPane(sayList));
  203. sayList.addListSelectionListener(this);
  204.  
  205.  
  206. cart = new JTextArea(11,12);
  207. cart.setBackground(new Color(189,183,107));
  208. cart.setBorder(BorderFactory.createTitledBorder
  209. (BorderFactory.createLineBorder(Color.black),"Cart:"));
  210. cart.setEditable(false);
  211. cart.setLineWrap(true);
  212. constraints.gridx = 0;
  213. constraints.gridy = 2;
  214. //constraints.anchor = GridBagConstraints.PAGE_END;
  215. gridbag.setConstraints(cart, constraints);
  216. wP.add(cart);
  217. add(wP, BorderLayout.WEST);
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224. eP = new JPanel();
  225. eP.setLayout(new GridLayout(12,1));
  226. eP.setBackground(new Color(189,183,107));
  227. lbl1 = new JLabel("Available");
  228. lbl1.setFont(twenty);
  229. lbl2 = new JLabel("Photo Sizes");
  230. lbl2.setFont(twenty);
  231. lbl3 = new JLabel("Check Selections");
  232. lbl3.setFont(twenty);
  233. cb3 = new JCheckBox("3X5 $0.50 each");
  234. cb5 = new JCheckBox("5X7 $1.00 each");
  235. cb6 = new JCheckBox("6X8 $1.50 each");
  236. cb8 = new JCheckBox("8X10 $3.00 each");
  237. cb16 = new JCheckBox("16X20 $5.00 each");
  238. completeBtn = new JButton("Complete Order");
  239. clrBtn = new JButton("Clear");
  240. exitBtn = new JButton("Exit");
  241. eP.add(lbl1);
  242. eP.add(lbl2);
  243. eP.add(lbl3);
  244. eP.add(cb3);
  245. eP.add(cb5);
  246. eP.add(cb6);
  247. eP.add(cb8);
  248. eP.add(cb16);
  249. eP.add(completeBtn);
  250. eP.add(clrBtn);
  251. eP.add(exitBtn);
  252. cb3.addItemListener(this);
  253. cb5.addItemListener(this);
  254. cb6.addItemListener(this);
  255. cb8.addItemListener(this);
  256. cb16.addItemListener(this);
  257. completeBtn.addActionListener(this);
  258. clrBtn.addActionListener(this);
  259. exitBtn.addActionListener(this);
  260. add(eP, BorderLayout.EAST);
  261.  
  262.  
  263. /* commented out for posting uses icons
  264.  
  265. cP = new JPanel();
  266. cP.setLayout(new GridLayout(2,2));
  267. cP.setBackground(new Color(240,230,140));
  268. cpLbl1 = new JLabel("Photo Selection");
  269. cpLbl1.setFont(twenty);
  270. cP.add(cpLbl1);
  271. //picList = new JList(icons);
  272. //picList.setVisibleRowCount(2);
  273. //picList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  274. //cP.add(new JScrollPane(picList));
  275. //picList.addListSelectionListener(this);
  276. colorBtn = new JButton("Click to select a matte color and preview");
  277. cP.add(colorBtn);
  278. colorBtn.addActionListener(this);
  279. cP.add(cpLbl2);
  280. add(cP, BorderLayout.CENTER);
  281. */
  282.  
  283. }
  284. public void mouseReleased(MouseEvent event){}
  285. public void mouseClicked(MouseEvent event){}
  286. public void mousePressed(MouseEvent event){}
  287. public void mouseEntered(MouseEvent event){}
  288. public void mouseExited(MouseEvent event){}
  289.  
  290.  
  291. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: GridBagLayout HELP

 
0
  #2
Nov 6th, 2005
Have you tried using gridwidth or ipadx?
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 22
Reputation: vex is an unknown quantity at this point 
Solved Threads: 0
vex vex is offline Offline
Newbie Poster

Re: GridBagLayout HELP

 
0
  #3
Nov 6th, 2005
sure did everything works fine till I add the JList I can comment out the JList and use a JLabel and it works fine
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC