Problem with JTextField [Beginner]
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Problem with JTextField [Beginner]

  1. #1
    Join Date
    Nov 2012
    Posts
    3

    Question Problem with JTextField [Beginner]

    Hello people,

    i'm making virtual keyboard and i stucked at smth silly but dunno how to solve it

    Code:
    JButton btnC = new JButton("C");
    		btnC.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				text = String.format(" %s", "c");
     
    				Text_t.setText(text);
    now i dont understand, when i press C, and after it i press N for example , it deletes C and writes N, what i need to add here to make it write normally.
    Or maybe there is some other way than mine to do it , so if u can write an example. I'm beginner in this.

    Thank you

  2. #2
    Join Date
    Nov 2012
    Posts
    3

    Re: Problem with JTextField [Beginner]

    Here is the full list of code:

    Code:
    import java.awt.Event;
    import java.awt.EventQueue;
    import java.awt.Window;
    
    import javax.swing.JFrame;
    import java.awt.GridLayout;
    import javax.swing.JButton;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.BoxLayout;
    import com.jgoodies.forms.layout.FormLayout;
    import com.jgoodies.forms.layout.ColumnSpec;
    import com.jgoodies.forms.layout.RowSpec;
    import com.jgoodies.forms.factories.FormFactory;
    import net.miginfocom.swing.MigLayout;
    import javax.swing.JRadioButton;
    import java.awt.BorderLayout;
    import javax.swing.JFormattedTextField;
    
    
    public class Keyboard1 {
    
    	private JFrame frame;
    	
    	JFormattedTextField Text_t = new JFormattedTextField();
    	String text = "smth";
    
    
    
    
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				
    		
    				
    				try {
    					Keyboard1 window = new Keyboard1();
    					window.frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
    
    	/**
    	 * Create the application.
    	 */
    	public Keyboard1() {
    		initialize();
    		
    
    		
    		
    	}
    	
    
    
    	/**
    	 * Initialize the contents of the frame.
    	 */
    	private void initialize() {
    		frame = new JFrame();
    		frame.setBounds(100, 100, 860, 304);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.getContentPane().setLayout(null);
    		
    		JButton btnSpace = new JButton("SPACE");
    		btnSpace.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    			}
    		});
    		btnSpace.setBounds(236, 225, 259, 30);
    		frame.getContentPane().add(btnSpace);
    		
    		JButton btnEsc = new JButton("ESC");
    		btnEsc.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				
    				
    			}
    		});
    		btnEsc.setBounds(10, 11, 81, 40);
    		frame.getContentPane().add(btnEsc);
    		
    		JButton btnTab = new JButton("Tab");
    		btnTab.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    			}
    		});
    		btnTab.setBounds(10, 62, 95, 40);
    		frame.getContentPane().add(btnTab);
    		
    		JButton btnCaps = new JButton("Caps");
    		btnCaps.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    			}
    		});
    		btnCaps.setBounds(10, 106, 108, 40);
    		frame.getContentPane().add(btnCaps);
    		
    		JButton btnShift = new JButton("Shift");
    		btnShift.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    			}
    		});
    		btnShift.setBounds(10, 159, 108, 30);
    		frame.getContentPane().add(btnShift);
    		
    		JButton btn1 = new JButton("\u00B8\u00A8");
    		btn1.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    			}
    		});
    		btn1.setBounds(98, 11, 45, 40);
    		frame.getContentPane().add(btn1);
    		
    		JButton btn_1 = new JButton("\u00B8\u00A8");
    		btn_1.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    			}
    		});
    		
    		JButton btn1f = new JButton("1");
    		btn1f.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    			}
    		});
    		btn1f.setBounds(145, 11, 45, 40);
    		frame.getContentPane().add(btn1f);
    		
    		JButton btn2 = new JButton("2");
    		btn2.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    			}
    		});
    		btn2.setBounds(191, 11, 45, 40);
    		frame.getContentPane().add(btn2);
    		
    		JButton btn3 = new JButton("3");
    		btn3.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    			}
    		});
    		btn3.setBounds(236, 11, 45, 40);
    		frame.getContentPane().add(btn3);
    		
    		JButton btn4 = new JButton("4");
    		btn4.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    			}
    		});
    		btn4.setBounds(281, 11, 45, 40);
    		frame.getContentPane().add(btn4);
    		
    		JButton btn5 = new JButton("5");
    		btn5.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    			}
    		});
    		btn5.setBounds(327, 11, 45, 40);
    		frame.getContentPane().add(btn5);
    		
    		JButton btn6 = new JButton("6");
    		btn6.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    			}
    		});
    		btn6.setBounds(372, 11, 45, 40);
    		frame.getContentPane().add(btn6);
    		
    		JButton btn7 = new JButton("7");
    		btn7.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    			}
    		});
    		btn7.setBounds(417, 11, 45, 40);
    		frame.getContentPane().add(btn7);
    		
    		JButton bnt8 = new JButton("8");
    		bnt8.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    			}
    		});
    		bnt8.setBounds(462, 11, 45, 40);
    		frame.getContentPane().add(bnt8);
    		
    		JButton btn9 = new JButton("9");
    		btn9.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    			}
    		});
    		btn9.setBounds(507, 11, 45, 40);
    		frame.getContentPane().add(btn9);
    		
    		JButton btn0 = new JButton("0");
    		btn0.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    			}
    		});
    		btn0.setBounds(552, 11, 45, 40);
    		frame.getContentPane().add(btn0);
    		
    		JButton btnBackspace = new JButton("Backspace");
    		btnBackspace.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    			}
    		});
    		btnBackspace.setBounds(597, 11, 153, 40);
    		frame.getContentPane().add(btnBackspace);
    		
    		JButton btnEnter = new JButton("ENTER");
    		btnEnter.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    			}
    		});
    		btnEnter.setBounds(664, 55, 86, 91);
    		frame.getContentPane().add(btnEnter);
    		
    		JButton btnQ = new JButton("Q");
    		btnQ.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    			}
    			
    		});
    		btnQ.setBounds(108, 62, 45, 40);
    		frame.getContentPane().add(btnQ);
    		
    		JButton btnW = new JButton("W");
    		btnW.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    			}
    		});
    		btnW.setBounds(155, 62, 45, 40);
    		frame.getContentPane().add(btnW);
    		
    		JButton btnR = new JButton("R");
    		btnR.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    			}
    		});
    		btnR.setBounds(201, 62, 45, 40);
    		frame.getContentPane().add(btnR);
    		
    		JButton btnT = new JButton("T");
    		btnT.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    			}
    		});
    		btnT.setBounds(246, 62, 45, 40);
    		frame.getContentPane().add(btnT);
    		
    		JButton btnZ = new JButton("Z");
    		btnZ.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    			}
    		});
    		btnZ.setBounds(291, 62, 45, 40);
    		frame.getContentPane().add(btnZ);
    		
    		JButton btnU = new JButton("U");
    		btnU.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    			}
    		});
    		btnU.setBounds(337, 62, 45, 40);
    		frame.getContentPane().add(btnU);
    		
    		JButton btnI = new JButton("I");
    		btnI.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    			}
    		});
    		btnI.setBounds(382, 62, 45, 40);
    		frame.getContentPane().add(btnI);
    		
    		JButton btnO = new JButton("O");
    		btnO.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    			}
    		});
    		btnO.setBounds(427, 62, 45, 40);
    		frame.getContentPane().add(btnO);
    		
    		JButton btnP = new JButton("P");
    		btnP.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    			}
    		});
    		btnP.setBounds(472, 62, 45, 40);
    		frame.getContentPane().add(btnP);
    		
    		JButton button_1 = new JButton("\u0160");
    		button_1.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    			}
    		});
    		button_1.setBounds(517, 62, 45, 40);
    		frame.getContentPane().add(button_1);
    		
    		JButton button_2 = new JButton("\u0110");
    		button_2.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    			}
    		});
    		button_2.setBounds(562, 62, 45, 40);
    		frame.getContentPane().add(button_2);
    		
    		JButton btnA = new JButton("A");
    		btnA.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    			}
    		});
    		btnA.setBounds(118, 106, 45, 40);
    		frame.getContentPane().add(btnA);
    		
    		JButton btnS = new JButton("S");
    		btnS.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    			}
    		});
    		btnS.setBounds(165, 106, 45, 40);
    		frame.getContentPane().add(btnS);
    		
    		JButton btnD = new JButton("D");
    		btnD.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    			}
    		});
    		btnD.setBounds(211, 106, 45, 40);
    		frame.getContentPane().add(btnD);
    		
    		JButton btnF = new JButton("F");
    		btnF.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    			}
    		});
    		btnF.setBounds(256, 106, 45, 40);
    		frame.getContentPane().add(btnF);
    		
    		JButton btnG = new JButton("G");
    		btnG.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    			}
    		});
    		btnG.setBounds(301, 106, 45, 40);
    		frame.getContentPane().add(btnG);
    		
    		JButton btnH = new JButton("H");
    		btnH.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    			}
    		});
    		btnH.setBounds(347, 106, 45, 40);
    		frame.getContentPane().add(btnH);
    		
    		JButton btnJ = new JButton("J");
    		btnJ.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    			}
    		});
    		btnJ.setBounds(392, 106, 45, 40);
    		frame.getContentPane().add(btnJ);
    		
    		JButton btnK = new JButton("K");
    		btnK.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    			}
    		});
    		btnK.setBounds(437, 106, 45, 40);
    		frame.getContentPane().add(btnK);
    		
    		JButton btnL = new JButton("L");
    		btnL.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    			}
    		});
    		btnL.setBounds(482, 106, 45, 40);
    		frame.getContentPane().add(btnL);
    		
    		JButton button_3 = new JButton("\u010C");
    		button_3.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    			}
    		});
    		button_3.setBounds(527, 106, 45, 40);
    		frame.getContentPane().add(button_3);
    		
    		JButton button_4 = new JButton("\u0106");
    		button_4.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    			}
    		});
    		button_4.setBounds(572, 106, 45, 40);
    		frame.getContentPane().add(button_4);
    		
    		JButton button_5 = new JButton("\u017D");
    		button_5.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    			}
    		});
    		button_5.setBounds(618, 106, 45, 40);
    		frame.getContentPane().add(button_5);
    		
    		JButton btnY = new JButton("Y");
    		btnY.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    			}
    		});
    		btnY.setBounds(128, 154, 45, 40);
    		frame.getContentPane().add(btnY);
    		
    		JButton btnDel = new JButton("DEL");
    		btnDel.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    			}
    		});
    		btnDel.setBounds(607, 62, 56, 40);
    		frame.getContentPane().add(btnDel);
    		
    		JButton btnX = new JButton("X");
    		btnX.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				text = String.format(" %s", "x");
    				
    				Text_t.setText(text);
    			}
    		});
    		btnX.setBounds(175, 154, 45, 40);
    		frame.getContentPane().add(btnX);
    		
    		JButton btnC = new JButton("C");
    		btnC.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				text = String.format(" %s", "c");
    				
    				Text_t.setText(text);
    			}
    		});
    		btnC.setBounds(221, 154, 45, 40);
    		frame.getContentPane().add(btnC);
    		
    		JButton btnV = new JButton("V");
    		btnV.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				text = String.format(" %s", "v");
    				
    				Text_t.setText(text);
    			}
    		});
    		btnV.setBounds(266, 154, 45, 40);
    		frame.getContentPane().add(btnV);
    		
    		JButton btnB = new JButton("B");
    		btnB.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				text = String.format(" %s", "b");
    				
    				Text_t.setText(text);
    			}
    		});
    		btnB.setBounds(311, 154, 45, 40);
    		frame.getContentPane().add(btnB);
    		
    		JButton btnN = new JButton("N");
    		btnN.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				text = String.format(" %s", "n");
    				
    				Text_t.setText(text);
    			}
    		});
    		btnN.setBounds(357, 154, 45, 40);
    		frame.getContentPane().add(btnN);
    		
    		JButton btnM = new JButton("M");
    		btnM.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				
    			text = String.format(" %s", "m");
    			
    			Text_t.setText(text);
    			
    			}
    		});
    		btnM.setBounds(402, 154, 45, 40);
    		frame.getContentPane().add(btnM);
    		
    		
    		Text_t.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    			}
    		});
    		Text_t.setBounds(611, 208, 164, 20);
    		frame.getContentPane().add(Text_t);
    		
    	}
    		
    	
    
    		
    	}

  3. #3
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Problem with JTextField [Beginner]

    Please use Java naming conventions.

    Why are you using a JFormattedTextField and not just a JTextField?

    Your problem is because setText() overwrites what is currently in the text field so you need to call getText(), append the new character(s) to the returned value and then call setText() with the whole string to display.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  4. #4
    Join Date
    Nov 2012
    Posts
    3

    Re: Problem with JTextField [Beginner]

    thank u man, problem solved ...i appreciate your help

  5. #5
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Problem with JTextField [Beginner]

    BTW hard coding the bounds of the buttons is the wrong way to go, it means your keyboard can't be resized. Use a layout manager to layout the buttons instead. You'll probably need something like GridBagLayout to do a keyboard layout, if you don't know how to use it there are plenty of tutorials on-line.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Problem with JTextField [Beginner]

    Another thing. Do you really want to create a separate ActionListener for every button, you may be better creating one ActionListener for all the buttons that type a character and add that to all those buttons.

    Each button could have a unique action command value (maybe it's standard character or name) and the action listener could get the event source's action command and map it to the actual character to display. The beauty of doing this (ie adding a level of indirection) is:
    1. You centralise your code for a common action.
    2. You can easily handle modifier keys such as shift by providing different mapping for each combination of modifier keys.
    3. When modifier key(s) are pressed you can use the appropriate mapping to display the new characters on the keys.
    4. You can provide support multiple languages by providing different mappings for different language keyboard layouts ie via ResourceBundles.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center