簡體   English   中英

動態將項目添加到JComboBox(值+圖標= jlabel)

[英]Dynamic add items to JComboBox (value + icon = jlabel)

我使用DefaultComboBoxModel將特定項添加到我的JComboBox(字符串文本,圖標圖標)。 但是出了點問題。 當我將這兩項添加到組合模型時,它看起來像這樣:

ComboBoxWindow: 

              [icon          ]

              [icon     value]

總之,我的combobox代碼如下所示:

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.我使用了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);
}

...現在我們到家了。 希望代碼可以幫助某人:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM