1

This is my code:

private class ValueReporter implements ListSelectionListener {

    public void valueChanged(ListSelectionEvent event) {
        if (!event.getValueIsAdjusting()) {
            jTextField9.setText(jList2.getSelectedValue().toString());
            JLabel someLabel = new JLabel("Some new Label");
            jPanel7.add(someLabel);
            jPanel7.revalidate();
        }
    }
}

The "jTextField9" get's update with the text but the panel "jPanel7" doesn't update the new label that was assigned to it.

Starkey
  • 9,673
  • 6
  • 31
  • 51
Tom
  • 9,275
  • 25
  • 89
  • 147
  • 3
    for better help sooner post an [SSCCE](http://sscce.org/) – mKorbel Jun 25 '12 at 18:39
  • have you tried calling `revalidate()` on the frames instance rather then the panels? – David Kroukamp Jun 25 '12 at 18:39
  • @DavidKroukamp i'm getting an exception when i try to revalidate the frame: Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError – Tom Jun 25 '12 at 18:54
  • Not sure, Post all the relevant code or as mKorbel suggested do a small example to illustrate the problem, right now i hardly have anything to work with but a single class? – David Kroukamp Jun 25 '12 at 18:56
  • You may need to call repaint() on the panel. See [Java Swing revalidate() vs repaint()](http://stackoverflow.com/q/1097366/1048330). Post your [SSCCE](http://sscce.org/) that demonstrates the problem. – tenorsax Jun 25 '12 at 20:19

1 Answers1

2

Here a code snippet I quickly threw together which adds labels to a JPanel and repaints

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class UpdatingJPanel extends JPanel{

  public UpdatingJPanel() {
    Timer timer = new Timer( 1500, new ActionListener() {
      @Override
      public void actionPerformed( ActionEvent e ) {
        UpdatingJPanel.this.add( new JLabel( "A label" ) );
        UpdatingJPanel.this.revalidate();
        UpdatingJPanel.this.repaint();

        if( UpdatingJPanel.this.getComponentCount() == 0 ){
          ( ( Timer ) e.getSource() ).stop();
        }
      }
    } );
    timer.setRepeats( true );
    timer.start();
  }

  public static void main( String[] args ) {
    EventQueue.invokeLater( new Runnable() {
      @Override
      public void run() {
        JFrame testFrame = new JFrame(  );
        testFrame.getContentPane().add( new UpdatingJPanel() );
        //not using pack() as the panel is still empty and I want avoid
        //resizing when adding labels as that triggers a repaint
        testFrame.setSize( 200,200 );
        testFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        testFrame.setVisible( true );
      }
    } );

  }
}
Robin
  • 36,233
  • 5
  • 47
  • 99
  • +1 for reference sample with clear comments, but don't forget [Initial Threads](http://download.oracle.com/javase/tutorial/uiswing/concurrency/initial.html). – trashgod Jun 25 '12 at 23:22
  • @trashgod Correct. Updated the main method to call the code on the EDT – Robin Jun 26 '12 at 05:48