AutoComplete修饰器

我在Swing应用程序中实现了autocompleteDecorator。我的代码是这样的。AutoComplete修饰器

public inventory_purchase() { 

initComponents();

AutoCompleteDecorator.decorate(this.combo);

}

public void autocomplete(){

try{

Class.forName("org.apache.derby.jdbc.ClientDriver");

Connection conn= DriverManager.getConnection("jdbc:derby://localhost:1527/C:/jpublisher/pub", "APP", "app");

Statement stmt = conn.createStatement();

String query="SELECT * FROM INVENTORY";

ResultSet rs = stmt.executeQuery(query);

while(rs.next()){

combo.addItem(rs.getString("CATEGORY"));

}

}

catch (ClassNotFoundException | SQLException ex) {

JOptionPane.showMessageDialog(null, "Data cannot be loaded. Error!!");

}

}

这个自动完成装饰工程,只有当我称之为

formWindowOpened(java.awt.event.WindowEvent evt){autocomplete();} 

此功能如何使用这个自动完成一键收听?像:

private void comboKeyReleased(java.awt.event.KeyEvent evt) { 

autocomplete();

}

是否有任何其他简单的过程来使用从数据库的自动完成?

回答:

您可能想要使用Key Bindings。这里有一个简单的例子:

import java.awt.event.*; 

import javax.swing.*;

public class KeyBindings extends Box{

public KeyBindings(){

super(BoxLayout.Y_AXIS);

final JLabel text = new JLabel("Original Text");

add(text);

Action action = new AbstractAction() {

@Override

public void actionPerformed(ActionEvent e) {

text.setText("New Text");

}};

String keyStrokeAndKey = "control H";

KeyStroke keyStroke = KeyStroke.getKeyStroke(keyStrokeAndKey);

getInputMap().put(keyStroke, keyStrokeAndKey);

getActionMap().put(keyStrokeAndKey, action);

}

public static void main(String[] args) {

JFrame frame = new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setContentPane(new KeyBindings());

frame.pack();

frame.setVisible(true);

}

}

以上是 AutoComplete修饰器 的全部内容, 来源链接: utcz.com/qa/258309.html

回到顶部