GUI Keyboard Simulator

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 1604 Views - Last Post: 25 February 2012 - 10:39 PM Rate Topic: -----

#1 wilson9493  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 21-February 12

GUI Keyboard Simulator

Posted 21 February 2012 - 02:57 PM

As you can see, when I try to run the program, the last 'button' of each line on the keyboard will locate on the start of the next line, can anyone help me with that?
cheers



public class StockTicker extends JFrame
{	
	String firstRow[] = {"1","2","3","4","5","6","7","8","9","0","Back\nSpace"};
	String secondRow[] = {"Q","W","E","R","T","Y","U","I","O","P"};
	String thirdRow[] = {"A","S","D","F","G","H","J","K","L","Enter"};
	String fourthRow[] = {"Z","X","C","V","B","N","M","."};
	 
	
	JButton first[] = new JButton[11];
	
	JButton second[] = new JButton[10];
	
        JButton third[] = new JButton[10];
	
	JButton fourth[] = new JButton[8];

	
	public StockTicker()
	{
		super ("A Simple Stock Market GUI");
		
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);
        
		initWidgets();
	}
	
	
	private void initWidgets()
	{
		

		JLabel jlOutput = new JLabel("Output: ");
		JLabel jlInput = new JLabel("Intput: ");
		final JLabel jtfOutput = new JLabel("");
		final JLabel jtfInput = new JLabel();
		
		
		
		setLayout(new BorderLayout());
		JPanel jpNorth = new JPanel();
		JPanel jpCenter = new JPanel();
		JPanel jpKeyboard = new JPanel();
		add( jpNorth, BorderLayout.NORTH);
		add( jpCenter, BorderLayout.CENTER);
		add(jpKeyboard, BorderLayout.SOUTH);
		
		jpNorth.setLayout(new BorderLayout());
		jpNorth.add(jlOutput, BorderLayout.WEST);
		jpNorth.add(jtfOutput, BorderLayout.SOUTH);
		
		jpCenter.setLayout( new BorderLayout());
		jpCenter.add(jlInput, BorderLayout.WEST);
		jpCenter.add(jtfInput, BorderLayout.CENTER);
		
		
		
		jpKeyboard.setLayout(new GridLayout(4,1));
		
		
		
		pack();
		
		
		for(int i = 0; i < firstRow.length ; i++)
		{
			first[i] = new JButton(firstRow[i]);
			jpKeyboard.add(first[i]);
		}
		
		for(int i = 0; i < secondRow.length; i++)
		{
			second[i] = new JButton(secondRow[i]);
			jpKeyboard.add(second[i]);
		}
		
		for(int i = 0; i <thirdRow.length; i++)
		{
			third[i] = new JButton(thirdRow[i]);
			jpKeyboard.add(third[i]);
		}
		
		for(int i = 0; i < fourthRow.length; i++)
		{
			fourth[i] = new JButton(fourthRow[i]);
			jpKeyboard.add(fourth[i]);
               }
}



public class Main 
{
	public static void main(String[] args) 
	{
		 JFrame window = new StockTicker();
		 window.setSize(600,500);	
	     window.setVisible(true);
		
	}

}





		}


Is This A Good Question/Topic? 0
  • +

Replies To: GUI Keyboard Simulator

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: GUI Keyboard Simulator

Posted 21 February 2012 - 03:04 PM

better to make jpKeyboad a new GridLayout(4,11)
when you fill second and third row add an empty new JLabel("") at the end

or even better make jpKeyboard a GridLayiout(4,1)
for each row add a new JPanel() of GridLayout(1,numberOfButtonInThatRow)

This post has been edited by pbl: 21 February 2012 - 03:06 PM

Was This Post Helpful? 0
  • +
  • -

#3 wilson9493  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 21-February 12

Re: GUI Keyboard Simulator

Posted 21 February 2012 - 03:11 PM

A bit of a mistake, I wasn't intended to add an empty JLabel ("") at the end of 2 and 3 row.

	String firstRow[] = {"1","2","3","4","5","6","7","8","9","0","Back\nSpace"};
	String secondRow[] = {"Q","W","E","R","T","Y","U","I","O","P"};
	String thirdRow[] = {"A","S","D","F","G","H","J","K","L","Enter"};
	String fourthRow[] = {"Z","X","C","V","B","N","M","."};



So how can I make the two buttons 'Back\nSpace" and "Enter" not locate in the next line, if I remove those empty JLabel?

Cheers mate
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: GUI Keyboard Simulator

Posted 21 February 2012 - 03:26 PM

Use my second solution will be a easier

	String firstRow[] = {"1","2","3","4","5","6","7","8","9","0","Back\nSpace"};
	String secondRow[] = {"Q","W","E","R","T","Y","U","I","O","P"};
	String thirdRow[] = {"A","S","D","F","G","H","J","K","L","Enter"};
	String fourthRow[] = {"Z","X","C","V","B","N","M","."};
	 
	
	JButton first[];
	JButton second[];
    JButton third[];
	JButton fourth[];

	
	public StockTicker()
	{
		JPanel jpKeyboard = new JPanel(new GridLayout(4,1));
        first = new JButton[firstRow.length];
        JPanel p = new JPanel(new GridLayout(1, firstRow.length));
        for(int i = 0; i < firstRow.length; ++i) {
        	first[i] = new JButton(firstRow[i]);
        	p.add(first[i]); 
        }
        jpKeyboard.add(p);
        
        second = new JButton[secondRow.length];
        p = new JPanel(new GridLayout(1, secondRow.length));
        for(int i = 0; i < seondRow.length; ++i) {
        	second[i] = new JButton(secondRow[i]);
        	p.add(second[i]); 
        }
        jpKeyboard.add(p);
		


Was This Post Helpful? 1
  • +
  • -

#5 wilson9493  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 21-February 12

Re: GUI Keyboard Simulator

Posted 21 February 2012 - 04:13 PM

Cheers mate, it's life saving.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: GUI Keyboard Simulator

Posted 21 February 2012 - 07:58 PM

Actually would be even easier with a [][] array

	String rows[][] = {
			{"1","2","3","4","5","6","7","8","9","0","Back\nSpace"},
			{"Q","W","E","R","T","Y","U","I","O","P"},
			{"A","S","D","F","G","H","J","K","L","Enter"},
			{"Z","X","C","V","B","N","M","."}
	};


	JButton[][] button;

	public StockTicker()
	{
		JPanel jpKeyboard = new JPanel(new GridLayout(rows.length,1));
		button = new JButton[rows.length][];
		for(int i = 0; i < rows.length; ++i) {
			JPanel p = new JPanel(new GridLayout(1, rows[i].length));
			button[i] = new JButton[rows[i].length];
			for(int j = 0; j < rows[i].length; ++j) {
			     button[i][j] = new JButton(rows[i][j]);
			     p.add(button[i][j]);
			}
			jpKeyboard.add(p);
		}


:^:
Was This Post Helpful? 1
  • +
  • -

#7 wilson9493  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 21-February 12

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?
Was This Post Helpful? 0
  • +
  • -

#8 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: GUI Keyboard Simulator

Posted 22 February 2012 - 11:32 AM

You can add a line in before you add it to the panel.
        for(int j = 0; j < rows[i].length; ++j) {
             button[i][j] = new JButton(rows[i][j]);
             p.add(button[i][j]);


Before you add it to the panel just call the button and give it an action listener.

button[i][j].addActionListener(this);

Then create an action listener method to handle it. When you create it be sure to use the getSource() method once like so:
public viod actionListner(ActionEvent e)
{
    Object command = e.getSource();
    if(command.getText().equals("Enter"))
        // perform code for enter button
    else if( ... )
}


Was This Post Helpful? 1
  • +
  • -

#9 wilson9493  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 21-February 12

Re: GUI Keyboard Simulator

Posted 22 February 2012 - 05:34 PM

Thank you for the respond, but would you mind explaining a bit more as I'm fairly new to GUI

Do I have to create an listener for each button?

Thanks again.
Was This Post Helpful? 0
  • +
  • -

#10 wilson9493  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 21-February 12

Re: GUI Keyboard Simulator

Posted 22 February 2012 - 05:42 PM

It would be good if you can show me how to show the letter pressed on the keyboard in the Inout area (:
Was This Post Helpful? 0
  • +
  • -

#11 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

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.


Make sense?

This post has been edited by Fuzzyness: 22 February 2012 - 06:52 PM

Was This Post Helpful? 1
  • +
  • -

#12 wilson9493  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 21-February 12

Re: GUI Keyboard Simulator

Posted 22 February 2012 - 07:51 PM

Yes, making a lot of sense, learnt a lot! will try to work on it!
appreciated it so much.
you guys have been very helpful. thanks again
will come to you guys again if anything more comes up.
Was This Post Helpful? 0
  • +
  • -

#13 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: GUI Keyboard Simulator

Posted 22 February 2012 - 07:58 PM

Sure thing buddy! Just post an issues that you run into and we can help.

Good luck with it!
Was This Post Helpful? 0
  • +
  • -

#14 wilson9493  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 21-February 12

Re: GUI Keyboard Simulator

Posted 23 February 2012 - 11:24 AM

Hi guys, here I am again, get stuck with something.

So I managed to get the buttons working, credit to you guys!

However, I want the application to query the Yahoo Online Service and print out the result when the user has input some thing and hit the return button.

Hope that's not a lot to ask.

Thanks in advance!
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: GUI Keyboard Simulator

Posted 23 February 2012 - 07:05 PM

View Postwilson9493, 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. :^:
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2