箭头键在自定义搜索组件中不起作用 [英] Arrow Keys Not Working in Custom Search Component
问题描述
我正在开发自定义搜索组件.单击文本字段内的按钮(搜索图像图标)时,将显示一个表格.代码处于非常初期的阶段,因此没有很多东西.
I am developing a custom search component. When clicked on the button(search image icon) inside the text field, a table is displayed. The code is in very nascent stage so lots of things is not there.
到目前为止,由于无法使用箭头键浏览表格,我陷入了困境.在表格中选择一行后,我也无法使用"Tab"键移动到下一个文本字段.
As of now, I am stuck because I am not able to navigate the table with the arrow keys. Neither am I able to move to the next text field with "tab" key, once I select a row in the table.
该组件的目标是从表中选择一行,然后借助支持方法(尚未编写),将其他标签和文本字段的值默认为默认值,同时将文本中的第一列值默认为默认值附加的字段.
The objective of the component is to select a row from the table and then with the help of the supporting methods(yet to be written) values from other labels and text fields would be defaulted while defaulting 1 st column value in the text field it is attached to.
非常感谢您的帮助.谢谢!!
Would really appreciate your help. Thanks !!
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);
}
}
}
推荐答案
您的意思
-
使用未经修饰的
JDialog
代替JWindow
,否则JTextComponets
不可编辑
JDialog
对AlwaysOnTop
和ModalityTypes
有更好的支持(仅硬编码到setAlwaysOnTop
)
JDialog
has better support for AlwaysOnTop
and ModalityTypes
(only hardcoded to the setAlwaysOnTop
)
不要一次又一次地重新创建基本设置,不要重新创建弹出式窗口,使用lolac变量,删除所有内容并添加一个新的ony,一次为JDialog定义所有设置,请参见例外(该对话框是可显示的.)如果弹出窗口被隐藏并且JTextField中的值不为空
don't to recreate basic setting again and again, don't to recreate a popup windod, use lolac variable, remove all contents and add a new ony, define all setting for JDialog once time, see exception (The dialog is displayable.) in the case that popup is hidden and value in JTextField isn't empty
在ESC key event
上隐藏JDialog
的隐藏动作,如果JFrame
最小化,则隐藏JDialog
missed action for hide JDialog
on ESC key event
, hide JDialog
, if is JFrame
minimalized
如果调整了JFrame
的大小,则无法与JDialog
一起移动,而是在另一个Point
missed moving with JDialog
if JFrame
is resized, moved over screen on another Point
,
直接将Document
附加到JTextField
,而不丢失在JScrollPane
定义中的任何地方
attach Document
to the JTextField
directly, not lost somwhere in to the JScrollPane
definitions
您的代码缺少一些好的做法,然后一切皆有可能,但是使用此代码(在我进行编辑后也无法)
your code missing a few good practicies, then everything is possible but not with this code (after my edit too)
搜索JCalendar by Kay Toedter
(我在弹出窗口中看到的最佳弹出解决方法)
search for JCalendar by Kay Toedter
(best popup workaround as I saw for popup window)
仅删除了uselles方法,从-移到了,注意non_optimized_code
,简单地放在了recyclebin
并再次写下了
only removed uselles methods, moved from- to, notice non_optimized_code
, simple put to the recyclebin
and wrote that again
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.JDialog;
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.ListSelectionModel;
import javax.swing.RowFilter;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
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 static JFrame frame = new JFrame();
private JTextField editor;
private JButton arrowButton;
//private JToggleButton arrowButton;
private JDialog popupWindow = new JDialog(frame);
private Component userComponent;
private JTable table;
private TableRowSorter<DefaultTableModel> rowSorter;
private Object[] columnNames;
private DefaultTableModel tableModel;
private Object[][] sheetDataInString;
public SearchBox() {
super(new FlowLayout(FlowLayout.LEFT, 0, 0));
editor = new JTextField();
editor.setPreferredSize(new Dimension(250, 25));
editor.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void changedUpdate(DocumentEvent e) {
newFilter();
}
@Override
public void insertUpdate(DocumentEvent e) {
newFilter();
}
@Override
public void removeUpdate(DocumentEvent e) {
newFilter();
}
private void newFilter() {
RowFilter<DefaultTableModel, Object> rowFilter = null;
Document document = editor.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);
}
});
initialize();
addListeners();
setBorders();
}
private void addListeners() {
arrowButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (!popupWindow.isVisible()) {
showWindow();
} else {
hideWindow();
}
}
});
}
protected void showWindow() {
if (null != getWindowComponent()) {
popupWindow.getContentPane().add(getWindowComponent());
popupWindow.setLocation(new Point(SearchBox.this.getLocationOnScreen().x, SearchBox.this.getLocationOnScreen().y + 25));
popupWindow.setUndecorated(true);
popupWindow.setAlwaysOnTop(true);
popupWindow.pack();
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
popupWindow.setVisible(true);
}
});
}
}
protected void hideWindow() {
popupWindow.setVisible(false);
}
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() {
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 {
private static final long serialVersionUID = 1L;
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 true;
}
};
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));
table.setPreferredScrollableViewportSize(table.getPreferredSize());
setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
getViewport().add(table);
}
}
}
这篇关于箭头键在自定义搜索组件中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!