gpt4 book ai didi

java - 自定义 JComboBox 编辑器

转载 作者:行者123 更新时间:2023-11-29 07:07:45 24 4
gpt4 key购买 nike

我在向 JComboBox 编辑器添加按钮时遇到了困难,我成功添加了一个按钮,但我遇到了一些问题,比如当我第一次进入编辑器时,一个 Action 执行事件被触发,这是 Not Acceptable 另一个是我无法输入文本。

结果: result

问题:

problems

    @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.
*/

}
}

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

最佳答案

我能看到的第一组问题是,你在 NewComboEditor 中定义了一个 JTextFieldJButton,还定义了一个 textPanel,它以任何方式包含所有这些东西。但不是使用 textPane 上的组件,而是使用新创建的组件(在 NewComboEditor 中)......事实上,我什至不确定那是怎么回事可以工作,因为您永远不会初始化这些组件(在 NewComboEditor 中),所以应该有一个 NullPointerException...

如果这还不够,JTextFieldJButton 无论如何都不会添加到任何东西中......

相反...

  • NewComboEditor 不需要从任何东西扩展(或者如果你真的想的话,它可以从 textPane 扩展)。
  • 对该字段的所有引用都应指向 textPane 中的文本字段

举个例子...

enter image description here

import java.awt.Component;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionListener;
import javax.swing.ComboBoxEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class CustomComboBoxEditor {

public static void main(String[] args) {
new CustomComboBoxEditor();
}

public CustomComboBoxEditor() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}

JComboBox cb = new JComboBox();
cb.addItem("Apple");
cb.addItem("Banana");
cb.addItem("Orange");
cb.setEditable(true);
cb.setEditor(new MyComboBoxEditor());

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
frame.add(cb);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class MyComboBoxEditor implements ComboBoxEditor {

private EditorPane editorPane;

public MyComboBoxEditor() {
editorPane = new EditorPane();
}

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

@Override
public void setItem(Object anObject) {
editorPane.setText(anObject == null ? null : anObject.toString());
}

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

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

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

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

}

public class EditorPane extends JPanel {

private JTextField field;
private JButton button;

public EditorPane() {
field = new JTextField(10);
button = new JButton("X");
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
add(field, gbc);
gbc.weightx = 0;
gbc.fill = GridBagConstraints.NONE;
gbc.gridx++;
add(button, gbc);
}

@Override
public void addNotify() {
super.addNotify();
field.requestFocusInWindow();
}

public void selectAll() {
field.selectAll();
}

public void setText(String text) {
field.setText(text);
}

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

public void addActionListener(ActionListener listener) {
field.addActionListener(listener);
}

public void removeActionListener(ActionListener listener) {
field.removeActionListener(listener);
}

}

}

现在,如果您想将字段的属性设置为与组合框相同,我只需将组合框的引用传递给编辑器并允许它提取您需要的属性(即字体、颜色、等等)

关于java - 自定义 JComboBox 编辑器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17892276/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com