Results 1 to 4 of 4
  1. #1
    kirzo is offline Member
    Join Date
    Jul 2009
    Posts
    2
    Rep Power
    0

    Default how to call other object's event

    hi, i'm new to java. i used to code with delphi n i have this java problem :

    private void button_mulaiActionPerformed(java.awt.event.ActionE vent evt) {
    // TODO add your handling code here:
    float nilai;
    nilai=Float.parseFloat(text_ujian.getText());
    if (nilai>60)
    {
    text_keterangan.setText(text_nama.getText()+", anda lulus!");
    }
    else
    {
    text_keterangan.setText(text_nama.getText()+", anda tidak lulus!");
    }

    }



    private void text_ujianKeyPressed(java.awt.event.KeyEvent evt) {
    // TODO add your handling code here:
    int tombol;
    tombol=evt.getKeyCode();
    if (tombol==KeyEvent.VK_ENTER)
    {

    }
    }

    let's say i have one textfield and one button
    i'd like to call the button's event if i pressed the ENTER key on the text field
    anyone can help me how to do it?
    thx b4 :)

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Add the same ActionListener to both the button and the textField.
    Something like this:
    Java Code:
    JButton button = ...
    JTextField textField = ...
    ActionListener al = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            ...
        }
    };
    button.addActionListener(al);
    textField.addActionListener(al);

  3. #3
    kirzo is offline Member
    Join Date
    Jul 2009
    Posts
    2
    Rep Power
    0

    Default thx for the reply

    thx for the reply but where shall i put those lines ?
    (sorry for the noob question :( )

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You need to go through the Sun Swing tutorial and read up on actionlisteners, buttons, and all of Swing. You'd be better off also not using NetBeans generated code but instead learn to hand-code your Swing apps. The tutorials will show you how to do everything: Using Swing Components

    Good luck

Similar Threads

  1. checking for an event during an event
    By infinity in forum AWT / Swing
    Replies: 22
    Last Post: 04-09-2009, 01:08 AM
  2. rmi and event handling
    By darkhorse in forum Advanced Java
    Replies: 0
    Last Post: 03-15-2009, 08:20 AM
  3. Key event
    By ivvgangadhar in forum Threads and Synchronization
    Replies: 1
    Last Post: 12-11-2008, 09:27 AM
  4. Replies: 0
    Last Post: 07-07-2008, 07:10 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
  •