Results 1 to 5 of 5
  1. #1
    JG4m3r is offline Member
    Join Date
    Mar 2012
    Posts
    16
    Rep Power
    0

    Question JFrame freezes when button is clicked

    Hi!
    I've ran into a pretty anoying problem that I just can't seem to solve myself, so I'm asking you guys for advice instead.
    Ok, so I've got a JFrame with a TextField, a Button and a ActionListener for the button. I get no errors or anything, but when i try to click the button the program freezes. For those of you wondering I've got javafx_sdk-2_0_1-windows-x64, jdk-6u29-windows-x64 and jdk-7u1-windows-x64

    Code:
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import java.lang.*;
    
    class SimonStopper extends JFrame{
    	
    	static boolean yes = new Boolean(true);
    	static int maxX;
    	static int maxY;
    	int k = 0;
    	int l = 0;
    	
    	//char[] letters = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 
    	//'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'z'};
    	
    	static String information_text = new String("Skriv in riktig passord for å låse opp pcen");
    	static String input_text = new String("Skriv in passord her");
    	static String confirm_text = new String("Confirm");
    	
    	static JPanel panel = new JPanel();
    	static JLabel information = new JLabel(information_text);
    	public static JPasswordField input = new JPasswordField(100);
    	static JButton confirm = new JButton(confirm_text);
    	static String password = new String("ipconfig");
    	ActionHandler handle = new ActionHandler();
    	
    	public static void main(String[] args){
    		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    		maxX = screenSize.width;
    		maxY = screenSize.height - 50;
    		
    		//Makes window
    		SimonStopper vindu = new SimonStopper();
    		vindu.setLocation(0, 0);
    		vindu.setSize(maxX, maxY);
    		vindu.setAlwaysOnTop(true);
    		vindu.setUndecorated(true);
    		vindu.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    		//vindu.setExtendedState(vindu.getExtendedState()|JFrame.MAXIMIZED_BOTH);
    		vindu.setResizable(false);
    		//setIconImage(). //FOR THE FUTURE
    		vindu.setVisible(true);
    	}
    	
    	public SimonStopper(){		
    		//Sets the layouts
    		setLayout(new BorderLayout());
    		panel.setLayout(new BorderLayout());
    		
    		//Adds stuff to the screen
    		panel.add(information, BorderLayout.PAGE_START);
    		panel.add(input, BorderLayout.LINE_START);
    		panel.add(confirm, BorderLayout.CENTER);
    		add(panel, BorderLayout.PAGE_START);
    		
    		input.requestFocusInWindow();
    		
    		//ActionListener
    		//input.addActionListener(this);
    		confirm.addActionListener(handle);
    		
    		addComponentListener(new ComponentAdapter() {
    			public void componentMoved(ComponentEvent e) {
    				setLocation(0,0);
    			}
    		});
    	}
    		
    	
    	
    	private static boolean isPasswordCorrect(char[] input){
    		char[] passW = {'i', 'p', 'c', 'o', 'n', 'f', 'i', 'g'};
    		boolean isCorrect = false;
    		
    		if (input.length != passW.length) {
    			isCorrect = false;
    		} else {
    			isCorrect = Arrays.equals(input, passW);
    		}
    		
    		return isCorrect;
    	}
    	
    	class ActionHandler implements ActionListener{
    		public void actionPerformed(ActionEvent e){	
    			SimonStopper stopper = new SimonStopper();
    			char[] inputPassword = input.getPassword();
    			System.out.println(inputPassword);
    			//stopper.windowActivated();
    			/*Runtime run = Runtime.getRuntime();
    			try{
    			Process p = run.exec("C:/Jacob/AntiTyveri/CompileAndRun.bat");
    			}catch(Exception error){
    				error.printStackTrace();
    			}*/
    			
    			if(stopper.isPasswordCorrect(inputPassword)){
    				System.exit(0);
    				System.out.println("correct");
    			}else{
    				System.out.println("incorrect");
    			}
    		}
    	}
    }
    Last edited by JG4m3r; 04-05-2012 at 12:06 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: JFrame freezes when button is clicked

    As you have the code at the moment it is very hard to debug. Consider commenting out the line which maximizes the window so you can see output from the program (or your IDE if you're using one.)

    Next I would suggest removing the static things. main() is required to be static, but for the rest I would suggest declaring things as "private" unless you have a reason not to. (keeping the compiler quiet is not a reason! Compiler messages are generally very useful indicators of a problem.)

    Related to this is the way you create a second frame when the button is clicked. You then (in the constructor) take components that used to be in the first frame and move them to the second. The gui is left in a rather odd state which could well be why it appears "frozen" to the user. Consider what you want the action listener to actually do and find a way to do that without creating a second frame.
    Last edited by pbrockway2; 04-05-2012 at 12:45 AM.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: JFrame freezes when button is clicked

    Thinking about this...

    No-one likes to be told to throw their code away and start again, so I won't suggest that. But if you said what you wanted this SimonStopper frame to actually do, someone might be able to suggest an approach that was simpler. It's strikes me that what you have is overly complex. (not that I know exactly what it's supposed to do ;)

    -----

    I'll move this thread to the Swing forum - because that's where all the Swing expertise is.

  4. #4
    JG4m3r is offline Member
    Join Date
    Mar 2012
    Posts
    16
    Rep Power
    0

    Default Re: JFrame freezes when button is clicked

    The point of this program is to make a frame/window that displays in front of everything, that you can not close, move or minimize. The only way that you are supposed to possibly close down the frame is by entering the correct password.
    Yes, i know that you can get around this by pressing: ctrl+alt+delete, alt+tab or the windows button, but these are problems that I will solve later.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: JFrame freezes when button is clicked

    Quote Originally Posted by JG4m3r View Post
    The point of this program is to make a frame/window that displays in front of everything, that you can not close, move or minimize. The only way that you are supposed to possibly close down the frame is by entering the correct password.
    Yes, i know that you can get around this by pressing: ctrl+alt+delete, alt+tab or the windows button, but these are problems that I will solve later.
    1. If this is not for some sort of kiosk application, you will have some very angry users if they try to use your program and then find themselves stuck.
    2. And regardless, Java isn't the best language for implementing this sort of behavior as it requires that you alter the basic behavior of the operating system, something that Java doesn't do well at all.

Similar Threads

  1. Replies: 1
    Last Post: 02-11-2012, 04:07 AM
  2. returning a string when button clicked
    By droidus in forum AWT / Swing
    Replies: 13
    Last Post: 02-02-2012, 04:50 AM
  3. form appear after ok button clicked
    By sks in forum NetBeans
    Replies: 1
    Last Post: 06-01-2011, 09:50 AM
  4. show panel when button clicked
    By Aggror in forum New To Java
    Replies: 6
    Last Post: 10-14-2010, 05:05 PM
  5. Replies: 11
    Last Post: 02-13-2009, 01:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •