Trying to update my JFrame, why won't repaint work?

10,257

Any time you add or remove a component, you must tell its container to re-layout the current components it holds. You do this by calling revalidate() on it. You would then call repaint() after the revalidate call to have the container repaint itself.

public void actionPerformed( ActionEvent e ) {
    if ( passEquals( password ) && clickAgain == false ) {
        image2 = new ImageIcon( "openD.png" );
        remove( label );

        label = new JLabel( "Good Job! Here is the journal of dreams.", 
             image2 , JLabel.LEFT );
            add( label );

            clickAgain = true;
    }
    revalidate(); // **** added ****
    repaint();
}

Note: your question is worded in such a way as if you assume that we know what you're trying to do. Please give us more information next time. The better and more informative the question, the better and more informative the answer.

Edit 2:
I wonder if you could simplify your code a bit. Instead of removing and adding a JLabel, better to just simply set the current JLabel's text and Icon:

public void actionPerformed( ActionEvent e ) {
    if ( passEquals( password ) && clickAgain == false ) {
        image2 = new ImageIcon( "openD.png" );
        // remove( label );  // removed

        label.setText( "Good Job! Here is the journal of dreams.");
        label.setIcon(image2);
    }
}
Share:
10,257
Mark de la Cruz
Author by

Mark de la Cruz

Updated on June 04, 2022

Comments

  • Mark de la Cruz
    Mark de la Cruz almost 2 years

    I will run the program, but when I activate the event, the JFrame will not update (it only removes the JLabel ) unless I manually drag the window to resize it, even with the repaint() being called after the event takes place. What's wrong?

    public Driver() {
        setLayout( new FlowLayout() );
    
        pass = new JPasswordField( 4 );
            add( pass );
    
        image = new ImageIcon( "closedD.png" );
        label = new JLabel( "Enter the password to enter the journal of dreams" , image , JLabel.LEFT );
            add( label );
    
        button = new JButton( "Enter" );
            add( button );
    
        event e = new event();
            button.addActionListener( e );
    
        setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        setVisible( true );
        setSize( 1600/2 , 900/2 );
        setTitle( "Diary" );
    }
    
    //main method
    //
    //
    public static void main( String[] args ) {
        win = new Driver();
    }
    
    public class event implements ActionListener {
        private boolean clickAgain = false;
    
        public void actionPerformed( ActionEvent e ) {
            if ( passEquals( password ) && clickAgain == false ) {
                image2 = new ImageIcon( "openD.png" );
                remove( label );
    
                label = new JLabel( "Good Job! Here is the journal of dreams." , image2 , JLabel.LEFT );
                    add( label );
    
                    clickAgain = true;
            }
    
            repaint();
        }
    }
    
  • Andrew Thompson
    Andrew Thompson about 11 years
    My bad. I missed your edit. Agree CL would be overkill here. +1