Ok, I am having problems with a textfield in a popup JFrame... I actually had 2 separate programs, one was a game, and one was a map editor. I decided to make the game have a sort of built in map editor, so I added the classes from the map editor to a new package in my game's project, made a few small adjustments(like removing the main() method from the map editor), and then got things working. The map editor pops up as a new JFrame, and when I click the "new" button it opens a new JFrame with a couple TextFields and a button requesting a width and a height for the new map. I cannot edit the values in the textfields... and I have no clue why... Popup code:
private class newMap extends JFrame implements ActionListener{
JLabel wlbl = new JLabel("Map width: ");
JTextField w = new JTextField("12");
JLabel hlbl = new JLabel("Map height: ");
JTextField h = new JTextField("8");
JButton create = new JButton("Create map");
public newMap(Component p){
super("New Map");
setSize(100,75);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setResizable(false);
setLayout(null);
setVisible(true);
int bw = 96, bh = 24, s = 4, x = s;
wlbl.setBounds(x,s,bw,bh);
x+=s+bw;
add(wlbl);
w.setBounds(x,s,bw,bh);
x+=s+bw;
add(w);
hlbl.setBounds(x,s,bw,bh);
x+=s+bw;
add(hlbl);
h.setBounds(x,s,bw,bh);
x+=s+bw;
add(h);
create.setBounds(x,s,bw,bh);
x+=s+bw;
create.addActionListener(this);
add(create);
setSize(getWidth()-this.getContentPane().getWidth()+x,
getHeight()-this.getContentPane().getHeight()+s+s+bh);
setLocationRelativeTo(p);
}
public void actionPerformed(ActionEvent e){
try{
mapBox.create(Integer.parseInt(w.getText()),Integer.parseInt(h.getText()));
}catch(NumberFormatException ex){
return;
}
dispose();
}
}
I have obviously tried things like w.requestFocusInWidnow(), and w.requestFocus(), and the same for the frame in general and a few other solutions I found online, but none of them have worked for me.
Problem solved: After explaining my problem I realized that the problem HAD to be in the code for the game somewhere, probably in the class for the JFrame, and I noticed that I did something weird instead of using a KeyListener implementation to handle input.(I previously was attempting to create the game as a JApplet, and I was having trouble with Keylisteners on the JApplet). I made my Main class a KeyEventDispatcher, or some such thing. Thanks for the help guys, it was a dumb problem(I was just looking in the wrong places for a solution).
So the KeyEventDispatcher wasn't properly "dispatching" the key actions to other components. I just went back to a conventional KeyListener, since it works fine for me in a JFrame.