Colden Cullen
Colden Cullen

Reputation: 78

Buttons Don't Initially Draw

I'm having an issue with my applet where the buttons do not draw until you mouse over them. They stay after that, but once you click one, they go away again. Here is my code:

    checkPanel = new JPanel();

    buttonPanel = new JPanel();
    buttonPanel.setLayout( new GridLayout( 1, 7 ) );

    openAdvancedSettings = new JButton( "Advanced Settings" );
    openAdvancedSettings.addActionListener( this );

    checkPanel = new JPanel();
    checkvolt = new Checkbox( "Voltage", true );
    checkv = new Checkbox( "v gate", false );
    checkw = new Checkbox( "w gate", false );
    checks = new Checkbox( "s gate", false );

    checkvolt.addItemListener( this );
    checkv.addItemListener( this );
    checkw.addItemListener( this );
    checks.addItemListener( this );

    checkPanel.setLayout( new GridLayout( 20, 8 ) );
    checkPanel.add( butp );
    checkPanel.add( prompt0 );
    checkPanel.add( checkvolt );
    checkPanel.add( prompt1 );

    butt = new JButton( "Start" );
    butt.addActionListener( this );
    buttonPanel.add( butt );

    setLayout( new BorderLayout() );
    add( checkPanel, BorderLayout.EAST );
    add( buttonPanel, BorderLayout.NORTH );
    add( openAdvancedSettings, BorderLayout.SOUTH );

The first 2 adds are JPanels, and the last one is a JButton. Most of the buttons (all but the last one) are in those 2 panels.

I have also come to the conclusion that the method repaint() is causing this to happen. What am I doing wrong?

Upvotes: 3

Views: 157

Answers (2)

Andrew Thompson
Andrew Thompson

Reputation: 168825

  • Don't mix Swing (e.g. JPanel) & AWT (e.g. CheckBox) components. Comment out any AWT import(s) and check that only layouts are missing.
  • Ensure that validate() is called after all components are added.
  • And as Gagandeep Bali advised:- Construct the GUI on the EDT, and post an SSCCE if you cannot get it sorted.

Upvotes: 3

nIcE cOw
nIcE cOw

Reputation: 24616

I am assuming in the absence of a good SSCCE, that you must have failed to put your code, that is responsible for creating and displaying your GUI, for attaining Concurrency in Swing, inside SwingUtilities.invokeLater(...); Secondly, you can try calling revalidate() method, on your panel, before calling repaint(). That might can solve the problem for the time being.

Hope that might help in some way. Regards

Upvotes: 4

Related Questions