← Back to Kurakani

Java queston.

Original Post

Hi can someone help me disappear the button on click..... i have the windowDestroyer and the driver class mport javax.swing.*; import java.awt.*; import java.awt.event.*; /** Simple demonstration of putting buttons in a JFrame. */ public class ButtonDemo extends JFrame implements ActionListener { public static final int WIDTH = 400; public static final int HEIGHT = 300; public ButtonDemo( ) { setSize(WIDTH, HEIGHT); WindowDestroyer listener = new WindowDestroyer( ); addWindowListener(listener); Container contentPane = getContentPane( ); contentPane.setBackground(Color.WHITE); contentPane.setLayout(new FlowLayout( )); JButton sunnyButton = new JButton("Sunny"); sunnyButton.addActionListener(this); contentPane.add(sunnyButton); sunnyButton.setVisible(false); JButton cloudyButton = new JButton("Cloudy"); cloudyButton.addActionListener(this); contentPane.add(cloudyButton); } public void actionPerformed(ActionEvent e) { String actionCommand = e.getActionCommand( ); Container contentPane = getContentPane( ); JButton sunnyButton = new JButton("Sunny"); JButton cloudyButton = new JButton("Cloudy"); if (actionCommand.equals("Sunny")) contentPane.setBackground(Color.BLUE); sunnyButton.setVisible(false);/*SHOULDN'T THIS METHOD MAKE BUTTON DISAPPEAR BUT IT DOESN'T WORK!!!! HELP PLZ*/ else if (actionCommand.equals("Cloudy")) contentPane.setBackground(Color.GRAY); else System.out.println("Error in button interface."); }

pat
Dec 8, 2006 9:08 PM · 4,302 views
3 Replies
spp4045 Dec 8, 2006 10:44 PM
HowardRoark Dec 8, 2006 11:19 PM
This might give a better answer to you. http://forum.java.sun.com/thre... I have faced similar problems too when I tried to add buttions outside of init(). So I choose to do the Enable and Disable. You dont have to change the layout too. But what when to want to show a picture upon clicking the button? The picture will come on the way. It seems setVisible doesnt work anywhere outside.
divdude Dec 9, 2006 4:48 AM
class{ Define all the swing variables as globals main() decideWhereToPlaceAllComponents(); updatewindow();//set visible class ButtonListener{ onclick(){ container.remove(button); updatewindow(); } } I think it will work even without updating but if it doesnt just update it.
Login to join the conversation.

You might be interested in...

Recent Classifieds View all
Upcoming Events View all
Service Providers View all
Ask Sajha AI Assistant