Can't add to JPanel after removeAll() is triggered by another swing compone

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.)
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
}

It should be
                private void changeLabel()
          System.out.println("language = " + language);
          jPanel1.removeAll();
          jPanel1.add(new JLabel(language), "Center");
          jPanel1.revalidate();
          jPanel1.repaint();
     }

Similar Messages

  • How can i add an Engraving after i order the ipad ( add, not edit)

    how can i add an Engraving after i order the ipad ( add, not edit)

    Sorry, I don't mean to appear rude, but I don't understand the question. What do you mean "how do you call"? You pick up the telphone and call the store's number, just as you would any other person or company.
    None of the Apple Stores have on-line chat.
    Regards.

  • Can I add a JPanel in JScrollPane?

    Can I add a JPanel in JScrollPane?
    If I added,components in the JPanel can effect the JScrollPane?
    thanks!

    Can I add a JPanel in JScrollPane?yes you can - it is meant for this.
    If I added,components in the JPanel can effect the
    JScrollPane?if you mean affect like focusing and scrolling to a certain place - the answer is yes, you have to look the right functions in each component you insert to the panel.

  • Contacts. Can't add new contacts after upgrading to Mtn Lion from Snow Leopard.

    I can't add new contacts after upgrading from Snow Leopard to Mountain last week.  I do not use iCloud.  Any suggestions?

    Problems such as yours are sometimes caused by files that should belong to you but are locked or have wrong permissions. This procedure will check for such files. It makes no changes and therefore will not, in itself, solve your problem.
    First, empty the Trash.
    Launch the Terminal application in any of the following ways:
    ☞ Enter the first few letters of its name into a Spotlight search. Select it in the results (it should be at the top.)
    ☞ In the Finder, select Go ▹ Utilities from the menu bar, or press the key combination shift-command-U. The application is in the folder that opens.
    ☞ Open LaunchPad. Click Utilities, then Terminal in the icon grid.
    Triple-click anywhere in the line below to select it, then drag or copy it — do not type — into the Terminal window:
    find . $TMPDIR.. \( -flags +sappnd,schg,uappnd,uchg -o ! -user $UID -o ! -perm -600 -o -acl \) 2> /dev/null | wc -l
    Press return. The command may take a noticeable amount of time to run. Wait for a new line ending in a dollar sign (“$”) to appear.
    The output of this command, on a line directly below what you entered, will be a number such as "35." Please post it in a reply.

  • Can't add/edit contact after upgrade to IOS5 - HELP!

    After upgrading to ios 5, I can't add or edit the contact. The buttons are gone! How to get them back? I tried numerous restart also no use.

    I was having issues with Calendar after updating to iOS 5.0.1 also.  I finally was able to get Calendar functioning again by turning it OFF within Settings > iCloud.  This action keeps all my calendars on my iPod. I also reverted to backing up my iPod Touch to my computer within iTunes, then restoring, and re-syncing after I made the changes.  Now, I am able to add, edit, and delete events in my calendar just as I was able to do before updating to iOS 5.0.1.  Restoring my iPod Touch only worked temporarily for me (before I change the settings), as the problem returned the very next time I attempted to add an event.  This fix probably will not allow syncing with other Apple devices through iCloud, but it is less drastic than deleting my iCloud account.

  • How can I add a pause after each photo to my flash file?

    How can I add a break to my sequence of photos on a flash file.
    I created a flash with different photos and I want each one to stay on the stay for about a minute before moving away.
    How do I do this? Please help

    //Timers are measured in milliseconds
    var minuteTimer:Timer = new Timer(60000);
    minuteTimer.addEventListener(TimerEvent.TIMER, exchangePhoto);
    function exchangePhoto(e:Event):void{
         //put your code of exchanging your photo here
    //you have to start the timer explicitly, when your app starts
    minuteTimer.start();

  • Can we add Java Stack after complete installation

    We have already installed SAP ECC 6.0. While installation, we had opted only for ABAP stack. Can we add the Java stack now?
    Regards.

    Hi,
    Yes you can install java server on existing ABAP but
    If you have an existing ABAP central instance + ABAP dialog instance and
    you want to install Java Add-in to the central instance, you first need
    to uninstall the ABAP dialog instance, then Install the Java central
    instance add-in and then install an ABAP+Java Dialog insntance.
    Please refer ABAP+Java instalation guide.
    Best regards,
    Vipin

  • Can't add new contacts after upgrading to 10.2

    I just updated my Z10 with 10.2
    And now I'm having an issue.
    I can't add any new contacts on my phone. Even I can't find any social contacts from fb, twitter, skype, linkedin.
    I can only find the contact with BBM pin. Prior I was able to see my sim contacts. Suddenly those were disappeared too.
    It's really Hugh problem for me due to loss of contacts.
    How can solve this?

    So how did you sync your contacts, in what what?
    Open your contacts app.
    at the bottom left, tap on the tab and tap ALL. Do you see your contacts now?
    1. If any post helps you please click the below the post(s) that helped you.
    2. Please resolve your thread by marking the post "Solution?" which solved it for you!
    3. Install free BlackBerry Protect today for backups of contacts and data.
    4. Guide to Unlocking your BlackBerry & Unlock Codes
    Join our BBM Channels (Beta)
    BlackBerry Support Forums Channel
    PIN: C0001B7B4   Display/Scan Bar Code
    Knowledge Base Updates
    PIN: C0005A9AA   Display/Scan Bar Code

  • Can we add same JPanel twice??.. on JFrame and JDialog together!!

    Hello Frnds,
    my problem is quite weird!! i will try to explain..as much as I can !!
    My requirement is I want to show same JPanel in JDialog and JFrame at once..!!
    I am adding a
    JPanel A ---> JDialog
    JPanel A ---> JScrollPanel B
    JScrollPane B ---> JFrame.. and making frame and dialog visible together.. but JPanel is visible in dialog only..
    if i dont add in JDialog.. then JPanel is visible in frame.. !!.. but why that??
    JFrame fr = new JFrame();
    JPanel np = new JPanel();      // This JPanel is added Twice
    np.setBounds(0, 0, 128, 128);
    JScrollPane scroll=new JScrollPane(np);      // Once JPanel added here..
    scroll.setPreferredSize(new Dimension(385,385));
    fr.add(scroll);
    JDialog dlg=new JDialog(fr, "Another Panel", false);
    dlg.add(np);                        // Second Time JPanel added here..
    dlg.pack();
    dlg.setVisible(true);
    fr.setVisible(true);

    Let's make it very clear : it is NOT possible to add
    the same instance of a component to two different
    containers.
    Is it possible to have frame with component.. but on closing that frame component shifts to another frame.. and vice versa I will add to the above:
    it is NOT possible to add the same instance of a component to two different containers... at the same time. Does that clarify it?
    Why is it so hard to try it yourself?
    Instead of waiting a couple of hours hoping someone answers the question you could have solved your problem in a couple of minutes.

  • I can't add songs, even after I delete songs.  Only at 16.5 Gb

    I had my computer reformatted so i had to get all my songs from my iPod and put them on my laptop. After I did that, I tried importing the whole music folder into iTunes but it stopped after 3151 and about 16.5 GB. I tried every way of adding songs. I tried left clicking and open with. I tried file and open file. I tried dragging the files onto iTunes. is there anything I am forgetting? Right now I am stuck with half of my music library in Itunes and half of it in media player.

    Grey text when you look at the content and lock symbol at the bottom of the screen simply shows that your iPod is set to "automatically update songs and playlists". . If you want to access your iPod directly, play it through your iTunes, connect it to a second computer etc. you just need to change the update method to "Manually manage songs and Playlists" in the iPod preferences. Managing Your Songs Manually
    One thing to remember about manually updating though is that the iPod also has to be manually ejected before the "Do Not Disconnect" message will go away. Safely disconnect iPod
    For info: iPod 101
    The iPod offers three ways to transfer music from your computer. You can select one of the following update modes from the iPod Preferences menu in iTunes (Edit=>Preferences=>'iPod' tab):
    1) Automatically update all songs and playlists. This is the default mode, in which your entire music library, including playlists, is automatically synced to your iPod. If the music library on your computer exceeds the iPod storage capacity, you are prompted to select a different update method.
    2) Automatically update selected playlists only. With this option, iTunes automatically copies the playlists you have selected to the iPod when you connect it to the computer.
    3) Manually manage songs and playlists. You can also choose to transfer music to the iPod manually. This allows you to drag and drop individual songs and playlists from iTunes to the iPod

  • CAN YOU ADD NEW PHOTOS AFTER CREATING A BOOK?

    I finished making a book on iPhoto, but realized there were still some photos that i wanted to add. Is there any way that i can do this?

    A good general step for strange issues is to renew the iPhoto preference file - quit iPhoto and go to "your user name" ==> library ==> preferences ==> com.apple.iPhoto.plist and trash it - launch iPhoto which creates a fresh new default preference file and reset any personal preferences you have changed and if you have moved the iPhoto library repoint to it. This may help
    This does not affect your photos or any database information (keywords, faces, places, ratings, etc) in any way - they are stored in the iPhoto library - the iPhoto preference file simply controls how iPhoto works - which is why renewing it is a good first step.
    LN

  • How can I add my photo after setting up family share

    I Set family share as organizer and did not add a picture can i now add my picture.

    Hello there, bryan1953.
    The photo used by Family Sharing is based on your Contact card in Contacts. The following Knowledge Base article walks through how to add a photo:
    Contacts: Add or change contact pictures
    http://support.apple.com/kb/PH11580
    Add or change a picture
    Select a contact or your own card, and then double-click the picture well.
    Select a picture:
    Drag an image to the picture well.
    Click Camera to take a picture.
    If you have pictures in iPhoto, click the Faces button to use a face from iPhoto.
    If the contact is linked (it’s in multiple accounts, but displayed only once in Contacts) and has multiple account pictures, click Linked.
    Click Edit to edit the picture.
    To zoom in and out of the picture, drag the slider. Drag the picture to change which part of it is visible.
    To rotate the picture, hold down the Option key while you click and drag the picture.
    To apply special effects, click the Effects Gallery button and choose an effect.
    Click Done.
    Thanks for reaching out to Apple Support Communities.
    Cheers,
    Pedro.

  • How can I add pretest quiz after chapters/groups

    My project requires the learning to be divided into 17 chapters with pretests or quizzes at the end of each one then one final test at the end. It is restricting me from moving any quiz slides around in any segment type arrangements. It is locking all quiz slides into one group in the filmstrip view.
    Also to create a "chapter" I am naming and "grouping" accordingly. But no control over where I can section quizzes into the chapters. Please help. I'm on captivate 8 on Mac mavericks.

    Pretest slides have to be together at the start of the file, and will disable navigation by playbar or by TOC.
    It is a SCORM requirement that each file can have only one (real) quiz. Quiz slides can be distributed in a file, score slide will be at the end. If you need to report more quizzes, you have to split up your course in different files.

  • Can I add charter marks after iMovie and iDVD

    a few years ago I transfered old 16mm to my DV camcorder and then into iMovie and iDVD.
    I was looking at one of them for a family reunion and on one of the DVD's I somehow forgot to put in the last 3 chapter stops.
    Is there an easy way of redoing the DVD and adding the 3 chapter stops ?
    I made several color and lighting adjustments to them and i did not keep anything on the hard drive as I did two B/U's of them.
    Thanks
    Greg

    These links regarding chapter markers may also help thereafter:
    http://docs.info.apple.com/article.html?path=iDVD/6.0/en/277.html
    http://docs.info.apple.com/article.html?path=iDVD/6.0/en/418.html
    http://docs.info.apple.com/article.html?path=iDVD/6.0/en/417.html

  • Can't add list element when calling a method from another class

    I am trying to call a method in another class, which contains code listmodel.addElement("text"); to add an element into a list component made in that class.
    I've put in System.out.println("passed"); in the method just to make sure if the method was being called properly and it displays normally.
    I can change variables in the other class by calling the method with no problem. The only thing I can't do is get listmodel.addElement("text"); to add a new element in the list component by doing it this way.
    I've called that method within it's class and it added the element with no problem. Does Java have limitations about what kind of code it can run from other classes? And if that's the case I'd really like to know just why.

    There were no errors, just the element doesnt get added to the list by doing it this way
    class showpanel extends JPanel implements ActionListener, MouseMotionListener {
           framepanel fp = new framepanel();
           --omitted--
         public void actionPerformed(ActionEvent e){
                  if(e.getSource() == button1){
                       fp.addLayer();
    /*is in a different class file*/
    class framepanel extends JPanel implements ActionListener{
            --omitted--
         public void addLayer(){
              listmodel.addElement("Layer"+numLayer);
              numLayer++;
    }

Maybe you are looking for