我希望我的组合框顶部瓷砖只有在我们点击回车键时才会更新,而不是在使用向上/向下箭头滚动时。
当前,当我使用向上/向下箭头滚动时,当前突出显示的当前项目将更新为选中项目JComboBox
的顶部平铺。我需要避免这样。
这是我的代码。提前感谢!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingControlDemo {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
public SwingControlDemo() {
prepareGUI();
}
public static void main(String[] args) {
SwingControlDemo swingControlDemo = new SwingControlDemo();
swingControlDemo.showComboboxDemo();
}
private void prepareGUI() {
mainFrame = new JFrame("Java Swing Examples");
mainFrame.setSize(400, 400);
mainFrame.setLayout(new GridLayout(3, 1));
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent) {
System.exit(0);
}
});
headerLabel = new JLabel("", JLabel.CENTER);
statusLabel = new JLabel("", JLabel.CENTER);
statusLabel.setSize(350, 100);
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
private void showComboboxDemo() {
headerLabel.setText("Control in action: JComboBox");
final DefaultComboBoxModel fruitsName = new DefaultComboBoxModel();
fruitsName.addElement("Apple");
fruitsName.addElement("Grapes");
fruitsName.addElement("Mango");
fruitsName.addElement("Peer");
fruitsName.addElement("java");
fruitsName.addElement("Mango");
final JComboBox fruitCombo = new JComboBox(fruitsName);
fruitCombo.setSelectedIndex(0);
JScrollPane fruitListScrollPane = new JScrollPane(fruitCombo);
JButton showButton = new JButton("Show");
fruitCombo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
if (arg0.getModifiers() == ActionEvent.MOUSE_EVENT_MASK || arg0.getModifiers() == ActionEvent.KEY_EVENT_MASK || true) {
System.out.println("arg0" + " " + arg0.getModifiers());
}
}
});
fruitCombo.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
if (arg0.getKeyCode() == KeyEvent.VK_ENTER) {
System.out.println("enter key pressed" + arg0.getKeyCode());
}
}
@Override
public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
}
});
System.out.println(fruitCombo.getComponentListeners().toString());
controlPanel.add(fruitListScrollPane);
controlPanel.add(showButton);
mainFrame.setVisible(true);
}
}
Oracle Java已经解决了这个问题,因为它与使用鼠标进行选择不同。但要启用修复,我们必须使用键“ComboBox.noActionOnKeyNavigation”更改默认的SWING UI属性。在源代码中实现的修复中,他们使用此属性作为标志。如果我们将其设为真,则使用键盘的JComboBox选择不会触发动作事件。此链接将为您提供修复的详细信息。这来自OpenJDK,但我已经在Oracle JDK 1.8.111中确认了这一点http://osdir.com/ml/swing-dev-java-openjdk/2013-01/msg00029.html
所以基本上我们要加上这条车道
UIManager.getLookAndFeelDefaults().put(“ComboBox.noActionOnKeyNavigation”, true);
我找到了一个使用自定义键绑定操作的解决方案。下面是包含这些更改的代码。
package abcd;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.plaf.basic.BasicComboPopup;
public class SwingControlDemo {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
public SwingControlDemo(){
prepareGUI();
}
public static void main(String[] args){
SwingControlDemo swingControlDemo = new SwingControlDemo();
swingControlDemo.showComboboxDemo();
}
private void prepareGUI(){
mainFrame = new JFrame("Java Swing Examples");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
headerLabel = new JLabel("", JLabel.CENTER);
statusLabel = new JLabel("",JLabel.CENTER);
statusLabel.setSize(350,100);
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
private void showComboboxDemo(){
headerLabel.setText("Control in action: JComboBox");
final DefaultComboBoxModel fruitsName = new DefaultComboBoxModel();
fruitsName.addElement("Apple");
fruitsName.addElement("Grapes");
fruitsName.addElement("Mango");
fruitsName.addElement("Peer");
fruitsName.addElement("java");
fruitsName.addElement("mkm");
final JComboBox fruitCombo = new JComboBox(fruitsName);
fruitCombo.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke("DOWN"), "cDownArrow");
fruitCombo.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke("UP"), "cUpArrow");
Action customDownArrow = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
System.out.println("in");
JComboBox box = (JComboBox)arg0.getSource();
BasicComboPopup pop = (BasicComboPopup)box.getAccessibleContext().getAccessibleChild(0);
JList list = pop.getList();
System.out.println("next index" + " "+ (list.getSelectedIndex()+1)%box.getItemCount());
list.setSelectedIndex((list.getSelectedIndex()+1)%box.getItemCount());
}
};
Action customUpArrow = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
JComboBox box = (JComboBox)arg0.getSource();
BasicComboPopup pop = (BasicComboPopup)box.getAccessibleContext().getAccessibleChild(0);
JList list = pop.getList();
System.out.println("next index" + " "+ (list.getSelectedIndex()+1)%box.getItemCount());
list.setSelectedIndex(((list.getSelectedIndex()-1)%box.getItemCount()+box.getItemCount())%box.getItemCount());
}
};
fruitCombo.getActionMap().put("cDownArrow",customDownArrow);
fruitCombo.getActionMap().put("cUpArrow", customUpArrow);
fruitCombo.setSelectedIndex(0);
JScrollPane fruitListScrollPane = new JScrollPane(fruitCombo);
JButton showButton = new JButton("Show");
fruitCombo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
if(arg0.getModifiers()==ActionEvent.MOUSE_EVENT_MASK || arg0.getModifiers()==ActionEvent.KEY_EVENT_MASK||true){
System.out.println("arg0"+" "+arg0.getModifiers());
}
}
});
/*fruitCombo.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
if(arg0.getKeyCode()==KeyEvent.VK_ENTER){
System.out.println("enter key pressed"+arg0.getKeyCode());
}
}
@Override
public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
}
});*/
System.out.println(fruitCombo.getComponentListeners().toString());
controlPanel.add(fruitListScrollPane);
controlPanel.add(showButton);
mainFrame.setVisible(true);
}
}
您可以在组合框上设置一个属性,以防止在使用箭头键时触发事件:
comboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
注意:使用上述属性时,您可能(也可能不)需要手动关闭弹出窗口:
/*
This works on non editable combo boxes
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.plaf.basic.*;
import javax.swing.text.*;
public class ComboBoxAction extends JFrame implements ActionListener
{
public ComboBoxAction()
{
JComboBox<String> comboBox = new JComboBox<String>();
comboBox.addActionListener( this );
comboBox.addItem( "Item 1" );
comboBox.addItem( "Item 2" );
comboBox.addItem( "Item 3" );
comboBox.addItem( "Item 4" );
// This prevents action events from being fired when the
// up/down arrow keys are used on the dropdown menu
comboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
getContentPane().add( comboBox );
getContentPane().add( new JTextField(), BorderLayout.SOUTH );
}
public void actionPerformed(ActionEvent e)
{
System.out.println( e.getModifiers() );
JComboBox comboBox = (JComboBox)e.getSource();
System.out.println( comboBox.getSelectedItem() );
// make sure popup is closed when 'isTableCellEditor' is used
// comboBox.hidePopup();
}
public static void main(String[] args)
{
ComboBoxAction frame = new ComboBoxAction();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setVisible( true );
}
}
我正在寻找一种滚动以在顶部显示选定项的方法。 RecycerView不支持滚动到绝对位置 如何滚动以将选定项放在视图的顶部?
我在网上找不到关于我的问题的任何信息,所以我想在这里分享一下。 我的问题是:我正在开发一款平板电脑应用程序,它有一个ActionBar。所述ActionBar的显示选项之前是在我的活动“onCreate through”中设置的: 当时一切都很顺利,主页图标上会自动显示一个箭头,向用户显示他可以向上导航。 现在,我想通过XML样式来设置它——我已经有了用于背景的样式,但只是关于用于显示选项。我是这
我一直在寻找一个适当的API来做我需要做的事情,我几乎可以肯定的是,必须有一个API。但我找不到。 提前致谢
问题内容: 我在JScrollPane中有一个JTextArea组件,并且文本区域不可编辑。我想使用向上和向下箭头键滚动文本区域(即,按箭头键将文本区域滚动一行)。任何想法如何实现这一目标? 问题答案: 是的,键绑定是必经之路,但是您不一定总是需要创建自己的动作。Swing组件带有您经常可以重用的默认操作。 有关这些操作的完整列表,请参见键绑定。 现在您知道了动作名称,您可以将其绑定到keyStr
在我用Java创建的Window 95模拟器的“位置信息”JDialog中,在“你现在在哪个国家?”的问题下面有一个JComboBox。 为什么JComboBox没有显示其默认项和右侧的箭头?JComboBox中有一个指向下方的箭头。 以下是位置信息JDialog中包含的对象的相关代码片段:
问题内容: 假设我要对JComboBox中的箭头使用自定义图像,该怎么做? 我知道可以使用synth xml文件甚至UIManager.put(…),但是我不知道如何。我现在要做的就是将箭头图像更改为其他图像,无论是以编程方式还是仅覆盖其使用的图像。我到底该怎么做? 问题答案: 您可以在中覆盖。是一个方便的起点。 然后安装它。