Okay I need help with this , My Brain thinks it's right but the keyevent compiles but doesn't do anything, I have it set , so that that code get's users keycode, Then If the item in the list box is selected and they press d, the item is removed. I can't figure out why it doesn' work.
public void keyTyped(KeyEvent event) {
int code = event.getKeyCode();
if (code == KeyEvent.VK_D) {
String [] items = lstbox.getSelectedItems();
for (int i = 0; i < items.length; i ++) {
lstbox.remove(i);
}
}
}
12-06-2004, 11:30 PM
rogueace
shouldn't it be event.VK_D instead of KeyEvent.VK_D?
maybe try that.. also, make sure you have focus. If neither of those work, maybe there is something wrong with other parts of your code, in which case you'd have to post it all.
Hope that helps.
12-07-2004, 06:25 AM
mikeBarr81
Which component did you add the KeyEvent to?
12-07-2004, 06:27 AM
mikeBarr81
And VK_D is a static field of KeyEvent so don't change it.
12-07-2004, 11:48 AM
RPBLEA
In the book I have, It tells me to add the keyevent if I want to get that key to select the D key. Here's the entire code, I'm creating a movie database, and in The list when the user types in d it represents a delete and removes everything from the list? I can't figure it out? I've changed it since my last post but still nothing works, Any help appreciated, thanks,
// here is the section of the code, the rest of the program is there with this in it, take a look see if you see anything wrong?
// Key Events to delete
// Cannot get this part of the code to work
public void keyPressed(KeyEvent event) {}
public void keyReleased(KeyEvent event) {}
public void keyTyped(KeyEvent event) {
// int code = event.getKeyCode();
/// if (code == KeyEvent.VK_D) {
// lstbox.delItem();
// String [] items = lstbox.getSelectedItems();
// for (int i = 0; i < items.length; i ++) {
// lstbox.remove(i);
// }
// }
}
public class movie extends Canvas implements ActionListener,KeyListener {
public static void main (String [] args) {
movie movie = new movie();
}
private CheckboxGroup chkgroup = new CheckboxGroup();
private Label lblname = new Label("Name");
private Label lblyear = new Label("Year");
private Label lblrunning = new Label("Running Time");
private TextField txtname = new TextField(15);
private TextField txtyear = new TextField(4);
private TextField txtrunning = new TextField(15);
private Label lblrate = new Label("Rate the movie");
private Checkbox chkgood = new Checkbox("Good", false, chkgroup);
private Checkbox chkokay = new Checkbox("Okay",false, chkgroup);
private Checkbox chkbad = new Checkbox("Bad", false, chkgroup);
private Button btnenter = new Button("Enter");
//private TextArea txtarea = new TextArea(25,65);
private List lstbox = new List(10);
public movie () {
Frame theframe = new Frame();
theframe.setSize(400, 350);
theframe.add(this);
theframe.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
theframe.setTitle("DvD Collection");
MenuBar bar = new MenuBar();
theframe.setMenuBar(bar);
Menu FileMenu = new Menu("File");
bar.add(FileMenu);
MenuItem Save = new MenuItem("Save", new MenuShortcut(KeyEvent.VK_S));
MenuItem Exit = new MenuItem("Exit", new MenuShortcut(KeyEvent.VK_E));
MenuItem New = new MenuItem("New", new MenuShortcut(KeyEvent.VK_N));
// for (int i = 0; i < items.length; i ++) {
// lstbox.remove(i);
// }
Did you say that when you press D, then the whole list erases? Or is that what it's supposed to do?
12-07-2004, 06:55 PM
RPBLEA
That's what I want it to do, I want it, so that when i select something on the list that when I press D it deletes It, Right now everything i've tried has not worked, it doesn't work And I want it to Delete items on the list
12-10-2004, 04:39 AM
sjalle
The keyTyped callback is not invoked for all kinds of keys....
This code works:
public class Movie extends Canvas implements ActionListener,KeyListener {
private CheckboxGroup chkgroup = new CheckboxGroup();
private Label lblname = new Label("Name");
private Label lblyear = new Label("Year");
private Label lblrunning = new Label("Running Time");
private TextField txtname = new TextField(15);
private TextField txtyear = new TextField(4);
private TextField txtrunning = new TextField(15);
private Label lblrate = new Label("Rate the movie");
private Checkbox chkgood = new Checkbox("Good", false, chkgroup);
private Checkbox chkokay = new Checkbox("Okay",false, chkgroup);
private Checkbox chkbad = new Checkbox("Bad", false, chkgroup);
private Button btnenter = new Button("Enter");
//private TextArea txtarea = new TextArea(25,65);
private List lstbox = new List(10);
public void Movie () {
String strname = txtname.getText();
String stryear = txtyear.getText();
String strunning = txtrunning.getText();
String [] titles = {strname, stryear, strunning};
for (int i = 0; i < titles.length; i++) {
lstbox.add(titles[i]);
System.out.println(titles[i]);
}
}
public static void main (String [] args) {
Movie movie = new Movie();
}
public Movie () {
Frame theframe = new Frame();
theframe.setSize(400, 350);
theframe.add(this);
theframe.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
theframe.setTitle("DvD Collection");
MenuBar bar = new MenuBar();
theframe.setMenuBar(bar);
Menu FileMenu = new Menu("File");
bar.add(FileMenu);
MenuItem Save = new MenuItem("Save", new MenuShortcut(KeyEvent.VK_S));
MenuItem Exit = new MenuItem("Exit", new MenuShortcut(KeyEvent.VK_E));
MenuItem New = new MenuItem("New", new MenuShortcut(KeyEvent.VK_N));
// Key Events to delete
// Cannot get this part of the code to work
public void keyPressed(KeyEvent event) {}
public void keyReleased(KeyEvent event) {
int code = event.getKeyCode();
if (code == KeyEvent.VK_DELETE) {
int ix=lstbox.getSelectedIndex();
if (ix < 0) return;
lstbox.remove(ix);
}
}
public void keyTyped(KeyEvent event) {}
Are professor doesn't want us to work on anything in regards to applets, He wants us to work in Frames, Canvases. I fogot the reason he told us, but He said it's because it enables us to do more with java? I can't remember his explanation sorry, he said they were better t hough?
12-10-2004, 08:29 PM
sjalle
Well thats dust dung.
Frames are the primary java (visuel) objects and the rest is just containment. Checek out container.....