Hello guys, I am stuck again with a new problem.
I need help with 3 things:
1. Timer
2. State Pattern
3. Disabling the button on JFrame
lets go with the easiest one in the list, the Disabling of button one.
I have an app with buttons, and I need to disable all the buttons on it for certain period of time. Lets forget the time part.. I just want to disable the buttons on the frame.
BEFORE YOU GO ON SUGGESTING ME THE FUNCTION, I ALREADY HAVE TRIED "button.setEnabled(false);" AND IT DOESNT WORK! I dont know why :/
here's my source code:
myloader.java
/**
* @(#)loader.java
*
*
* @author
* @version 1.00 2010/6/24
*/
package uniKEY;
import java.*;
import java.awt.*;
import javax.swing.*;
import java.lang.*;
public class myLoader extends JFrame
{
private myUI _uiobj;
public myLoader()
{
_uiobj = new myUI();
setSize(180,300);
setTitle("UniKEY");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
getContentPane().add(_uiobj);
}
public static void main(String[] args)
{
new myLoader();
}
}
myUI.java
/**
* @(#)ui.java
*
*
* @author
* @version 1.00 2010/6/24
*/
package uniKEY;
import java.*;
import java.awt.*;
import java.awt.image.*;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
class myUI extends JPanel implements ActionListener
{
private JPanel _mainpanel;
private JPanel _lightpanel;
private JButton _button[];
private JPanel _keypadpanel;
private String _dbPass;
private String _userPass;
private String _clickValue;
private int _click = 0;
private JLabel _imagelabel;
private Light _myLight;
private Timer _timer;
private static final int WAIT = 0;
private static final int YES = 1;
private static final int NO = 2;
private int _currentState;
public myUI()
{
_clickValue= null;
_userPass= null;
loadDB();
renderLayout();
renderFrame();
}
public void renderLayout()
{
_mainpanel = new JPanel();
_keypadpanel = new JPanel();
_lightpanel = new JPanel();
_button = new JButton[12];
_mainpanel.setLayout(new BorderLayout());
_keypadpanel.setLayout(new GridLayout(4,3,0,0));
_lightpanel.setLayout(new FlowLayout());
for (int i=0; i<=9; i++)
{
_button[i]= new JButton(String.valueOf(i));
_button[i].setPreferredSize(new Dimension(55,55));
}
_button[10] = new JButton("*");
_button[0] = new JButton("0");
_button[11] = new JButton("#");
for (int i=1; i<=3; i++)
{
_keypadpanel.add(_button[i]);
}
for (int i=4; i<=6; i++)
{
_keypadpanel.add(_button[i]);
}
for (int i=7; i<=9; i++)
{
_keypadpanel.add(_button[i]);
}
_keypadpanel.add(_button[10]);
_keypadpanel.add(_button[0]);
_keypadpanel.add(_button[11]);
for(int i=0;i<12;i++)
{
_button[i].addActionListener(this);
}
_myLight = new Light();
//_myLight.setImage();
_currentState=WAIT;
_lightpanel.add(_myLight);
}
public void renderFrame()
{
_mainpanel.add(_keypadpanel,BorderLayout.NORTH);
_mainpanel.add(_lightpanel,BorderLayout.SOUTH);
this.add(_mainpanel);
}
public void loadDB()
{
try
{
FileInputStream _fis = new FileInputStream("myfile.txt");
DataInputStream _dis = new DataInputStream(_fis);
BufferedReader _br = new BufferedReader(new InputStreamReader(_dis));
_dbPass=_br.readLine();
_dis.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(_mainpanel,"Error Occoured!\n"+e.getMessage()+"\n");
System.exit(0);
}
}
public void resetState()
{
_click=0;
if((_currentState==YES)||(_currentState==NO))
{
_currentState=WAIT;
for(int i=0;i<12;i++)
{
_button[i].setEnabled(true);
}
}
}
private void check()
{
if(_userPass.equals(_dbPass))
{
if(_currentState == WAIT)
{
_currentState = YES;
_myLight.flipImage("go");
for(int i=0;i<12;i++)
{
_button[i].setEnabled(false);
}
resetState();
}
}
else
{
if(_currentState == WAIT)
{
_currentState = NO;
_myLight.flipImage("stop");
for(int i=0;i<12;i++)
{
_button[i].setEnabled(false);
}
resetState();
}
}
}
private void reportError()
{
JOptionPane.showMessageDialog(_mainpanel,"An Error Occoured!\nThe Application needs to be restarted!\n");
System.exit(0);
}
private void processInput(String _value)
{
if(_click==0)
{
_userPass=_value;
_click++;
}
else if(_click>0)
{
_userPass+=_value;
_click++;
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == _button[0])
{
_clickValue = _button[0].getText();
if(_click < 3)
{
processInput(_clickValue);
}
else if(_click ==3)
{
processInput(_clickValue);
check();
}
else
{
reportError();
}
}
else if(e.getSource() == _button[1])
{
_clickValue = _button[1].getText();
if(_click <3)
{
processInput(_clickValue);
}
else if(_click ==3)
{
processInput(_clickValue);
check();
}
else
{
reportError();
}
}
else if(e.getSource() == _button[2])
{
_clickValue = _button[2].getText();
if(_click <3)
{
processInput(_clickValue);
}
else if(_click ==3)
{
processInput(_clickValue);
check();
}
else
{
reportError();
}
}
else if(e.getSource() == _button[3])
{
_clickValue = _button[3].getText();
if(_click <3)
{
processInput(_clickValue);
}
else if(_click ==3)
{
processInput(_clickValue);
check();
}
else
{
reportError();
}
}
else if(e.getSource() == _button[4])
{
_clickValue = _button[4].getText();
if(_click <3)
{
processInput(_clickValue);
}
else if(_click ==3)
{
processInput(_clickValue);
check();
}
else
{
reportError();
}
}
else if(e.getSource() == _button[5])
{
_clickValue = _button[5].getText();
if(_click <3)
{
processInput(_clickValue);
}
else if(_click ==3)
{
processInput(_clickValue);
check();
}
else
{
reportError();
}
}
else if(e.getSource() == _button[6])
{
_clickValue = _button[6].getText();
if(_click <3)
{
processInput(_clickValue);
}
else if(_click ==3)
{
processInput(_clickValue);
check();
}
else
{
reportError();
}
}
else if(e.getSource() == _button[7])
{
_clickValue = _button[7].getText();
if(_click <3)
{
processInput(_clickValue);
}
else if(_click ==3)
{
processInput(_clickValue);
check();
}
else
{
reportError();
}
}
else if(e.getSource() == _button[8])
{
_clickValue = _button[8].getText();
if(_click <3)
{
processInput(_clickValue);
}
else if(_click ==3)
{
processInput(_clickValue);
check();
}
else
{
reportError();
}
}
else if(e.getSource() == _button[9])
{
_clickValue = _button[9].getText();
if(_click <3)
{
processInput(_clickValue);
}
else if(_click ==3)
{
processInput(_clickValue);
check();
}
else
{
reportError();
}
}
else if(e.getSource() == _button[10])
{
_clickValue = _button[10].getText();
if(_click <3)
{
processInput(_clickValue);
}
else if(_click ==3)
{
processInput(_clickValue);
check();
}
else
{
reportError();
}
}
else if(e.getSource() == _button[11])
{
_clickValue = _button[11].getText();
if(_click <3)
{
processInput(_clickValue);
}
else if(_click ==3)
{
processInput(_clickValue);
check();
}
else
{
reportError();
}
}
else
{
reportError();
}
}
}
package uniKEY;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class Light extends JLabel
{
public Light()
{
this.setIcon(resizeImage(new ImageIcon("wait.jpg")));
}
public void flipImage(String _state)
{
this.setIcon(resizeImage(new ImageIcon(_state+".jpg")));
}
private ImageIcon resizeImage(ImageIcon _myImage)
{
Image _img = _myImage.getImage();
Image _newimg = _img.getScaledInstance(50, 38,Image.SCALE_SMOOTH);
ImageIcon _newIcon = new ImageIcon(_newimg);
return _newIcon;
}
}
Light.java
1111
myfile.txt
and the following are the images used in the app:
The app runs finely without any problems. The actual behaviour of the app must be something like this:
when a password is entered right, a green image must be replaced by default one and keypad must freeze for few seconds, and unfreeze (disable/enable) along with resetting the image by original one and if a wrong password is entered, a red image must be replaced by default image and the process continues...
I have done the image part, they do change without any problems, now the buttons wont disable. I dont know why but they dont :(
can anyone please look into the code and help me out..?
thanks in advance