Results 1 to 9 of 9
- 07-05-2011, 07:27 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 5
- Rep Power
- 0
can't add to JPanel after removeAll() is triggered by another swing component
Consider the bit of code below. It's a much simplified version of my real app.
In jPanel1 is a single label. In jPanel2 is a single button.
The button in jPanel2 is supposed to wipe clear (with removeAll()) jPanel1 and add a new label in place of the old one. What actually happens is that after the first button press, jPanel1 is indeed cleared by the removeAll() method but the new label can't be added (or rather it can be added but won't show.) I can hard code the removal & addition back and forth all day & it works fine. It's just when I use a swing component like JButton or JComboBox that it doesn't work. Why?? Is this a thread thing? (I know I can just change the text of the label but the real app is much more complicated.)
Java Code:package my.stuff; import java.awt.*; import javax.swing.*; public class TreeTest3 extends javax.swing.JFrame { private String language = "english"; public TreeTest3() { initComponents(); jPanel1.setLayout(new FlowLayout()); jPanel1.setVisible(true); changeLabel(); } private void changeLabel() { System.out.println("language = " + language); jPanel1.removeAll(); jPanel1.validate(); jPanel1.add(new JLabel(language), "Center"); repaint(); } @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jPanel1 = new javax.swing.JPanel(); jPanel2 = new javax.swing.JPanel(); jButton1 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jPanel1.setBorder(new javax.swing.border.MatteBorder(null)); jPanel1.setPreferredSize(new java.awt.Dimension(400, 204)); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 398, Short.MAX_VALUE) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 133, Short.MAX_VALUE) ); jButton1.setText("switch"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup( jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addContainerGap() .addComponent(jButton1) .addContainerGap(308, Short.MAX_VALUE)) ); jPanel2Layout.setVerticalGroup( jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addContainerGap() .addComponent(jButton1) .addContainerGap(126, Short.MAX_VALUE)) ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); pack(); }// </editor-fold> private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { if (language.equals("english")) language = "spanish"; else language = "english"; changeLabel(); } public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new TreeTest3().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; // End of variables declaration }
- 07-05-2011, 08:04 AM #2
- 07-05-2011, 08:04 AM #3
- 07-05-2011, 08:07 AM #4
Well thats 4 forums. Anymore?
Last edited by Junky; 07-05-2011 at 08:16 AM.
- 07-05-2011, 08:08 AM #5
Thanks Junky. Added that to the notification on the other two forums.
db
- 07-05-2011, 08:09 AM #6
- 07-05-2011, 09:23 AM #7
Member
- Join Date
- Apr 2011
- Posts
- 5
- Rep Power
- 0
What's the issue here guys? So what if I posted the same question on javaranch & other web sites. They're different websites with different admins & different users. Crossposting across different forums on the same site is rotten I agree. But asking multiple sources in different places is sensible. I rarely get a reply on more than 1. So where's the beef?
- 07-05-2011, 09:59 AM #8
Member
- Join Date
- Apr 2011
- Posts
- 5
- Rep Power
- 0
for the archives: the correct code is:
Thanks to StanislavLJava Code:private void changeLabel() { System.out.println("language = " + language); jPanel1.removeAll(); jPanel1.add(new JLabel(language), "Center"); jPanel1.revalidate(); repaint(); }
- 07-05-2011, 10:12 AM #9
Similar Threads
-
Which swing component is suitable for TextEditor...?
By Dinesh_rockz in forum AWT / SwingReplies: 1Last Post: 02-23-2011, 05:41 AM -
Cannot add component to JPanel
By adi.shoukat in forum NetBeansReplies: 7Last Post: 12-10-2010, 11:12 PM -
swing component
By cahyadiakbar in forum AWT / SwingReplies: 0Last Post: 09-22-2010, 10:40 AM -
Modifying and reloading JPanel component
By gopher in forum AWT / SwingReplies: 10Last Post: 02-07-2009, 04:15 AM -
how to remove all component in container(Jpanel)
By SaYuNaRa in forum New To JavaReplies: 3Last Post: 04-17-2008, 04:22 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks