Results 1 to 7 of 7

Thread: key event help

  1. #1
    Kyle227's Avatar
    Kyle227 is offline Member
    Join Date
    Apr 2010
    Location
    Mass
    Posts
    47
    Rep Power
    0

    Angry key event help

    i am making a hex and decimal converter and I'm trying to use Key Event also.

    it shows no errors but the key event is not working.

    here is are my classes
    Java Code:
    import javax.swing.JFrame;
    import java.awt.*;
    import javax.swing.*;
    
    public class Hex2Dec extends JFrame {
    	//pics
    	ImageIcon convertpic = new ImageIcon("src/defaultpackage/convert.gif");
    	
    	//buttons
    	JButton reset;
    	JButton converth;
    	JButton convertd;
    	// text fields
    	JTextField hex;
    	JTextField dec;
    
    	public Hex2Dec() {
    		super("Converting Hex and Dec");
    		setSize(350,175);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		GridLayout grid = new GridLayout(3,3);
    		setLayout(grid);
    		setVisible(true);
    		setFocusable(true);
    		
    		//row 1
    		JPanel row1 = new JPanel();
    		JLabel hexl = new JLabel("Hex");
    		row1.add(hexl);
    		hex = new JTextField("Enter Hex", 15);
    		hex.setFocusable(true);
    		row1.add(hex);
    		converth = new JButton(convertpic);
    		converth.setPreferredSize(new Dimension(35,35));
    		row1.add(converth);
    		add(row1);
    		
    		//row2
    		JPanel row2 = new JPanel();
    		JLabel decl = new JLabel("Decimal");
    		row2.add(decl);
    		dec = new JTextField("Enter Decimal",15);
    		dec.setFocusable(true);
    		row2.add(dec);
    		convertd = new JButton(convertpic);
    		convertd.setPreferredSize(new Dimension(35,35));
    		row2.add(convertd);
    		add(row2);
    		
    		//row3
    		JPanel row3 = new JPanel();
    		reset = new JButton("Reset");
    		row3.add(reset);
    		add(row3);
    		
    		Command cm = new Command(this);
    		Keycm km = new Keycm(this);
    		reset.addActionListener(cm);
    		converth.addActionListener(cm);
    		converth.addKeyListener(km);
    		converth.addMouseListener(cm);
    		convertd.addActionListener(cm);
    		convertd.addMouseListener(cm);
    		convertd.addKeyListener(km);
    		hex.addActionListener(cm);
    		dec.addActionListener(cm);
    		
    		
    		
    		
    		
    	}public static void main(String[] args) {
    		Hex2Dec h2d = new Hex2Dec();
    	}
    
    }
    second class
    Java Code:
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    
    public class Keycm implements KeyListener {
    	Hex2Dec hx;
    	
    	public Keycm(Hex2Dec hx) {
    		this.hx = hx;
    	}
    	public void keyTyped(KeyEvent k) {
    		if(k.getSource()==hx.hex) {
    		String ghex = hx.hex.getText();
    		int chex = Integer.parseInt(ghex, 16);
    		hx.dec.setText(Integer.toString(chex));
    		}else if(k.getSource()==hx.dec) {
    			String gdec = hx.dec.getText();
    			int idec = Integer.parseInt(gdec);
    			String cgex = Integer.toHexString(idec);
    			cgex.toUpperCase();
    			hx.hex.setText(cgex);
    		}
    
    }
    	 public void keyPressed(KeyEvent e)
    	    {
    	    }
    
    
    	    public void keyReleased (KeyEvent e)
    	    {
    	    }
    
    
    	}
    please help:confused:

  2. #2
    Cruncher is offline Member
    Join Date
    Mar 2010
    Posts
    88
    Rep Power
    0

    Default

    Are these classes in separate files?

  3. #3
    Kyle227's Avatar
    Kyle227 is offline Member
    Join Date
    Apr 2010
    Location
    Mass
    Posts
    47
    Rep Power
    0

    Default

    yes there is one more class i didn't show
    Java Code:
    package defaultpackage;
    
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    
    
    public class Command extends KeyAdapter implements ActionListener, KeyListener, MouseListener {
    	Hex2Dec hx;
    	
    	public Command(Hex2Dec hx) {
    		this.hx = hx;
    	}
    	
    	
    	public void actionPerformed(ActionEvent e) {
    		if(e.getSource()==hx.converth) {
    			String ghex = hx.hex.getText();
    			int chex = Integer.parseInt(ghex, 16);
    			hx.dec.setText(Integer.toString(chex));
    			
    			
    		}else if(e.getSource()==hx.convertd) {
    			String gdec = hx.dec.getText();
    			int idec = Integer.parseInt(gdec);
    			String cgex = Integer.toHexString(idec);
    			cgex.toUpperCase();
    			hx.hex.setText(cgex);
    			}else if(e.getSource()==hx.reset) {
    				hx.hex.setText(null);
    				hx.dec.setText(null);
    				
    			}
    			
    		
    	
    	}public void mousePressed(MouseEvent e)
        {
            if (e.getSource() == hx.hex)
                hx.hex.setText ("");
        }
    	public void mouseReleased (MouseEvent e)
        {
        }
    
    
        public void mouseClicked (MouseEvent e)
        {
        }
    
    
        public void mouseEntered (MouseEvent e)
        {
        }
    
    
        public void mouseExited (MouseEvent e)
        {
        }
    
    
    	
    
    			
    }

  4. #4
    Cruncher is offline Member
    Join Date
    Mar 2010
    Posts
    88
    Rep Power
    0

    Default

    Okay, so what happens?

    you run it, the GUI loads fine? and when you type things it doesn't work as expected?

  5. #5
    Cruncher is offline Member
    Join Date
    Mar 2010
    Posts
    88
    Rep Power
    0

    Default

    Okay, you are adding keylisteners to converth and convertd

    so your key stuff will only trigger when they have focus, and since you are checking source to be the text fields, your key listener code will never be useful.

    Try adding the KeyListener to dec and hex instead.

  6. #6
    Kyle227's Avatar
    Kyle227 is offline Member
    Join Date
    Apr 2010
    Location
    Mass
    Posts
    47
    Rep Power
    0

    Default

    wow i am so stupid thanks:)

  7. #7
    Cruncher is offline Member
    Join Date
    Mar 2010
    Posts
    88
    Rep Power
    0

Similar Threads

  1. Trigger an Event
    By koxy in forum Advanced Java
    Replies: 1
    Last Post: 03-24-2010, 07:45 AM
  2. checking for an event during an event
    By infinity in forum AWT / Swing
    Replies: 22
    Last Post: 04-09-2009, 01:08 AM
  3. Event handling... help?
    By kevzspeare in forum New To Java
    Replies: 2
    Last Post: 04-04-2009, 08:46 PM
  4. Key event
    By ivvgangadhar in forum Threads and Synchronization
    Replies: 1
    Last Post: 12-11-2008, 09:27 AM
  5. Event
    By nt5515 in forum New To Java
    Replies: 0
    Last Post: 02-15-2008, 04:44 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
  •