-
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
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
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:
-
Are these classes in separate files?
-
yes there is one more class i didn't show
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)
{
}
}
-
Okay, so what happens?
you run it, the GUI loads fine? and when you type things it doesn't work as expected?
-
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.
-
wow i am so stupid thanks:)
-
no problem bro ;p
and you're not stupid :)