Re: GUI Keyboard Simulator

Posted 22 February 2012 - 11:21 AM

Thank you for the second solution.
I know this might sound a bit idiotic, but I'm wondering if I am to create some ActionListener to enable to functionality of the simulator I created,
which of the solution will be more suitable?

Re: GUI Keyboard Simulator

Posted 22 February 2012 - 06:25 PM

Ok so you do give it a listener. When you say addActionListener(this) you are telling the object to look at the actionPerformed method in the class rather than creating a new listener for each. So by saying "this" you can keep all of your buttons actions in the same Listener. That is why you do the comparison of what the letter should be.

Ok so here is what you will do that is fairly simple.

private JTextArea area = new JTextArea();
getContentPane().add(area);
private boolean shift = false;
public void actionPerformed(ActionEvent e)
{
// create and instantiate your objects
JButton button = new JButton();
Object command = e.getSource();
// check to see if it is instance of an button first
if(command instanceof JButton)
{
button = (JButton) command;
// Make a check to see if the button is the shift button, will save time instead of going to switch
if(button.getText().equals("Shift"))
{
//Change the shift boolean to opposite of what it is so it is like an On/Off mode
shift = !shift;
// Have a method to switch the labels on JButton to Capital and lowercase words.
if(shift)
toCapitals();
else toLowers();
}
else
{
if(button == someDifButton1)
{
//code
}
else if(button == someDifButton2)
{
//code
}
else switch(button.getText())
{
case "q" : area.append("q"); break; // Adds it to TextArea
case "Q" : area.append("Q"); break;
case "w" : area.append("w"); break;
// and so forth.. have a default for a button not pushed
default : JOptionPane.showMessageDialog(null, "Please contact Program Administrator - Program UnSyncd."); break;
} // end switch
// Put the rest of the buttons here that are not a key, so incase it is an different button will still get taken care of.
}// end the else statement if not Shift button
} // end the if statement for if instance of JButton
else // anything else that is not a JButton
{
if(command == someJLabel)
{
// code
}
else if(command == someJLabel2)
{
// code
}
}// end else statement for it not instanceof JButton
}// end actionPerformed method.

Re: GUI Keyboard Simulator

Posted 23 February 2012 - 07:05 PM

wilson9493, on 23 February 2012 - 01:24 PM, said:

Hope that's not a lot to ask.

Don't want to be rude but based on the really basic questions you asked in the previous posts Yes this is a lot.
I think you will have to practice your Java GUIs for a few weeks before trying to attempt such a thing.