PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : Entertaste gleiche Funktion wie Button zuweisen



ESU4RK
30.11.2010, 18:01
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?:


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



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_O N_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;
}

}

EBFE
30.11.2010, 19:51
Stichwort: Default Button ;)
How to Use Buttons, Check Boxes, and Radio Buttons (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components) (http://download.oracle.com/javase/tutorial/uiswing/components/button.html)


How to Use JButton Features

Ordinary buttons — JButton objects — have just a bit more functionality than the AbstractButton class provides: You can make a JButton be the default button. At most one button in a top-level container can be the default button. The default button typically has a highlighted appearance and acts clicked whenever the top-level container has the keyboard focus and the user presses the Return or Enter key. Here is a picture of a dialog, implemented in the ListDialog (http://download.oracle.com/javase/tutorial/uiswing/examples/components/index.html#ListDialog) example, in which the Set button is the default button:

ESU4RK
01.12.2010, 08:42
Und in welche Methode muss ich das einfügen?
also

getRootPane().setDefaultButton(setButton);

c4pone
01.12.2010, 10:29
Default button for dialog: press Enter to activate : Dialog « Swing JFC « Java (http://www.java2s.com/Code/Java/Swing-JFC/DefaultbuttonfordialogpressEntertoactivate.htm)

ESU4RK
04.12.2010, 08:27
Also der Searchbutton ist jetzt der defaultutton. Blau markiert ist er auch, was ja heisst das er der aktive button der beim Enterdrücken betätigt wird. Klappt aber immer noch nicht, wenn ich noch im textfeld bin.



jButton1 = new JButton();
getContentPane().add(jButton1);
getContentPane().add(getJRadioButton1());
getContentPane().add(getJRadioButton2());
getContentPane().add(getJLabel1());
jTextField1.getRootPane().setDefaultButton(jButton 1);
jButton1.setText("Search");
jButton1.setBounds(76, 55, 105, 21);
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButton1ActionPerformed(evt);

}
});
da hab ich es eingefügt, oder fehlt dafür noch etwas?

haggys22
04.12.2010, 08:53
Ich hätte das ganze so gelöst:


jButton1.setBounds(82, 94, 105, 23);
jTextField1.setText("Search");
Action action_ENTER = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
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();
}
}
}
};
jTextField1.getInputMap().put(KeyStroke.getKeyStro ke("ENTER"),"ENTER");
jTextField1.getActionMap().put("ENTER", action_ENTER);
getContentPane().add(jTextField1);

ESU4RK
05.12.2010, 19:29
Ich hätte das ganze so gelöst:


jButton1.setBounds(82, 94, 105, 23);
jTextField1.setText("Search");
Action action_ENTER = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
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();
}
}
}
};
jTextField1.getInputMap().put(KeyStroke.getKeyStro ke("ENTER"),"ENTER");
jTextField1.getActionMap().put("ENTER", action_ENTER);
getContentPane().add(jTextField1);



Sehr gut danke funktioniert.