0

The problem is then I switch between panes, my buttons and text-fields become not visible, but then I drag cursor over components, they appear. My Hierarchy looks similar to this:

JFrame

  • PanelWithComponents
  • _Button3
  • PanelWithPanels
  • _PanelOne
  • __Button1
  • __Label1(Background)
  • _PanelTwo
  • __Label2(Background)
  • __Button2

    private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                              
     if (jRadioButton1.isSelected()){
        CardLayout cl = (CardLayout)(bottomPanel.getLayout());
        cl.next(bottomPanel);
    
    }else{
        CardLayout cl = (CardLayout)(bottomPanel.getLayout());
        cl.next(bottomPanel);
    
    
     }
    

    }

1 Answers1

0

You should call the revalidate() and repaint() method when you switch the panes.

frame.getContentPane().revalidate();
frame.getContentPane().repaint();

From add method of Container.

Note: If a component has been added to a container that has been displayed, validate must be called on that container to display the new component. If multiple components are being added, you can improve efficiency by calling validate only once, after all the components have been added.

From remove method of Container.

Note: If a component has been removed from a container that had been displayed, validate() must be called on that container to reflect changes. If multiple components are being removed, you can improve efficiency by calling validate() only once, after all the components have been removed.

Edited as per camickr's comment.

4J41
  • 5,005
  • 1
  • 29
  • 41
  • I've tried to use it , but the frame gets underlined. I understand that it is JFrame, but I don't know how to get JFrame variable as an object. then I use the JFrame name, it doesn't seem to work. – user3100454 Dec 29 '13 at 11:26
  • Could you edit your question and add your code(atleast the portion where you make the switch between the panes). – 4J41 Dec 29 '13 at 11:29
  • I've tried to use it, but "frame" gets underlined. I understand that it is Jframe, but I can't get Jframe as an object. – user3100454 Dec 29 '13 at 11:32
  • can you just call `getContentPane().validate(); getContentPane().repaint();`? – 4J41 Dec 29 '13 at 11:33
  • @user3100454, when using Swing the code should be `revalidate(), repaint()`. – camickr Dec 29 '13 at 16:33