自定义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:
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
最满意答案
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:
- Linked the array controller as a property to my app delegate
- 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"]; }
相关问答
更多-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
您需要的代码的重要部分是DataSource委托函数: extension ViewController : NSTableViewDataSource { func numberOfRows(in tableView: NSTableView) -> Int { return devices.count } func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) - ...
-
你的数组控制器正在观察你的App Delegate的“数组”属性。 这意味着只有在设置了数组对象时才会发送KVO通知,而不是在添加对象时发送。 这听起来像你正在使用一个NSMutableArray并向它添加对象,这就解释了为什么数组控制器没有被通知变化,因为底层对象没有改变。 简单的解决方案是将你的电话打包在一个will / did change块中,如下所示: [self willChangeValueForKey:@"array"]; [self.array addObject:[NSDictionar ...
-
阵列控制器添加后如何排序NSTableView内容?(How is NSTableView Content Sorted After Array Controller Add?)[2022-04-29]
阵列控制器“排列”其内容。 它通过调用-arrangeObjects:方法来实现。 默认实现使用数组控制器的filterPredicate过滤内容,然后使用其sortDescriptors对其进行排序。 子类可以覆盖该方法以不同方式排列内容。 可以使用arrangedObjects属性访问排列的内容。 这与表视图中的行对应。 因此,如果您按表行索引,则应始终使用此方法。 例如: NSManagedObject *ci = [self.arrayController.arrangedObjects objec ... -
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 ...
-
将NSTableView填充并绑定到多个阵列控制器(Populate and bind an NSTableView to multiple array controllers)[2022-06-19]
我认为你应该使用一个阵列控制器。 你可以在Fruit上设置一个名为liked的属性。 现在,您的“喜欢”复选框列已连接到arrangedObjects.liked 。 之后,当您想要确定所有喜欢水果的集合时,您可以查询您的水果数组: NSArray * likedFruits = [ allFruitsArray filteredArrayUsingPredicate:[ NSPredicate predicateWithFormat:@"liked = YES"] ] ; 如果在UI的另一部分中,您只显 ... -
完成以下操作后,我能够成功更新页面 将我的CPMutableArray制作成CPArrayController。 由于我只是添加和删除对象,因此代码中的其他位置不会发生更改。 使用KVO添加和删除对象:[[self mutableArrayValueForKey:@“persons”] addObject:me]; 我最大的问题是依赖./bootstrap.sh。 它不会安装和配置所有内容。 克隆了github / cappuccino / cappuccino并运行了Jake Install。 现在xco ...
-
map第一个数组以使用第二个数组中的相应元素填充它: var orderArray = ["5ace454a2b22e17597d0e694", "5acde7c0f7d2520e3b205971", "5ad2086bf05ad342dc723ea1"] var objectsArray = [ { _id: '5acde7c0f7d2520e3b205971', name: 'Dinner', restaurant: '5a68d8ea17d9e4308e6400c3', ...
-
感谢您的评论。 我确实错过了从表到阵列控制器的连接。 通过将table绑定到Selection Indexes下的array controller ,并将selectionIndexes设置为Controller Key来修复。 Thanks for the comments. I was indeed missing a connection from the table to the array controller. Fixed by binding the table to the array co ...
-
如何填充NSTableView?(How to populate a NSTableView?)[2021-09-21]
我怎么样: 填充我的NSMutableArray,它是Person对象的数组 第1步:创建Person对象。 第2步: 将它们添加到数组中。 您注释掉的代码就是这样做的,尽管您可能需要单独创建Person,以防您想要配置它(例如,设置其名称)。 我应该在我的基类的mainViewDidLoad方法中填充NSMutableArray吗? 在用户看到你创建它的模型之前,它并不重要,但从概念上讲,它对我来说有点气味。 它与视图没有任何关系,所以我说它属于init 。 当然,如果主视图及其中的每个视图都已加载,您需 ...