首页 \ 问答 \ 自定义JComboBox编辑器(Custom JComboBox editor)

自定义JComboBox编辑器(Custom JComboBox editor)

我被困在添加一个按钮到一个JComboBox编辑器,我成功地添加了一个按钮,但我得到了一些问题,如当我第一次进入编辑器时,一个动作执行事件被触发,这是不可接受的,另一个是我无法得到文字输入。

结果: 结果

问题:

问题

    @Override  
        public Component getEditorComponent() {  
            return panel;  
        }  

这是问题,如果我返回panel.jtexfield我只得到一个没有按钮的文本字段,那么这里有什么窍门?

这是我的代码

    import Store.util.DatabaseHelper;  
import java.awt.*;  
import java.awt.event.*;  
import java.util.ArrayList;  
import javax.swing.*;  
import javax.swing.border.Border;  
import javax.swing.plaf.basic.BasicComboBoxEditor;  
import org.hibernate.HibernateException;  
import org.netbeans.lib.awtextra.AbsoluteLayout;  

public class NewComboTest extends JFrame {  

    private ArrayList<Object> shopCart = new ArrayList<Object>();  
    private JComboBox cb;  
    private static final Object[] comboContents = {  
        "First", "Second", "Third", "Fourth", "Fifth"  
    };  

    public NewComboTest() {  

        super("New Combo Test");  
        setLayout(null);  
        cb = new JComboBox();  
        cb.setRenderer(new NewComboRenderer());  

        cb.setEditor(new NewComboEditor());  
        cb.setEditable(true);  

        cb.setSize(new Dimension(350, 100));  
        for (int i = 0; i < comboContents.length; i++) {  
            cb.addItem(comboContents[ i]);  
        }  
        cb.addActionListener(new ActionListener() {  
            @Override  
            public void actionPerformed(ActionEvent e) {  
                System.out.println("_____________" + cb.getSelectedItem());  

                shopCart.add(cb.getSelectedItem());  
                System.out.println("items added" + shopCart);  
            }  
        });  
        cb.getEditor().getEditorComponent().addKeyListener(new KeyAdapter() {  
            @Override  
            public void keyReleased(KeyEvent e) {  
                System.out.println("KeyReleased" + cb.getEditor().getItem().toString());  
                populateModel(cb.getEditor().getItem().toString());  
            }  
        });  

        getContentPane().add(cb, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 200, 480, 50));  
        setSize(1200, 450);  
        setDefaultCloseOperation(EXIT_ON_CLOSE);  
        setVisible(true);  
    }  

    public static void main(String[] arg) {  
        new NewComboTest();  
    }  

    private class NewComboEditor extends JPanel implements ComboBoxEditor {  

        JTextField tf;  
        JButton eraseButton;  
        textPanel panel = new textPanel();  

        public NewComboEditor() {  

        }  

        @Override  
        public void addActionListener(ActionListener l) {  
            tf.addActionListener(l);  
        }  

        @Override  
        public Component getEditorComponent() {  
            return panel;  
        }  
        public Component getEditorComponent2() {  
            return panel;  
        }  

        @Override  
        public Object getItem() {  
            return tf.getText();  

        }  

        @Override  
        public void removeActionListener(ActionListener l) {  
            tf.removeActionListener(l);  
        }  

        @Override  
        public void selectAll() {  
            tf.selectAll();  
        }  

        @Override  
        public void setItem(Object o) {  
            if (o != null) {  
                tf.setText(tf.getText());  
            } else {  
                tf.setText("");  
            }  
        }  

        private class textPanel extends JPanel {  

            JTextField jTextField1 = new JTextField();  
            JButton jButton1 = new JButton();  

            public textPanel() {  
                setLayout(new BorderLayout());  

                jButton1.setBackground(new java.awt.Color(255, 255, 255));  
                jButton1.setForeground(new java.awt.Color(0, 51, 51));  
                jButton1.setText("X");  
                jButton1.addActionListener(new ActionListener() {  
                    @Override  
                    public void actionPerformed(ActionEvent e) {  
                        jTextField1.setText("");  
                    }  
                });  

                add(jTextField1, BorderLayout.CENTER);  
                add(jButton1, BorderLayout.EAST);  





            }  

            public String getText(){  
                return jTextField1.getText();  
            }  
        }  
    }  

    private class NewComboRenderer extends JLabel implements ListCellRenderer {  

        public NewComboRenderer() {  
            setOpaque(true);  
        }  

        public Component getListCellRendererComponent(  
                JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {  
            setText(value.toString());  

            setBackground(isSelected ? Color.BLUE : Color.white);  
            setForeground(isSelected ? Color.white : Color.red);  
            return this;  
        }  
    }  

   /* public void populateModel(String text) throws HibernateException { 
        java.util.List l = DatabaseHelper.GetProductsBy(text); 

        for (Object object : l) { 
            cb.addItem(object); 
        } 
ignore this its unnecessary. 
*/  

    }  
}  

我也希望将文本字体和大小设置为与组合框中的设置相同。


i got stuck at adding a button to a JComboBox editor, I succeeded to add a button but I got some issues like when I first enter to the editor an action perform event gets fired which is unacceptable and the other is I can't get the text typed.

Result: result

Problems:

problems

    @Override  
        public Component getEditorComponent() {  
            return panel;  
        }  

This is the problem, if I return panel.jtexfield I only get a text field without a button, so what's the trick here?

