【问题标题】:Dynamic add items to JComboBox (value + icon = jlabel)向 JComboBox 动态添加项目(值 + 图标 = jlabel)
【发布时间】:2013-08-06 03:42:45
【问题描述】:

我使用 DefaultComboBoxModel 将特定项目添加到我的 JComboBox(字符串文本、图标图标)。但是出了点问题。当我将这两个项目添加到我的组合模型时,它看起来像这样:

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,但我不知道如何“告诉”它应该同时使用“value”和“icon”来渲染 JLabel 组件。有可能通过使用 JButton 动态添加这些项目对我来说非常重要。

【问题讨论】:

  • 动态添加项目到 JComboBox(已经可见)== MutableComboBoxModel
  • 为了更好的帮助,请尽快发布SSCCE,简短,可运行,可编译,f.i. Icon you can take from JOptionPane
  • 嗯,你通过addItem() 添加两个项目,所以你会得到两行......你的ColorSwatch 类是否有一个toString() 方法返回颜色十六进制代码?
  • 有关基本解决方法,请参阅 Oracle 教程如何使用组合框,提供自定义渲染器部分,关于 ListCellRenderer
  • 哦!我知道!我将只发送图标颜色的十六进制值,在 ListCellRenderer 中我将使用 ColorSwatch 创建 JLabel 并发送十六进制值。毕竟要创建带有指定图标和文本的 JLabel,我只需要图标颜色的十六进制值:D 但问题是是否可以动态添加对象? : /

标签: java swing jcombobox listcellrenderer


【解决方案1】:

我做到了,它现在可以正常工作了 :) 基本 1. 我使用了 DefaultComboBoxModel,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;
   }
}

使用这样的渲染器,我可以将项目添加到我的组合框...

try {

   String hex = jtextfield.getText();
   boolean canI = CheckHexValue(hex); //Method for checkin' if 'hex' String fits some specific terms

   if (canI) {

      combobox.insertItemAt(hex, 0);
      combobox.setSelectedIndex(0);
   }
} catch (Exception e) {
   JOptionPane.showMessageDialog(null, e);
}

...我们现在到家了。希望代码对某人有所帮助:)

【讨论】:

    猜你喜欢
    • 2011-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-17
    相关资源
    最近更新 更多