我正在开发一个自定义搜索组件。单击文本字段内的按钮(搜索图像图标)时,将显示一个表格。代码处于非常初级的阶段,所以很多东西都不存在。
到目前为止,我被卡住了,因为我无法使用箭头键导航表格。一旦我在表格中选择了一行,我也无法使用“tab”键移动到下一个文本字段。
该组件的目标是从表中选择一行,然后在支持方法(尚未写入)的帮助下,来自其他标签和文本字段的值将被默认,而在文本字段中默认第一列值是附于。
非常感谢您的帮助。谢谢 !!
package myTableCombo;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JWindow;
import javax.swing.ListSelectionModel;
import javax.swing.RowFilter;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.plaf.basic.BasicArrowButton;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
public class SearchBox extends JPanel {
private static final long serialVersionUID = 1L;
private JTextField editor;
private JButton arrowButton;
private JWindow jWindow;
private Component userComponent;
public SearchBox() {
super(new FlowLayout(FlowLayout.LEFT, 0, 0));
editor = new JTextField();
editor.setPreferredSize(new Dimension(250, 25));
initialize();
addListeners();
setBorders();
}
private void addListeners() {
arrowButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
showWindow();
}
});
}
protected void showWindow() {
if (null != getWindowComponent()) {
jWindow = new JWindow();
jWindow.getContentPane().add(getWindowComponent());
jWindow.setLocation(new Point(SearchBox.this.getLocationOnScreen().x, SearchBox.this.getLocationOnScreen().y + 25));
jWindow.pack();
jWindow.setVisible(true);
}
}
private Component getWindowComponent() {
return userComponent;
}
public void setWindowComponent(Component component) {
userComponent = component;
}
private void initialize() {
arrowButton = new BasicArrowButton(SwingConstants.SOUTH);//In my code there is an icon here
arrowButton.setBorder(null);
arrowButton.setContentAreaFilled(false);
}
private void setBorders() {
add(editor);
add(arrowButton);
setBackground(editor.getBackground());
setBorder(editor.getBorder());
editor.setBorder(null);
}
public JTextField getTextComponent() {
return editor;
}
public JButton getActionButton() {
return arrowButton;
}
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
createAndShowGUI();
}
});
}
protected static void createAndShowGUI() {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
frame.setPreferredSize(new Dimension(500, 400));
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
panel.add(new JLabel("Test Table Combo"));
SearchBox searchBox = new SearchBox();
searchBox.userComponent = searchBox.new SearchBoxTable();
panel.add(searchBox);
frame.getContentPane().add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private class SearchBoxTable extends JScrollPane implements DocumentListener {
private static final long serialVersionUID = 1L;
private JTable table;
private Object[] columnNames;
private DefaultTableModel tableModel;
private Object[][] sheetDataInString;
private TableRowSorter<DefaultTableModel> rowSorter;
public SearchBoxTable() {
columnNames = new Object[]{"Column 1", "Column 2", "Column 3", "Column 4", "Column 5", "Column 6"};
sheetDataInString = new Object[21][6];
for(int i =0; i < sheetDataInString.length; i++) {
for(int j = 0; j< 6; j++) {
sheetDataInString[i][j] = "Row Value : " + i + ", Column Value : " + j;
}
}
tableModel = new DefaultTableModel(sheetDataInString, columnNames) {
private static final long serialVersionUID = 1L;
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
rowSorter = new TableRowSorter<DefaultTableModel>(tableModel);
table = new JTable(tableModel);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setAutoCreateRowSorter(true);
table.setFillsViewportHeight(true);
table.getTableHeader().setFont(new Font(null, Font.PLAIN, 13));
setPreferredSize(new Dimension(500, 225));
setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
getViewport().add(table);
}
@Override
public void changedUpdate(DocumentEvent e) {
newFilter(e);
}
@Override
public void insertUpdate(DocumentEvent e) {
newFilter(e);
}
@Override
public void removeUpdate(DocumentEvent e) {
newFilter(e);
}
/**
* Update the row filter regular expression from the expression/string/data in the text box.
*
* @param documentEvent
*/
private void newFilter(DocumentEvent documentEvent) {
RowFilter<DefaultTableModel, Object> rowFilter = null;
Document document = documentEvent.getDocument();
String filterText;
try {
filterText = document.getText(0, document.getLength()).trim();
rowFilter = RowFilter.regexFilter("(?i)" + filterText);
} catch (java.util.regex.PatternSyntaxException e) {
return;
} catch (BadLocationException badLocationException) {
return;
}
rowSorter.setRowFilter(rowFilter);
table.setRowSorter(rowSorter);
}
}
}