2

次のコードを見てください

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

public class JCombo extends JFrame
{
    JComboBox com1;

    public JCombo()
    {


        com1 = new JComboBox();

        com1.addItem("Select");
        com1.addItem("One");
        com1.addItem("two");
        com1.addItem("Three");

        com1.addItemListener(new Com1Action());

        this.setLayout(new FlowLayout());
        this.add(com1);

        this.pack();
        this.validate();
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private class Com1Action implements ItemListener
    {
        public void itemStateChanged(ItemEvent ae)
        {
            if(ae.getStateChange() == ItemEvent.SELECTED)
            {
                com1.getSelectedItem();
            }
        }
    }

    public static void main(String[]args)
    {
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            new JCombo();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

    }
}

ここでは、アイテムが選択されたときに、選択したアイテムに色を適用したいと考えています。どうやってやるの?

元:

User selects "One" - Now "One" changes to blue
  User selects "Two" - Now "Two" changes to blue. "One" is also blue as well, because we changed the colour at the first place
    User selected "Three" - Now "Three" changes to blue. "One" and "Two" remains blue as well

アップデート

これをカスタムレンダラーで再コーディングしました。選択した項目が強調表示されますが、マウスを動かすとすぐに色が元の状態に戻ります。言い換えれば、ここで起こった唯一のことはハイライトの色を変更することであり、選択したアイテムに永続的に色を適用することではありません

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

public class JCombo extends JFrame
{
    JComboBox com1;

    public JCombo()
    {


        com1 = new JComboBox();
        com1.setLightWeightPopupEnabled(true);

        com1.addItem("One");
        com1.addItem("two");
        com1.addItem("Three");

        com1.setRenderer(new MyCellRenderer());

        com1.addItemListener(new Com1Action());

        this.setLayout(new FlowLayout());
        this.add(com1);

        this.pack();
        this.validate();
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private class Com1Action implements ItemListener
    {
        public void itemStateChanged(ItemEvent ae)
        {
            if(ae.getStateChange() == ItemEvent.SELECTED)
            {
                com1.getSelectedItem();

            }
        }
    }

   class MyCellRenderer extends JLabel implements ListCellRenderer<Object> 
   {
     public MyCellRenderer() 
     {
         setOpaque(true);
     }

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

         setText(value.toString());

         Color background = Color.white;
         Color foreground = Color.black;

         // check if this cell represents the current DnD drop location
         JList.DropLocation dropLocation = list.getDropLocation();

         if (dropLocation != null
                 && !dropLocation.isInsert()
                 && dropLocation.getIndex() == index) {



         // check if this cell is selected
         } else if (isSelected) {
             background = Color.RED;
             foreground = Color.WHITE;

         // unselected, and not the DnD drop location
         } else {
         };

         setBackground(background);
         setForeground(foreground);

         return this;
     }
 }


    public static void main(String[]args)
    {
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            new JCombo();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

    }
}
4

4 に答える 4

4

これには、カスタム レンダラーの提供が必要です。API にはが含まれています。

補遺:選択したアイテムに永続的に色を適用するのではなく、強調表示の色を変更するだけです。

何かを永続的に変更するということは、その情報を保存する場所を持つことを意味します。2 つの選択肢があります。

  • state選択したフィールドにフィールドを追加し、ComboBoxModelそれを使用してレンダラーの背景色を調整します。を使用して、レンダラー内のモデルにアクセスできますlist.getModel()

  • @mKorbelがここで提案しているように、複数の選択を可能にするJListorに切り替えてJTable使用します。ListSelectionModel

于 2012-12-02T17:31:50.383 に答える
2

選択した項目の前景色のみを変更する場合は、以下のコードを使用します

コンボボックス.getEditor().getEditorComponent().setForeground(Color.GREEN);

選択したアイテムを含むすべてのアイテムの前景色を変更する場合は、上記のコードと下のコードも使用します。

   combobox.setRenderer(new DefaultListCellRenderer() {
@Override
public void paint(Graphics g) {
    setBackground(Color.WHITE);
    setForeground(Color.GREEN);
    super.paint(g);
}

});

于 2015-05-13T06:46:07.803 に答える
0

方法は試しました.setBackground(Color c)か?

Java の良いところは、ドキュメントが非常に充実していることです。

ここで JComponent のドキュメントを参照してください: JComponent Docs

于 2012-12-02T17:21:25.763 に答える