Hi alle,

Ich habe ein kleines Problem in meinem Programm. Also es ist ein Programm in dem man ein Textfeld hat und dann mit zwei Radiobuttons die Auswahl Google oder Bing treffen kann. Klickt man den Searchbutton öffnet sich der Standardbrowser und die Google/Bing Suche mit dem eingegebenen Wort wird aufgerufen. Nun möchte ich aber auch, dass man während man noch im Textfeld ist die Entertaste der Tastatur drücken kann und es passiert das gleiche als würde man Search drücken.

Meine Idee war hier drin die Methode für meinen Button aufzurufen (statt der Ausgabe ), nur leider funktioniert das nicht. Ist das überhaupt die richtige Methode?:

Code:
    public void keyTyped(KeyEvent event)
     {
      if (event.getKeyChar() == KeyEvent.VK_ENTER)
              {                              
           System.out.println("test");
        }
     }
hier nochmal der komplette Quellcode:

Code:
package GoogleSearch;
import java.awt.Desktop;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import javax.swing.ButtonGroup;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.KeyStroke;

import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;



public class NewJFrame extends javax.swing.JFrame {

    {
        //Set Look & Feel
        try {
            javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    private JTextField jTextField1;
    private JButton jButton1;
    private JLabel jLabel1;
    private JRadioButton jRadioButton2;
    private JRadioButton jRadioButton1;
    private ButtonGroup buttonGroup1;

    
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                NewJFrame inst = new NewJFrame();
                inst.setLocationRelativeTo(null);
                inst.setVisible(true);
            }
        });
    }
    
    public NewJFrame() {
        super();
        initGUI();
    }
    
    private void initGUI() {
        try {
            setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            this.setTitle("MK Google Search");
            getContentPane().setLayout(null);
            this.setAlwaysOnTop(true);
            {
                jTextField1 = new JTextField();
                getContentPane().add(jTextField1);
                jTextField1.setBounds(20, 60, 247, 23);
                
                
            }
            
            {
                jButton1 = new JButton();
                getContentPane().add(jButton1);
                getContentPane().add(getJRadioButton1());
                getContentPane().add(getJRadioButton2());
                getContentPane().add(getJLabel1());
                jButton1.setText("Search");
                jButton1.setBounds(82, 94, 105, 23);
                jButton1.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent evt) {
                        jButton1ActionPerformed(evt);
                    }
                });
            }
            pack();
            this.setSize(293, 173);
        } catch (Exception e) {
            //add your error handling code here
            e.printStackTrace();
        }
    }
    
    public void keyPressed(KeyEvent arg0) {
        if (arg0.getKeyCode() != KeyEvent.VK_ENTER)
        {
            System.out.println("hi");
        }
        }
    
    private void jButton1ActionPerformed(ActionEvent evt) {
        
        boolean b = jRadioButton1.isSelected();
        boolean c = jRadioButton2.isSelected();
        String search = jTextField1.getText();
        if (b!=false){
            
        
        String kompsearch = "http://www.google.de/#hl=de&q=" + search+"&aq=f&aqi=g10&aql=&oq=&gs_rfai=&fp=95d8f914f306e11a";
        try {
            Desktop.getDesktop().browse(new URI(kompsearch.replace(' ', '+')));
    } catch (IOException e) {
            e.printStackTrace();
    } catch (URISyntaxException e) {
            e.printStackTrace();
    }
        }
    
    else if (c!=false)
    {
        String kompsearch = "http://www.bing.com/search?q="+search+"&go=&form=QBLH&filt=all";
    try {
        Desktop.getDesktop().browse(new URI(kompsearch.replace(' ', '+')));
} catch (IOException e) {
        e.printStackTrace();
} catch (URISyntaxException e) {
        e.printStackTrace();
}
    }
}
        
    
    private ButtonGroup getButtonGroup1() {
        if(buttonGroup1 == null) {
            buttonGroup1 = new ButtonGroup();
        }
        return buttonGroup1;
    }
    
    private JRadioButton getJRadioButton1() {
        if(jRadioButton1 == null) {
            jRadioButton1 = new JRadioButton();
            jRadioButton1.setText("Google");
            jRadioButton1.setBounds(105, 2, 93, 23);
            getButtonGroup1().add(jRadioButton1);

        }
        return jRadioButton1;
    }
    
    private JRadioButton getJRadioButton2() {
        if(jRadioButton2 == null) {
            jRadioButton2 = new JRadioButton();
            jRadioButton2.setText("Bing");
            jRadioButton2.setBounds(105, 23, 45, 23);
            getButtonGroup1().add(jRadioButton2);
        }
        return jRadioButton2;
    }
    

    
    private JLabel getJLabel1() {
        if(jLabel1 == null) {
            jLabel1 = new JLabel();
            jLabel1.setText("I want to use:");
            jLabel1.setBounds(30, 6, 76, 14);
        }
        return jLabel1;
    }

}