You are on page 1of 18

Join DaniWeb Member Login

952,667 Members Technology Publication meets Social Media


sw ingcontr

Hardware & Software Software Development Web Development Internet Marketing Business Exchange Community Center

Software Development > Java > Swing controls not showing on jPanel 12> Have something to say? Contribute New Article Reply to this Article 1 Year Ago 0

Swing controls not showing on jPanel


Hi, I'm trying to use JAVA GUI for first time. I've got a Jframe that when loaded adds a jpanel. the problem is it will not show any swing controls that get added to the jPanel. I'm using netbeans 6.9.1. any idea to why is happerning would be greatful
2. 1. /* * To change this template, choose Tools | Templates 3. * and open the template in the editor. 4. */ 5. 6. /* 7. * testing.java 8. * 9. * Created on 03-Dec-2010, 16:46:54 10. */ 11. 12. package Test; 13. 14. /** 15. * 16. * @author e1159789

36.

17. */ 18. public class testing extends javax.swing.JFrame { 19. 20. /** Creates new form testing */ 21. public testing() { 22. initComponents(); 23. this.add(jPanel1); 24. this.validate(); 25. this.repaint(); 26. } 27. 28. private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 29. // TODO add your handling code here: 30. this.remove(jPanel1); 31. this.add(jPanel2); 32. this.validate(); 33. this.repaint(); 34. } 35. private void button1ActionPerformed(java.awt.event.ActionEvent evt) { 37. // TODO add your handling code here: 38. this.remove(jPanel1); 39. this.add(jPanel2); 40. this.validate(); 41. this.repaint(); 42. } 43. 44. /** 45. * @param args the command line arguments 46. */ 47. public static void main(String args[]) { 48. java.awt.EventQueue.invokeLater(new Runnable() { 49. public void run() { 50. new testing().setVisible(true); 51. } 52. }); 53. } 54. 55. // Variables declaration - do not modify 56. private java.awt.Button button1; 57. private javax.swing.JButton jButton1; 58. private javax.swing.JButton jButton2; 59. private javax.swing.JLabel jLabel1; 60. private javax.swing.JLabel jLabel2; 61. private javax.swing.JList jList1; 62. private javax.swing.JPanel jPanel1; 63. private javax.swing.JPanel jPanel2; 64. private javax.swing.JPasswordField jPasswordField1; 65. private javax.swing.JPasswordField jPasswordField2; 66. private javax.swing.JScrollPane jScrollPane1; 67. private javax.swing.JScrollPane jScrollPane2; 68. private javax.swing.JTextArea jTextArea1; 69. private javax.swing.JTextField jTextField1; 70. private javax.swing.JTextField jTextField2; 71. private java.awt.Label label1;

72. 73. 74.

private java.awt.TextArea textArea1; private java.awt.TextField textField1; // End of variables declaration 75. 76. }

Note: ive deleted the chunk of generated code so it dont spam the forum. ajst Junior Poster 121 posts since Nov 2010 Ads by Google Java Test Tool Automate Java AWT/Swing/SWT testing for Windows & Web apps. Try it Now! AutomationAnywhere.com/Java-Test 1 Year Ago 0 There's no code there to add any controls to a JPanel. JamesCherrill Posting Genius 6,219 posts since Apr 2008 1 Year Ago 0 I did say in the post that I have removed the generated code that adds the controls but here it is anyway.
1. // <editor-fold defaultstate="collapsed" desc="Generated Code"> 2. private void initComponents() { 3. 4. jPanel1 = new javax.swing.JPanel(); 5. jLabel1 = new javax.swing.JLabel(); 6. jButton1 = new javax.swing.JButton(); 7. jPasswordField1 = new javax.swing.JPasswordField(); 8. jTextField1 = new javax.swing.JTextField(); 9. label1 = new java.awt.Label(); 10. button1 = new java.awt.Button(); 11. textField1 = new java.awt.TextField(); 12. textArea1 = new java.awt.TextArea(); 13. jPanel2 = new javax.swing.JPanel(); 14. jLabel2 = new javax.swing.JLabel(); 15. jButton2 = new javax.swing.JButton(); 16. jScrollPane1 = new javax.swing.JScrollPane(); 17. jList1 = new javax.swing.JList(); 18. jTextField2 = new javax.swing.JTextField(); 19. jPasswordField2 = new javax.swing.JPasswordField(); 20. jScrollPane2 = new javax.swing.JScrollPane(); 21. jTextArea1 = new javax.swing.JTextArea();

22. 23. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 24. 25. jLabel1.setText("jLabel1"); 26. 27. jButton1.setText("jButton1"); 28. jButton1.addActionListener(new java.awt.event.ActionListener() { 29. public void actionPerformed(java.awt.event.ActionEvent evt) { 30. jButton1ActionPerformed(evt); 31. } 32. }); 33. 34. jPasswordField1.setText("jPasswordField1"); 35. 36. jTextField1.setText("jTextField1"); 37. 38. label1.setText("label1"); 39. 40. button1.setLabel("button1"); 41. button1.addActionListener(new java.awt.event.ActionListener() { 42. public void actionPerformed(java.awt.event.ActionEvent evt) { 43. button1ActionPerformed(evt); 44. } 45. }); 46. 47. textField1.setText("textField1"); 48. 49. javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); 50. jPanel1.setLayout(jPanel1Layout); 51. jPanel1Layout.setHorizontalGroup( 52. jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEA DING) 53. .addGroup(jPanel1Layout.createSequentialGroup() 54. .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Ali gnment.LEADING) 55. .addGroup(jPanel1Layout.createSequentialGroup() 56. .addGap(89, 89, 89) 57. .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Ali gnment.LEADING) 58. .addComponent(jLabel1) 59. .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) 60. .addGap(44, 44, 44) 61. .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Ali gnment.LEADING)

.addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 63. .addComponent(jButton1))) 64. .addGroup(jPanel1Layout.createSequentialGroup() 65. .addGap(98, 98, 98) 66. .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Ali gnment.TRAILING) 67. .addComponent(textField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 68. .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) 69. .addGap(49, 49, 49) 70. .addComponent(button1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 71. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 72. .addComponent(textArea1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) 73. .addContainerGap()) 74. ); 75. jPanel1Layout.setVerticalGroup( 76. jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEA DING) 77. .addGroup(jPanel1Layout.createSequentialGroup() 78. .addGap(61, 61, 61) 79. .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Ali gnment.BASELINE) 80. .addComponent(jLabel1) 81. .addComponent(jButton1)) 82. .addGap(38, 38, 38) 83. .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Ali gnment.BASELINE) 84. .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 85. .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) 86. .addGap(65, 65, 65)

62.

87. .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Ali gnment.LEADING) 88. .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 89. .addComponent(button1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) 90. .addGap(20, 20, 20) 91. .addComponent(textField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 92. .addContainerGap(29, Short.MAX_VALUE)) 93. .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() 94. .addContainerGap(210, Short.MAX_VALUE) 95. .addComponent(textArea1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 96. .addContainerGap()) 97. ); 98. 99. jLabel2.setText("jLabel2"); 100. 101. jButton2.setText("jButton2"); 102. jButton2.addActionListener(new java.awt.event.ActionListener() { 103. public void actionPerformed(java.awt.event.ActionEvent evt) { 104. jButton2ActionPerformed(evt); 105. } 106. }); 107. 108. jList1.setModel(new javax.swing.AbstractListModel() { 109. String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" }; 110. public int getSize() { return strings.length; } 111. public Object getElementAt(int i) { return strings[i]; } 112. }); 113. jScrollPane1.setViewportView(jList1); 114. 115. jTextField2.setText("jTextField2"); 116. 117. jPasswordField2.setText("jPasswordField2"); 118. 119. jTextArea1.setColumns(20); 120. jTextArea1.setRows(5); 121. jScrollPane2.setViewportView(jTextArea1); 122. 123. javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); 124. jPanel2.setLayout(jPanel2Layout);

jPanel2Layout.setHorizontalGroup( 126. jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEA DING) 127. .addGroup(jPanel2Layout.createSequentialGroup() 128. .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Ali gnment.LEADING, false) 129. .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup() 130. .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 131. .addComponent(jPasswordField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 132. .addGap(47, 47, 47)) 133. .addGroup(jPanel2Layout.createSequentialGroup() 134. .addGap(125, 125, 125) 135. .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Ali gnment.TRAILING) 136. .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 137. .addComponent(jLabel2)) 138. .addGap(61, 61, 61)) 139. .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup() 140. .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 141. .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 142. .addGap(20, 20, 20))) 143. .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Ali gnment.LEADING) 144. .addGroup(jPanel2Layout.createSequentialGroup() 145. .addGap(10, 10, 10) 146. .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) 147. .addComponent(jButton2)) 148. .addContainerGap(64, Short.MAX_VALUE)) 149. ); 150. jPanel2Layout.setVerticalGroup( 151. jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEA DING) 152. .addGroup(jPanel2Layout.createSequentialGroup() 153. .addGap(36, 36, 36)

125.

154. .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Ali gnment.TRAILING) 155. .addComponent(jButton2) 156. .addComponent(jLabel2)) 157. .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Ali gnment.LEADING) 158. .addGroup(jPanel2Layout.createSequentialGroup() 159. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 160. .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 161. .addContainerGap(103, Short.MAX_VALUE)) 162. .addGroup(jPanel2Layout.createSequentialGroup() 163. .addGap(32, 32, 32) 164. .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 165. .addGap(18, 18, 18) 166. .addComponent(jPasswordField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 167. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE) 168. .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 169. .addGap(24, 24, 24)))) 170. ); 171. 172. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 173. getContentPane().setLayout(layout); 174. layout.setHorizontalGroup( 175. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 176. .addGap(0, 404, Short.MAX_VALUE) 177. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment. LEADING) 178. .addGroup(layout.createSequentialGroup() 179. .addContainerGap() 180. .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 181. .addContainerGap())) 182. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment. LEADING) 183. .addGroup(layout.createSequentialGroup()

.addContainerGap() .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 186. .addContainerGap())) 187. ); 188. layout.setVerticalGroup( 189. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 190. .addGap(0, 322, Short.MAX_VALUE) 191. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment. LEADING) 192. .addGroup(layout.createSequentialGroup() 193. .addContainerGap() 194. .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 195. .addContainerGap())) 196. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment. LEADING) 197. .addGroup(layout.createSequentialGroup() 198. .addContainerGap() 199. .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 200. .addContainerGap())) 201. ); 202. 203. pack(); 204. }// </editor-fold>

184. 185.

ajst Junior Poster 121 posts since Nov 2010 1 Year Ago 0 Do you mean it never displays the controls, or is the problem when you click the buttons that change between the two panels? Have you tried to pack() the JFrame after adding or removing the panels? JamesCherrill Posting Genius 6,219 posts since Apr 2008 1 Year Ago 0 When ever i load the panels any swing controls on the panel will not be displayed. but if i use AWT controls then they will load.

this is just a program when you click on a button it switchs between the 2 panels. mainly to simulate my program with out publishing my code. I've just tried adding .Pack after i added the panel to the frame but still having the same problem. ajst Junior Poster 121 posts since Nov 2010 1 Year Ago 0 Do not use awt.Button in Swing Lines 30,31 and lines 38,39 do the same (logic) quuba Posting Pro 573 posts since Nov 2008 1 Year Ago 0 I missed that! Good point quuba. Never mix AWT and Swing components. JamesCherrill Posting Genius 6,219 posts since Apr 2008 1 Year Ago 0 I dont want to use awt.buttons or text fields but I'm having to becuase i cant get swing buttons or fields to appear when switching between panels. this is my problem im asking for help to solve. there must be a way to switch between panels on a jframe and still be able to see the buttons etc. lines 30,31 and 38,39 do the same becuase they are 2 diffrent buttons. 30,31 are the swing button that does not appear so cant test it. 38,39 are a awt button. which does appear so i can test. ajst Junior Poster 121 posts since Nov 2010 1 Year Ago 0 replace awt.Button with JButton

2.

1. jPanel1.setBackground(Color.red); jPanel2.setBackground(Color.blue);

1. private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 2. // TODO add your handling code here: 3. this.remove(jPanel1); 4. this.add(jPanel2); 5. this.validate(); 6. this.repaint(); 7. } 8. 9. private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { 10. // TODO add your handling code here: 11. this.remove(jPanel2);//this.remove(jPanel1); 12. this.add(jPanel1);//this.add(jPanel2); 13. this.validate(); 14. this.repaint(); 15. }

quuba Posting Pro 573 posts since Nov 2008 1 Year Ago 0 Just to make sure that I was testing just swing controls. I started again and used the code you supplied.
1. /* * To change this template, choose Tools | Templates 3. * and open the template in the editor. 4. */ 5. 6. /* 7. * NewJFrame.java 8. * 9. * Created on 06-Dec-2010, 11:37:02 10. */ 11. 12. package Test; 13. 14. /** 15. * 16. * @author e1159789 17. */ 18. public class NewJFrame extends javax.swing.JFrame { 19. 20. /** Creates new form NewJFrame */ 21. public NewJFrame() { 22. initComponents(); 23. } 24. 25. /** This method is called from within the constructor to 2.

26. * initialize the form. * WARNING: Do NOT modify this code. The content of this method is 28. * always regenerated by the Form Editor. 29. */ 30. @SuppressWarnings("unchecked") 31. // <editor-fold defaultstate="collapsed" desc="Generated Code"> 32. private void initComponents() { 33. 34. jPanel2 = new javax.swing.JPanel(); 35. jButton2 = new javax.swing.JButton(); 36. jPanel1 = new javax.swing.JPanel(); 37. jButton1 = new javax.swing.JButton(); 38. 39. jButton2.setText("jButton2"); 40. jButton2.addActionListener(new java.awt.event.ActionListener() { 41. public void actionPerformed(java.awt.event.ActionEvent evt) { 42. jButton2ActionPerformed(evt); 43. } 44. }); 45. 46. javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); 47. jPanel2.setLayout(jPanel2Layout); 48. jPanel2Layout.setHorizontalGroup( 49. jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEA DING) 50. .addGroup(jPanel2Layout.createSequentialGroup() 51. .addGap(142, 142, 142) 52. .addComponent(jButton2) 53. .addContainerGap(183, Short.MAX_VALUE)) 54. ); 55. jPanel2Layout.setVerticalGroup( 56. jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEA DING) 57. .addGroup(jPanel2Layout.createSequentialGroup() 58. .addGap(97, 97, 97) 59. .addComponent(jButton2) 60. .addContainerGap(145, Short.MAX_VALUE)) 61. ); 62. 63. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 64. 65. jButton1.setText("jButton1"); 66. jButton1.addActionListener(new java.awt.event.ActionListener() { 67. public void actionPerformed(java.awt.event.ActionEvent evt) { 68. jButton1ActionPerformed(evt); 69. } 70. }); 71. 27.

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); 73. jPanel1.setLayout(jPanel1Layout); 74. jPanel1Layout.setHorizontalGroup( 75. jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEA DING) 76. .addGroup(jPanel1Layout.createSequentialGroup() 77. .addGap(138, 138, 138) 78. .addComponent(jButton1) 79. .addContainerGap(197, Short.MAX_VALUE)) 80. ); 81. jPanel1Layout.setVerticalGroup( 82. jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEA DING) 83. .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() 84. .addContainerGap(199, Short.MAX_VALUE) 85. .addComponent(jButton1) 86. .addGap(184, 184, 184)) 87. ); 88. 89. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 90. getContentPane().setLayout(layout); 91. layout.setHorizontalGroup( 92. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 93. .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 94. ); 95. layout.setVerticalGroup( 96. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 97. .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 98. ); 99. 100. pack(); 101. }// </editor-fold> 102. 103. private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 104. // TODO add your handling code here: 105. this.remove(jPanel1); this.add(jPanel2); this.validate(); this.repaint(); 106. } 107. 108. private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { 109. // TODO add your handling code here: 110. this.remove(jPanel2);//this.remove(jPanel1); 111. this.add(jPanel1);//this.add(jPanel2); 112. this.validate(); this.repaint();

72.

113. } 114. 115. /** 116. * @param args the command line arguments 117. */ 118. public static void main(String args[]) { 119. java.awt.EventQueue.invokeLater(new Runnable() { 120. public void run() { 121. new NewJFrame().setVisible(true); 122. } 123. }); 124. } 125. 126. // Variables declaration - do not modify 127. private javax.swing.JButton jButton1; 128. private javax.swing.JButton jButton2; 129. private javax.swing.JPanel jPanel1; 130. private javax.swing.JPanel jPanel2; 131. // End of variables declaration 132. 133. }

