Help on KeyEvents


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 11 of 11

Thread: Help on KeyEvents

  1. #1
    Join Date
    Sep 2004
    Posts
    103

    Question Help on KeyEvents

    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);
    }
    }
    }

  2. #2
    Join Date
    Nov 2004
    Posts
    13
    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.

  3. #3
    Join Date
    Feb 2004
    Posts
    541
    Which component did you add the KeyEvent to?

  4. #4
    Join Date
    Feb 2004
    Posts
    541
    And VK_D is a static field of KeyEvent so don't change it.

  5. #5
    Join Date
    Sep 2004
    Posts
    103

    Question

    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);
    // }
    // }
    }




    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    import java.io.*;
    import java.awt.Color;
    import java.util.Arrays;


    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));

    FileMenu.add(New);
    FileMenu.add(Save);
    FileMenu.add(Exit);


    // sets labels name, year,running
    c.gridx = 0;
    c.gridy = 0;
    theframe.add(lblname, c);
    c.gridx = 1;
    c.gridy = 0;
    theframe.add(lblyear,c);
    c.gridx = 2;
    c.gridy = 0;
    theframe.add(lblrunning,c);
    c.gridx = 1;
    c.gridy = 2;
    theframe.add(lblrate,c);


    // sets textfields of name, year, running
    c.gridx = 0;
    c.gridy = 1;
    theframe.add(txtname,c);
    c.gridx = 1;
    c.gridy = 1;
    theframe.add(txtyear,c);
    c.gridx = 2;
    c.gridy = 1;
    theframe.add(txtrunning,c);

    // sets checkboxes good, okay, bad
    c.gridx = 0;
    c.gridy = 3;
    c.anchor = GridBagConstraints.WEST;
    theframe.add(chkgood,c);
    c.gridx = 1;
    c.gridy = 3;
    c.anchor = GridBagConstraints.CENTER;
    theframe.add(chkokay,c);
    c.gridx = 2;
    c.gridy = 3;
    c.anchor = GridBagConstraints.EAST;
    theframe.add(chkbad,c);

    // sets button enter
    c.gridx = 1;
    c.gridy = 4;
    c.weighty = .0001;
    c.anchor = GridBagConstraints.CENTER;
    theframe.add(btnenter,c);

    // sets text area
    c.gridx = 0;
    c.gridy = 5;
    c.gridwidth = 3;
    lstbox.setSize(100,70);
    theframe.add(lstbox,c);


    // adds actionlistner to menu bar items
    New.addActionListener(this);
    Save.addActionListener(this);
    Exit.addActionListener(this);
    btnenter.addActionListener(this);
    // lstbox.addActionListener(this);
    lstbox.addKeyListener(this);
    theframe.setVisible(true);
    }
    // opens new window
    public void go() {
    Frame window = new Frame();
    FileDialog fd = new FileDialog(window);
    fd.show();
    String fil = fd.getFile();
    System.out.println("File is: " + fil);
    }
    // opens dialog to save file
    public void go2() {
    Frame window = new Frame();
    FileDialog df = new FileDialog(window, "Save", FileDialog.SAVE);
    df.setVisible(true);
    String fname = df.getFile();
    System.out.println("File is Saved: " + fname);
    }
    public void movies() {
    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 void movietitles () {
    String titles1 = txtname.getText();
    String titles2 = txtyear.getText();
    String titles3 = txtrunning.getText();
    String [] names = {titles1, titles2,titles3};
    Arrays.sort(names);

    }
    */

    // 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 void actionPerformed(ActionEvent event) {


    String who = event.getActionCommand();
    String stname = txtname.getText();
    String styear = txtyear.getText();
    String strunning = txtrunning.getText();

    Object source = event.getSource();
    if( btnenter == source){
    String check = "";

    if (chkgood.getState()) {
    check = "Movie Rating: Good";
    } else if(chkokay.getState()) {
    check = "Movie Rating: Okay";
    } else if(chkbad.getState()) {
    check = "Movie Rating: Bad";
    }

    lstbox.addItem("Movie Name: " + stname + " Year: " + styear +
    " Time it Runs: " + strunning + check);
    txtname.setText("");
    txtyear.setText("");
    txtrunning.setText("");
    }


    if(who.equals("Exit")){
    System.exit(0);
    }

    if(who.equals("New")){
    go();
    try {
    File file = new File("movie.txt");
    boolean success = file.createNewFile();
    if (success) {
    System.out.println("File Was Created as " + file);
    } else {
    System.out.println("File already Exists");
    }
    }catch (IOException e) {}
    }

    if(who.equals("Save")){
    go2();
    try {
    BufferedReader read = new BufferedReader(new FileReader("movie.txt"));
    PrintWriter fil = new PrintWriter(new FileWriter("movie.txt"));

    String line = read.readLine();

    System.out.println("Data Saved" + movies);

    fil.write(stname);
    fil.write(strunning);
    fil.write(styear);

    }catch (IOException ioe) { }
    }
    }
    }

  6. #6
    Join Date
    Nov 2004
    Posts
    13
    // 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?

  7. #7
    Join Date
    Sep 2004
    Posts
    103
    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

  8. #8
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    The keyTyped callback is not invoked for all kinds of keys....
    This code works:

    Code:
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    import java.io.*;
    import java.awt.Color;
    import java.util.Arrays;
    
    
    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));
    
        FileMenu.add(New);
        FileMenu.add(Save);
        FileMenu.add(Exit);
    
    
        // sets labels name, year,running
        c.gridx = 0;
        c.gridy = 0;
        theframe.add(lblname, c);
        c.gridx = 1;
        c.gridy = 0;
        theframe.add(lblyear,c);
        c.gridx = 2;
        c.gridy = 0;
        theframe.add(lblrunning,c);
        c.gridx = 1;
        c.gridy = 2;
        theframe.add(lblrate,c);
    
    
        // sets textfields of name, year, running
        c.gridx = 0;
        c.gridy = 1;
        theframe.add(txtname,c);
        c.gridx = 1;
        c.gridy = 1;
        theframe.add(txtyear,c);
        c.gridx = 2;
        c.gridy = 1;
        theframe.add(txtrunning,c);
    
        // sets checkboxes good, okay, bad
        c.gridx = 0;
        c.gridy = 3;
        c.anchor = GridBagConstraints.WEST;
        theframe.add(chkgood,c);
        c.gridx = 1;
        c.gridy = 3;
        c.anchor = GridBagConstraints.CENTER;
        theframe.add(chkokay,c);
        c.gridx = 2;
        c.gridy = 3;
        c.anchor = GridBagConstraints.EAST;
        theframe.add(chkbad,c);
    
        // sets button enter
        c.gridx = 1;
        c.gridy = 4;
        c.weighty = .0001;
        c.anchor = GridBagConstraints.CENTER;
        theframe.add(btnenter,c);
    
        // sets text area
        c.gridx = 0;
        c.gridy = 5;
        c.gridwidth = 3;
        lstbox.setSize(100,70);
        theframe.add(lstbox,c);
    
    
        // adds actionlistner to menu bar items
        New.addActionListener(this);
        Save.addActionListener(this);
        Exit.addActionListener(this);
        btnenter.addActionListener(this);
        // lstbox.addActionListener(this);
        lstbox.addKeyListener(this);
        theframe.setVisible(true);
      }
      // opens new window
      public void go() {
        Frame window = new Frame();
        FileDialog fd = new FileDialog(window);
        fd.show();
        String fil = fd.getFile();
        System.out.println("File is: " + fil);
      }
      // opens dialog to save file
      public void go2() {
        Frame window = new Frame();
        FileDialog df = new FileDialog(window, "Save", FileDialog.SAVE);
        df.setVisible(true);
        String fname = df.getFile();
        System.out.println("File is Saved: " + fname);
      }
    
      /*
    
      public void movietitles () {
      String titles1 = txtname.getText();
      String titles2 = txtyear.getText();
      String titles3 = txtrunning.getText();
      String [] names = {titles1, titles2,titles3};
      Arrays.sort(names);
    
      }
      */
    
      // 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) {}
    
      public void actionPerformed(ActionEvent event) {
        String who = event.getActionCommand();
        String stname = txtname.getText();
        String styear = txtyear.getText();
        String strunning = txtrunning.getText();
    
        Object source = event.getSource();
        if( btnenter == source){
        String check = "";
    
        if (chkgood.getState()) {
          check = "Movie Rating: Good";
        } else if(chkokay.getState()) {
          check = "Movie Rating: Okay";
        } else if(chkbad.getState()) {
          check = "Movie Rating: Bad";
        }
    
        lstbox.addItem("Movie Name: " + stname + " Year: " + styear +
                                  " Time it Runs: " + strunning + check);
        txtname.setText("");
        txtyear.setText("");
        txtrunning.setText("");
      }
    
    
      if(who.equals("Exit")){
        System.exit(0);
      }
    
      if(who.equals("New")){
        go();
        try {
          File file = new File("movie.txt");
          boolean success = file.createNewFile();
          if (success) {
            System.out.println("File Was Created as " + file);
          } else {
            System.out.println("File already Exists");
          }
      }catch (IOException e) {}
      }
    
      if(who.equals("Save")){
        go2();
        try {
          BufferedReader read = new BufferedReader(new FileReader("movie.txt"));
          PrintWriter fil = new PrintWriter(new FileWriter("movie.txt"));
    
          String line = read.readLine();
    
          System.out.println("Data Saved");// + movies);
    
          fil.write(stname);
          fil.write(strunning);
          fil.write(styear);
    
        }catch (IOException ioe) { }
        }
      }
    }
    eschew obfuscation

  9. #9
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    btw... whats with the Canvas ?
    eschew obfuscation

  10. #10
    Join Date
    Sep 2004
    Posts
    103
    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?

  11. #11
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Well thats dust dung.

    Frames are the primary java (visuel) objects and the rest is just containment. Checek out container.....
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center