Thread: JButton Help

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    1
    Rep Power
    0

    JButton Not Clickable


    When I launch my choices applet, it works just fine, but if I close it and launch it again, the save and exit button doesn't want to work? (It shows up but this time, it's not clickable) Is there any way I can fix this so that it is clickable after the first time?

    Heres the source code for my options class:

    Code:
    package com.game.labyrinth.gui;
    
    import java.awt.Choice;
    import java.awt.Dimension;
    import java.awt.Rectangle;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    import com.game.labyrinth.Configuration;
    
    public class Options extends JFrame{
    	private static final long serialVersionUID = 1L;
    	
    	private int width = 550;
    	private int height = 450;
    	private JTextField twidth, theight;
    
    	private JLabel lwidth, lheight, custom, choose;
    	static JButton SAE;
    	static Rectangle rSAE, rresolution;
    	Choice resolution = new Choice();
    	Configuration config = new Configuration();
    	Toolkit toolkit = Toolkit.getDefaultToolkit();
    	Dimension dim = toolkit.getScreenSize();
    	
    	int w = 0;
    	int h = 0;
    	private int button_width = 110;
    	private int button_height = 40;
    	
    	static JPanel window = null;
    	
    	public Options(int id){
    		
    		if(window == null){
    			 window = new JPanel();
    			 window.setLayout(null);
    			 drawButtons();
    		}
    		
    		setTitle("Labyrinth Options");
    		setSize(new Dimension(width, height));
    		add(window);
    		setLocationRelativeTo(null);
    		setResizable(false);
    		setVisible(true);
    		window.repaint();
    	}
    
    	public void drawButtons(){
    		SAE = new JButton("Save and Exit");
    		rSAE = new Rectangle((width - 110), (height - 70), button_width, button_height - 10);
    		SAE.setBounds(rSAE);
    		window.add(SAE);
    		
    		rresolution = new Rectangle(6, 80, 160, 25);
    		resolution.setBounds(rresolution);
    		resolution.add("640, 480 - Fastest");
    		resolution.add("800, 600 - Fast");
    		resolution.add("1024, 768 - Slow");
    		resolution.add("Fullscreen - Slowest");
    		window.add(resolution);
    		
    		lwidth = new JLabel("Width: ");
    		lwidth.setBounds(6, 150, 120, 20);
    		window.add(lwidth);
    			
    		lheight = new JLabel("Height: ");
    		lheight.setBounds(6, 180, 120, 20);
    		window.add(lheight);
    		
    		twidth = new JTextField();
    		twidth.setBounds(50, 150, 60, 20);
    		window.add(twidth);
    			
    		theight = new JTextField();
    		theight.setBounds(50, 180, 60, 20);
    		window.add(theight);
    			
    		choose = new JLabel("Choose a resolution:");
    		choose.setBounds(8, 60, 160, 25);
    		window.add(choose);
    			
    		custom = new JLabel("Or type a custom resolution:");
    		custom.setBounds(6, 130, 210, 20);
    		window.add(custom);
    		
    		SAE.addActionListener(new ActionListener(){
    			
    				public void actionPerformed(ActionEvent e){
    					dispose();
    					config.saveConfiguration("width", parseWidth());
    					config.saveConfiguration("height", parseHeight());
    				}
    			}
    		);
    	}
    	
    	private void drop(){
    		int selection = resolution.getSelectedIndex();
    		
    		if(selection == 0){
    			w = 640;
    			h = 480;
    		}
    		
    		if(selection == 1 || selection == -1){
    			w = 800;
    			h = 600;
    		}
    		
    		if(selection == 2){
    			w = 1024;
    			h = 768;
    		}
    		
    		if(selection == 3){
    			w = (int) dim.getWidth();
    			h = (int) dim.getHeight();
    		}
    	}
    	
    	private int parseWidth(){
    		
    		try{
    			int w = Integer.parseInt(twidth.getText());
    			return w;
    		}
    		
    		catch(NumberFormatException e){
    			drop();
    			return w;
    		}
    	}
    	
    	private int parseHeight(){
    		
    		try{
    			int h = Integer.parseInt(theight.getText());
    			return h;
    		}
    		
    		catch(NumberFormatException e){
    			drop();
    			return h;
    		}
    	}
    }
    Last edited by ts4437; February 19th, 2013 at 12:20 PM. Reason: Wanted to make title more specific
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,719
    Rep Power
    348
    Did you post the correct code? I don't see an applet in what is posted.

    How can this code be compiled and executed for testing? I don't see a main() method for starting execution.

    If its part of a big program, make a smaller simpler program that compiles, executes and shows the problem. Don't post too much code.

IMN logo majestic logo threadwatch logo seochat tools logo