I still have the same problem. The panel that it starts on will load the Button but once clicked it will not show the second panel or the other button. Here are links to screen shots of my design layout from netbeans and how the program looks once run. jpanel 1 design http://yfrog.com/izpanel1oj jpanel 2 design http://yfrog.com/6gpanel2oj the program running first screen. http://yfrog.com/49firstscreenj second screen. http://yfrog.com/f3secondscreenj ajst Junior Poster 121 posts since Nov 2010 1 Year Ago 0 A couple of things to try... Call revalidate() rather than just validate() after changing contents. Rather than remove/add, maybe try setVisible(...) to show/hide the two panels?

JamesCherrill Posting Genius 6,219 posts since Apr 2008 1 Year Ago 0 Ok. If change the visibilty to false and true instead of adding and remove it works perfectly. but is there a way to do the same via add/remove? ajst Junior Poster 121 posts since Nov 2010 1 Year Ago 0 I don't know why the add/remove thing isn't working. I've used setVisible myself in the past to do things like this and I admit that because it's simple and it works I just stick to that. JamesCherrill Posting Genius 6,219 posts since Apr 2008 1 Year Ago 0 Thank you all for the help I'll just code it all using hide/unhide rather then add/remove ajst Junior Poster 121 posts since Nov 2010 1 Year Ago 0 Ok I said this Thread was solved, but from the solution I've encounted a new Problem. I'm now switching between panels via setting visable to true or false. the problem i have is on the menu screen I've got events for when mousing over the buttons to change a label on there panel. now if my mouse moves to where these buttons are on that panel then the buttons start to be drawn even though that panel is hidden. ive also set it to be un focused. ajst Junior Poster 121 posts since Nov 2010

1 Year Ago 0 It shouldn't be hard to check what's supposed to be visible before changing things in the mouse event handler? Anyway, if you are just changing the text for a component that's in an invisible container I wouldn't expect that to display anything. Are you just changing the text, or are you creating new components? JamesCherrill Posting Genius 6,219 posts since Apr 2008 1 Year Ago 0 Since posting my problem has changed. I've fixed the problem of hidden panels displaying there compenents when they are not ment to. my problem is now when i load the next panel everything is hidden until i mouse over them. I've tried repainting and validateing etc. the mouse over event just changes the text in a label and does not change any other properties. but even the label and a button with out a mouse over event does not get shown on the screen till i move the mouse over it. ajst Junior Poster 121 posts since Nov 2010 1 Year Ago 0 revalidate() should do it. You're not overriding paint or paintcomponent are you? JamesCherrill Posting Genius 6,219 posts since Apr 2008 1 Year Ago 0 no I'm not overriding paint or paintcomponent only calling .repaint() Now am i being stupid about revalidate() because when ever i do this.revalidate() it says this method does not exist.

validate() works but it wont compile if i put revalidate() ajst Junior Poster 121 posts since Nov 2010 1 Year Ago Show Comments 1 This works fine with swing components
1. public Testing() { 2. initComponents(); 3. this.remove(jPanel2); // remove no add once more! 4. } 5. private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 7. this.remove(jPanel1); 8. this.add(jPanel2); 9. this.repaint(); 10. } 11. private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { 13. this.remove(jPanel2); 14. this.add(jPanel1); 15. this.repaint(); 16. } 1. //to view set //jPanel1.setOpaque(false); //jPanel2.setOpaque(false);

6.

12.

2. 3.

quuba Posting Pro 573 posts since Nov 2008


sw ingcontr

This question has already been solved


Post: Markdown Syntax: Formatting Help Bold Italic Code Inline Code Link Quote Heading Sub-Heading # List List Undo Redo

You

12> Software Development > Java 0 inShare View similar articles that have also been tagged: java jpanel swing 2d access ajax android api applet array arraylist arrays atm awt beginner button c# c++ char class classes code connection constructors convert database eclipse error expression file files for-loop frame game graphics gui help help! homework homeworkhelp html image input insert int interface j2ee java java. javascript jbutton jdbc jframe jpanel jsp jtable jtextfield layoutmanager linked list loop loops mac method methods mysql netbeans null number object output paint pattern php print problem program programming project query read recursive regex scanner script server servlet simple sockets sort sql statement string student swing text thread tomcat tree uml update web xml 2012 DaniWeb LLC Home | About Us | Contact Us | Terms of Service | Advertising Opportunities

Recently Updated Articles Join the DaniWeb Community

You might also like