Here is my code

    import Store.util.DatabaseHelper;  
import java.awt.*;  
import java.awt.event.*;  
import java.util.ArrayList;  
import javax.swing.*;  
import javax.swing.border.Border;  
import javax.swing.plaf.basic.BasicComboBoxEditor;  
import org.hibernate.HibernateException;  
import org.netbeans.lib.awtextra.AbsoluteLayout;  

public class NewComboTest extends JFrame {  

    private ArrayList<Object> shopCart = new ArrayList<Object>();  
    private JComboBox cb;  
    private static final Object[] comboContents = {  
        "First", "Second", "Third", "Fourth", "Fifth"  
    };  

    public NewComboTest() {  

        super("New Combo Test");  
        setLayout(null);  
        cb = new JComboBox();  
        cb.setRenderer(new NewComboRenderer());  

        cb.setEditor(new NewComboEditor());  
        cb.setEditable(true);  

        cb.setSize(new Dimension(350, 100));  
        for (int i = 0; i < comboContents.length; i++) {  
            cb.addItem(comboContents[ i]);  
        }  
        cb.addActionListener(new ActionListener() {  
            @Override  
            public void actionPerformed(ActionEvent e) {  
                System.out.println("_____________" + cb.getSelectedItem());  

                shopCart.add(cb.getSelectedItem());  
                System.out.println("items added" + shopCart);  
            }  
        });  
        cb.getEditor().getEditorComponent().addKeyListener(new KeyAdapter() {  
            @Override  
            public void keyReleased(KeyEvent e) {  
                System.out.println("KeyReleased" + cb.getEditor().getItem().toString());  
                populateModel(cb.getEditor().getItem().toString());  
            }  
        });  

        getContentPane().add(cb, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 200, 480, 50));  
        setSize(1200, 450);  
        setDefaultCloseOperation(EXIT_ON_CLOSE);  
        setVisible(true);  
    }  

    public static void main(String[] arg) {  
        new NewComboTest();  
    }  

    private class NewComboEditor extends JPanel implements ComboBoxEditor {  

        JTextField tf;  
        JButton eraseButton;  
        textPanel panel = new textPanel();  

        public NewComboEditor() {  

        }  

        @Override  
        public void addActionListener(ActionListener l) {  
            tf.addActionListener(l);  
        }  

        @Override  
        public Component getEditorComponent() {  
            return panel;  
        }  
        public Component getEditorComponent2() {  
            return panel;  
        }  

        @Override  
        public Object getItem() {  
            return tf.getText();  

        }  

        @Override  
        public void removeActionListener(ActionListener l) {  
            tf.removeActionListener(l);  
        }  

        @Override  
        public void selectAll() {  
            tf.selectAll();  
        }  

        @Override  
        public void setItem(Object o) {  
            if (o != null) {  
                tf.setText(tf.getText());  
            } else {  
                tf.setText("");  
            }  
        }  

        private class textPanel extends JPanel {  

            JTextField jTextField1 = new JTextField();  
            JButton jButton1 = new JButton();  

            public textPanel() {  
                setLayout(new BorderLayout());  

                jButton1.setBackground(new java.awt.Color(255, 255, 255));  
                jButton1.setForeground(new java.awt.Color(0, 51, 51));  
                jButton1.setText("X");  
                jButton1.addActionListener(new ActionListener() {  
                    @Override  
                    public void actionPerformed(ActionEvent e) {  
                        jTextField1.setText("");  
                    }  
                });  

                add(jTextField1, BorderLayout.CENTER);  
                add(jButton1, BorderLayout.EAST);  





            }  

            public String getText(){  
                return jTextField1.getText();  
            }  
        }  
    }  

    private class NewComboRenderer extends JLabel implements ListCellRenderer {  

        public NewComboRenderer() {  
            setOpaque(true);  
        }  

        public Component getListCellRendererComponent(  
                JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {  
            setText(value.toString());  

            setBackground(isSelected ? Color.BLUE : Color.white);  
            setForeground(isSelected ? Color.white : Color.red);  
            return this;  
        }  
    }  

   /* public void populateModel(String text) throws HibernateException { 
        java.util.List l = DatabaseHelper.GetProductsBy(text); 

        for (Object object : l) { 
            cb.addItem(object); 
        } 
ignore this its unnecessary. 
*/  

    }  
}  

I also wish to set the text font and size to the same as the set up at the combo box.


原文:https://stackoverflow.com/questions/17892276
更新时间:2021-12-25 21:12

最满意答案

NSArrayController不跟踪对可变数组的更改,只是更改它正在观察的array属性。 所以你要做的是:

NSMutableArray  *mutableArray = [[NSMutableArray alloc] init];

SomeObject* foo = [[Object alloc] init];
foo.text = @"sup";
[mutableArray addObject:foo]; //Repeat this a few times

array=mutableArray;

阵列控制器将看到array被更改为填充的数组。


As it turned out, I should have:

  1. Linked the array controller as a property to my app delegate
  2. Added the objects to the array controller itself

This was in my app delegate.h:

@property (copy) NSMutableArray *pastes;
@property (assign) IBOutlet NSArrayController *controller;

and my app delegate.m:

@synthesize pastes, controller;

 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    pastes = [[NSMutableArray alloc] init];

    [controller addText:@"sup"];
    [controller addText:@"hey"];
    [controller addText:@"hi"];
}

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。