Java 将项目动态添加到JComboBox(value+;icon=jlabel)

Java 将项目动态添加到JComboBox(value+;icon=jlabel),java,swing,jcombobox,listcellrenderer,Java,Swing,Jcombobox,Listcellrenderer,我使用DefaultComboxModel将特定项添加到我的JComboxBox(字符串文本、图标)。但是出了点问题。当我将这两个项目添加到我的组合模型中时,它如下所示: ComboBoxWindow: [icon ] [icon value] private JComboBox combobox; ... DefaultComboBoxModel model = new DefaultComboBoxMo

我使用DefaultComboxModel将特定项添加到我的JComboxBox(字符串文本、图标)。但是出了点问题。当我将这两个项目添加到我的组合模型中时,它如下所示:

ComboBoxWindow: 

              [icon          ]

              [icon     value]
private JComboBox combobox;
...
DefaultComboBoxModel model = new DefaultComboBoxModel();
combobox = new JComboBox(model);
...
/*
 * I use JButton for 'sending' hex value taken from JTextField to 'combobox'
 * with help of 'addToComboBox()' method
 */
 public void addToComboBox() {

    String value = field.getText().toString();     // getin' text from 'left' JTextField

    Color color = tcc.getColor();                  // getin' color from some other JLabel
    ColorSwatch icon = new ColorSwatch(10, true);  // using some custom method to create little square icon
    icon.setColor(color);     // seting color of created icon

    combobox.addItem(icon);
    combobox.addItem(value);
 }
总之,我的combobox代码如下所示:

ComboBoxWindow: 

              [icon          ]

              [icon     value]
private JComboBox combobox;
...
DefaultComboBoxModel model = new DefaultComboBoxModel();
combobox = new JComboBox(model);
...
/*
 * I use JButton for 'sending' hex value taken from JTextField to 'combobox'
 * with help of 'addToComboBox()' method
 */
 public void addToComboBox() {

    String value = field.getText().toString();     // getin' text from 'left' JTextField

    Color color = tcc.getColor();                  // getin' color from some other JLabel
    ColorSwatch icon = new ColorSwatch(10, true);  // using some custom method to create little square icon
    icon.setColor(color);     // seting color of created icon

    combobox.addItem(icon);
    combobox.addItem(value);
 }
我考虑过使用ListCellRenderer,但我不知道如何通过同时使用“值”和“图标”来“告诉”它应该渲染,例如,一个JLabel组件。对我来说,使用JButton动态添加这些项是非常重要的


我做到了&现在效果很好:)基本1。我使用了DefaultComboxModel,2。我已经把它添加到我的JComboBox中,3。我添加了一个自定义ListCellRenderer,它将获取的字符串(例如#FFFFFF)“翻译”为图标和适当的文本,并在最后创建一个带有该新图标和文本的JLabel

/**
 * Main Class
 */
public class ColorChooser {
   ...
   public ColorChooser() {
      ...
      DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>();
      JComboBox combobox = new JComboBox<String>(model);
      combobox.setEditable(false);
      cobobox.setRenderer(new ComboRenderer());
      ...
   }
   ...
}

/**
 * Renderer Class
 */
public class ComboRenderer extends JLabel implements ListCellRenderer<Object> {

   public ComboRenderer() {

   }

   public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {

      setFont(newFont("Consolas", Font.PLAIN, 14));
      setOpaque(true);

      String hex;

      if (value != null) {

         /*
          * So basically I add to my 'model' in ColorChooser main class only Strings
          * which I get e.g. from some JTextField. 
          * On base of this String I create icon for future JLabel
          * and I set String 'value' as text for it.
          */
         hex = value.toString();

         Color color = HexToRgb(hex); //Method which translates String to Color

         ColorSwatch icon = new ColorSwatch(10, true); // ColorSwatch is a method which creates specific square icon (in this case a little square)
         icon.setColor(color);

         setText(hex);
         setIcon(icon);
      }
      return this;
   }

   /*
    * My translate method which translates given String to a specific color value
    * (RGB/RGBA)
    */
   public Color HexToRgb(String colorStr) {

      Color color = null;

      // For String hex value '#RRGGBB'
      if (colorStr.length() == 7) {

         color = new Color(
            Integer.valueOf(colorStr.substring(1, 3), 16),
            Integer.valueOf(colorStr.substring(3, 5), 16),
            Integer.valueOf(colorStr.substring(5, 7), 16));

        // For String hex value '#AARRGGBB'
      } else if (colorStr.length() == 9) {

         color = new Color(
            Integer.valueOf(colorStr.substring(3, 5), 16),
            Integer.valueOf(colorStr.substring(5, 7), 16),
            Integer.valueOf(colorStr.substring(7, 9), 16),
            Integer.valueOf(colorStr.substring(1, 3), 16));

        // For String hex value '0xRRGGBB'
      } else if (colorStr.length() == 8) {

         color = new Color(
            Integer.valueOf(colorStr.substring(2, 4), 16),
            Integer.valueOf(colorStr.substring(4, 6), 16),
            Integer.valueOf(colorStr.substring(6, 8), 16));

        // For String hex value '0xAARRGGBB'
      } else if (colorStr.length() == 10) {

         color = new Color(
            Integer.valueOf(colorStr.substring(4, 6), 16),
            Integer.valueOf(colorStr.substring(6, 8), 16),
            Integer.valueOf(colorStr.substring(8, 10), 16),
            Integer.valueOf(colorStr.substring(2, 4), 16));

      } else
         JOptionPane.showMessageDialog(null, "Something wen wrong... :|");

      return color;
   }
}

…我们现在到家了。希望代码能帮助某人:)

我做到了&它现在工作得很好:)基本上是1。我使用了DefaultComboxModel,2。我已经把它添加到我的JComboBox中,3。我添加了一个自定义ListCellRenderer,它将获取的字符串(例如#FFFFFF)“翻译”为图标和适当的文本,并在最后创建一个带有该新图标和文本的JLabel

/**
 * Main Class
 */
public class ColorChooser {
   ...
   public ColorChooser() {
      ...
      DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>();
      JComboBox combobox = new JComboBox<String>(model);
      combobox.setEditable(false);
      cobobox.setRenderer(new ComboRenderer());
      ...
   }
   ...
}

/**
 * Renderer Class
 */
public class ComboRenderer extends JLabel implements ListCellRenderer<Object> {

   public ComboRenderer() {

   }

   public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {

      setFont(newFont("Consolas", Font.PLAIN, 14));
      setOpaque(true);

      String hex;

      if (value != null) {

         /*
          * So basically I add to my 'model' in ColorChooser main class only Strings
          * which I get e.g. from some JTextField. 
          * On base of this String I create icon for future JLabel
          * and I set String 'value' as text for it.
          */
         hex = value.toString();

         Color color = HexToRgb(hex); //Method which translates String to Color

         ColorSwatch icon = new ColorSwatch(10, true); // ColorSwatch is a method which creates specific square icon (in this case a little square)
         icon.setColor(color);

         setText(hex);
         setIcon(icon);
      }
      return this;
   }

   /*
    * My translate method which translates given String to a specific color value
    * (RGB/RGBA)
    */
   public Color HexToRgb(String colorStr) {

      Color color = null;

      // For String hex value '#RRGGBB'
      if (colorStr.length() == 7) {

         color = new Color(
            Integer.valueOf(colorStr.substring(1, 3), 16),
            Integer.valueOf(colorStr.substring(3, 5), 16),
            Integer.valueOf(colorStr.substring(5, 7), 16));

        // For String hex value '#AARRGGBB'
      } else if (colorStr.length() == 9) {

         color = new Color(
            Integer.valueOf(colorStr.substring(3, 5), 16),
            Integer.valueOf(colorStr.substring(5, 7), 16),
            Integer.valueOf(colorStr.substring(7, 9), 16),
            Integer.valueOf(colorStr.substring(1, 3), 16));

        // For String hex value '0xRRGGBB'
      } else if (colorStr.length() == 8) {

         color = new Color(
            Integer.valueOf(colorStr.substring(2, 4), 16),
            Integer.valueOf(colorStr.substring(4, 6), 16),
            Integer.valueOf(colorStr.substring(6, 8), 16));

        // For String hex value '0xAARRGGBB'
      } else if (colorStr.length() == 10) {

         color = new Color(
            Integer.valueOf(colorStr.substring(4, 6), 16),
            Integer.valueOf(colorStr.substring(6, 8), 16),
            Integer.valueOf(colorStr.substring(8, 10), 16),
            Integer.valueOf(colorStr.substring(2, 4), 16));

      } else
         JOptionPane.showMessageDialog(null, "Something wen wrong... :|");

      return color;
   }
}

…我们现在到家了。希望代码能帮助某些人:)

动态添加项目到JComboBox(已经可见)=MutableComboxModel为了更好地帮助您尽快发布一个简短、可运行、可编译的文档,您正在通过
addItem()
添加两个项目,因此您将获得两行内容。。。您的
ColorSwatch
类是否有返回颜色六进制代码的
toString()
方法?有关Oracle教程如何使用组合框(部分提供自定义渲染器)的基本解决方法,请参阅关于ListCellRenderOh!我知道!我将只发送图标颜色的十六进制值,并在ListCellRenderer中使用ColorSwatch和sended十六进制值创建JLabel。毕竟,要创建带有指定图标和文本的JLabel,我只需要图标颜色的十六进制值:D,但问题是是否可以动态添加对象/Dynamic add items to JComboBox(已经可见)=MutableComboxModel要获得更好的帮助,请尽快发布一个简短的、可运行的、可编译的项目,好吧,您正在通过
addItem()
添加两个项目,因此您将得到两行。。。您的
ColorSwatch
类是否有返回颜色六进制代码的
toString()
方法?有关Oracle教程如何使用组合框(部分提供自定义渲染器)的基本解决方法,请参阅关于ListCellRenderOh!我知道!我将只发送图标颜色的十六进制值,并在ListCellRenderer中使用ColorSwatch和sended十六进制值创建JLabel。毕竟,要创建带有指定图标和文本的JLabel,我只需要图标颜色的十六进制值:D,但问题是是否可以动态添加对象/