精华内容
下载资源
问答
  • I'm trying to listen to a change of selection in a Java JComboBox. I have tried to use an ActionListener but the problem is this: the action listener does something like thispublic void actionPerforme...

    I'm trying to listen to a change of selection in a Java JComboBox. I have tried to use an ActionListener but the problem is this: the action listener does something like this

    public void actionPerformed(ActionEvent e){

    JComboBox source = ((JComboBox)e.getSource());

    String selected = source.getItemAt(source.getSelectedIndex());

    /*now I compare if the selected string is equal to some others

    and in a couple of cases I have to add elements to the combo*/

    }

    As you can notice, when I need to add elements to the combo another event is fired and the actionPerformed method is called again, even if I don't want that, and the code may loops... :(

    Is there any way to listen to the selection change only and not to a generic change event?

    Thanks

    解决方案

    You can try itemStateChanged() method of the ItemListener interface:

    class ItemChangeListener implements ItemListener{

    @Override

    public void itemStateChanged(ItemEvent event) {

    if (event.getStateChange() == ItemEvent.SELECTED) {

    Object item = event.getItem();

    // do something with object

    }

    }

    }

    And add the listener to your JComboBox:

    source.addItemListener(new ItemChangeListener());

    展开全文
  • java JComboBox的使用

    2010-06-21 17:05:53
    java JComboBox的使用 java JComboBox的使用 java JComboBox的使用
  • java JComboBox的事件处理

    热门讨论 2010-06-21 17:06:16
    java JComboBox的事件处理 java JComboBox的事件处理 java JComboBox的事件处理
  • I have populated a combobox B1 from database. When itemStateChanged event raises it should populate another combobox B2, but its not working.ArrayList1 = //call method in database connection class()fo...

    I have populated a combobox B1 from database. When itemStateChanged event raises it should populate another combobox B2, but its not working.

    ArrayList1 = //call method in database connection class()

    for (int j = 0; j < ArrayList1.size(); j++)

    {

    if (j == 0)

    {

    combobox1.addItem("Select Any");

    }

    combobox1.addItem(ArrayList1.get(j));

    }

    combobox1.addItemListener(new ItemListener()

    {

    @Override

    public void itemStateChanged(ItemEvent ie)

    {

    String catName = (String)combobox1.getSelectedItem();

    if (!catName.equalsIgnoreCase("Select Any"))

    {

    ArrayList2=//call method in DB class with cat_name as argument

    for(int i=0;i < ArrayList2.size();i++)

    {

    if (i == 0)

    {

    combobox2.addItem("Select Any");

    }

    combobox2.addItem(ArrayList2.get(i));

    }

    }

    }

    });

    first combobox gets populated from database, but after selecting any item from it second combobox keeps empty.

    and why debugging this my computer hangs on?

    解决方案

    DefaultComboBoxModel model = new DefaultComboBoxModel(yourstringarray);

    item_combobox.setModel( model );

    n ma problem get solved....

    展开全文
  • Java JComboBox下拉框

    2019-04-06 11:43:05
    package Window; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;...public class MyJComboBoxTest extends JFrame { ... JComboBox jcb; ...
    package Window;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class MyJComboBoxTest extends JFrame {
    
        JComboBox jcb;  //下拉框
        JPanel jp;    //面板
    
        //构造函数
        public MyJComboBoxTest(){
            jp = new JPanel();
    
            String str1[] = {"1", "2", "3", "4"};
            jcb = new JComboBox(str1);
            jcb.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent actionEvent) {
                    String itemSize = (String) jcb.getSelectedItem();
                    System.out.println(itemSize);
                }
            });
    
            jp.add(jcb);
    
            this.setLayout(new GridLayout(1, 1));
            this.add(jp);
    
            this.setSize(200,200);
            this.setTitle("下拉框");
            this.setVisible(true);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    
        public static void main(String[] args) {
            new MyJComboBoxTest();
    
        }
    
    }
    

     

    展开全文
  • I have a simple JComboBox filter code like this :import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import java.util.ArrayList;import java.util.List;import javax.swing.DefaultComboBoxMode...

    I have a simple JComboBox filter code like this :

    import java.awt.event.KeyAdapter;

    import java.awt.event.KeyEvent;

    import java.util.ArrayList;

    import java.util.List;

    import javax.swing.DefaultComboBoxModel;

    import javax.swing.JComboBox;

    import javax.swing.JFrame;

    import javax.swing.JTextField;

    import javax.swing.SwingUtilities;

    import javax.swing.UIManager;

    public class FilterComboBox extends JComboBox {

    private List array;

    public FilterComboBox(List array) {

    super(array.toArray());

    this.array = array;

    this.setEditable(true);

    final JTextField textfield = (JTextField) this.getEditor().getEditorComponent();

    textfield.addKeyListener(new KeyAdapter() {

    public void keyReleased(KeyEvent ke) {

    SwingUtilities.invokeLater(new Runnable() {

    public void run() {

    comboFilter(textfield.getText());

    }

    });

    }

    });

    }

    public void comboFilter(String enteredText) {

    List filterArray= new ArrayList();

    for (int i = 0; i < array.size(); i++) {

    if (array.get(i).toLowerCase().contains(enteredText.toLowerCase())) {

    filterArray.add(array.get(i));

    }

    }

    if (filterArray.size() > 0) {

    this.setModel(new DefaultComboBoxModel(filterArray.toArray()));

    this.setSelectedItem(enteredText);

    this.showPopup();

    }

    else {

    this.hidePopup();

    }

    }

    /* Testing Codes */

    public static List populateArray() {

    List test = new ArrayList();

    test.add("");

    test.add("Mountain Flight");

    test.add("Mount Climbing");

    test.add("Trekking");

    test.add("Rafting");

    test.add("Jungle Safari");

    test.add("Bungie Jumping");

    test.add("Para Gliding");

    return test;

    }

    public static void makeUI() {

    JFrame frame = new JFrame("Adventure in Nepal - Combo Filter Test");

    FilterComboBox acb = new FilterComboBox(populateArray());

    frame.getContentPane().add(acb);

    frame.pack();

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setVisible(true);

    }

    public static void main(String[] args) throws Exception {

    //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

    makeUI();

    }

    }

    The performance of the combo filter is not so good but it is fine for few data set. My problem is - when I remove the comment UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); to change look and feel, the filter doesn't work. In WindowsLookAndFeel, the combo box only takes single character in it by replacing the previously entered character.

    KY9uP.png

    Can you please tell me whats going on? Manoj Shrestha's answer below helps in some way but , can you please provide some other suggestions to achieve combo box filter in Java?

    解决方案

    Firstly you are creating new model everytime and then invoking show popup from code which leads to flickering etc. We can modify the model itself. Secondly you set the currently entered text as selected item which seems to have selectAll behavior as noted by others. I have modified the code as follows:

    public void comboFilter(String enteredText) {

    if (!this.isPopupVisible()) {

    this.showPopup();

    }

    List filterArray= new ArrayList();

    for (int i = 0; i < array.size(); i++) {

    if (array.get(i).toLowerCase().contains(enteredText.toLowerCase())) {

    filterArray.add(array.get(i));

    }

    }

    if (filterArray.size() > 0) {

    DefaultComboBoxModel model = (DefaultComboBoxModel) this.getModel();

    model.removeAllElements();

    for (String s: filterArray)

    model.addElement(s);

    JTextField textfield = (JTextField) this.getEditor().getEditorComponent();

    textfield.setText(enteredText);

    }

    }

    Hope it works for you.

    展开全文
  • <code class="language-java">import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JLabel; import javax.swing.JButton; import javax.swing.JTextField; import java.awt.Color; import ...
  • Below statement in if condition is not working, please provide me some solution that how to set selected item for JComboBox which are populated by ENUM.public enum EbayContryEnum{AT (3),AU (4),B...
  • I have this error when i try to add items in the JComboBoxincompatible types: ComboBox cannot be converted to StringThis is my method to load the data from database to the JComboBox...public final voi...
  • sexBox=new JComboBox(sexList); sexBox.setPreferredSize(new Dimension(120,25)); String[] classList= {"一班","二班","三班","四班"}; classBox=new JComboBox(classList); classBox....
  • /* * 功能:JCombobox和JList的使用 */ package com.swing; import java.awt.GridLayout; import javax.swing.*; class jcombobox extends JFrame{ public static void main(String[] args){ ...
  • java JComboBox 自动查找

    千次阅读 2008-02-29 17:29:00
    import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.util.List;import java.util.Vector;import ja
  • java JComboBox 放置图片

    千次阅读 2008-02-29 17:09:00
    package test1;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class test1 extends JPanel { ImageIcon images[]; public test1() { //Load the pet images Strin
  • 版权声明:本文为博主原创文章,未经博主允许不得转载。 目录(?)[+] 参考资料: ...https://www.java.net/node/650758 https://community.oracle.com/thread/2332288?start=0&tstart=0...
  • //下拉框选项居中 import java.awt.Color; import java.awt.Component; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.ListCellRenderer; import javax.swing.SwingConstants; public ...
  • java JComboBox显示值与绑定至分开

    千次阅读 2008-02-27 10:45:00
    public class PMMdmComboBox extends JComboBox {  String[] desc;  boolean isNeedName = false;  Vector[] defaultValue;  public void setisNeedName(boolean isneed) {  this.isNeedName = isneed;  } ...
  • Java下拉框JComboBox举例

    千次阅读 2014-10-25 19:39:35
    import java.awt.event.ActionEvent; import java.awt.event.ActionListener;...import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JPanel; public class Demo10 extends JFrame implem
  • Java:JComboBox 用法

    千次阅读 2012-11-25 21:47:56
    用Vector建立 JcomboBox 中出现了一个哭笑不得的情况,最后发现了 private Vector vComID = new Vector(); private JComboBox cbComID ;cbComID = new JComboBox(vComID); 这样建立才是正确的 JComboBox...
  • JavaJComboBox详解

    万次阅读 多人点赞 2019-01-07 09:28:47
    将按钮或可编辑字段与下拉列表组合的组件。用户可以从下拉列表中选择值,下拉列表在用户请求时显示。如果使组合框处于可编辑状态,则组合框将包括用户可在...  此类实现 JComboBox 类的可访问性支持。 static...
  • Java Swing JComboBox 数据刷新

    千次阅读 2019-05-12 15:55:51
    相关源码 可见:setModel...Java GUI编程中,按照历史进度:AWT -> Swing -> SWT。 在Swing编程中,JComboBox为下拉列表插件。 use JComboBox有两种方式传入数据。 - Vector<T> - E[] 我遇到的...
  • javaJComBoBox的使用

    千次阅读 2016-09-21 20:31:16
    下拉列表(JComboBox)使用方法及示例详解 * JComboBox() 建立一个JComboBox组件. * JComboBox(ComboBoxModel model) 根据model建立一个JComboBox组件. * JComboBox(Objext[] items) 利用数组对象建立一个JComboBox...
  • java 取出JComboBox中的当前值

    千次阅读 2019-01-12 14:41:56
    java 取出JComboBox中的当前值
  • 主要介绍了Java Swing JComboBox下拉列表框的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
  • Swing学习10:Java Swing JComboBox

    千次阅读 2020-03-13 14:56:31
    下拉列表的特点是将多个选项折叠在一起,只显示最前面的或被选中的一个。选择时需要单击下拉列表右边的下三角按钮,这时候会弹出包含所有选项...JComboBox():创建一个空的 JComboBox 对象。 JComboBox(ComboBoxMod...
  • java 利用ComboModel构造JComboBox java 利用ComboModel构造JComboBox

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 7,038
精华内容 2,815
关键字:

javajcombobox

java 订阅