2013-01-15 44 views
5

有効になっている場合は緑の背景、無効にされている場合はグレーの背景(未完了)の矢印ボタンなしのJComboBoxが必要です。スイングでJComboBoxの背景を設定する

BasicComboBoxUIのソースコードをチェックしていくつかのメソッドをオーバーライドしようとしましたが、何も起こりませんでした。ドロップダウンは常に灰色/青色の背景を持ちます。

ここに私の最後の試みがあるSSCCEがあります。私は考えることができるすべてを試みた。私にヒントを与えてください、私は迷っています。私はこのスレッドをチェックしたいJComboBox背景色を変更するための

import java.awt.Color; 
import java.awt.Component; 
import java.awt.Graphics; 
import java.awt.Rectangle; 

import javax.swing.BorderFactory; 
import javax.swing.DefaultComboBoxModel; 
import javax.swing.DefaultListCellRenderer; 
import javax.swing.JButton; 
import javax.swing.JComboBox; 
import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.JList; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.SwingUtilities; 
import javax.swing.plaf.basic.BasicComboBoxUI; 

public class DropDownBackground 
{ 
    public static void main(final String[] args) 
    { 
     SwingUtilities.invokeLater(new Runnable() 
     { 

      public void run() 
      { 
       final JComboBox dropdown = new JComboBox(new DefaultComboBoxModel(new String[] { "one", "two", "three" })); 
       dropdown.setRenderer(new ComboBoxListCellRenderer()); 
       dropdown.setUI(new BasicComboBoxUI() 
       { 
        @Override 
        public void paint(final Graphics g, final JComponent c) 
        { 

         final Rectangle r = this.rectangleForCurrentValue(); 
         this.paintCurrentValueBackground(g, r, true); 
         this.paintCurrentValue(g, r, true); 

        } 

        @Override 
        public void paintCurrentValueBackground(final Graphics g, final Rectangle bounds, final boolean hasFocus) 
        { 
         final Color t = g.getColor(); 
         if (this.comboBox.isEnabled()) 
          g.setColor(Color.GREEN); 
         else 
          g.setColor(Color.GRAY); 
         g.fillRect(bounds.x, bounds.y, bounds.width, bounds.height); 
         g.setColor(t); 
        } 

        @Override 
        protected JButton createArrowButton() 
        { 
         return new JButton() 
         { 
          @Override 
          public int getWidth() 
          { 
           return 0; 
          } 
         }; 
        } 
       }); 
       dropdown.setBackground(Color.GREEN); 
       final JPanel p = new JPanel(); 
       p.add(dropdown); 

       final JFrame f = new JFrame(); 
       f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       f.getContentPane().add(new JScrollPane(p)); 
       f.setSize(800, 200); 
       f.setLocation(0, 0); 

       f.setVisible(true); 

      } 
     }); 

    } 

    public static class ComboBoxListCellRenderer extends DefaultListCellRenderer 
    { 
     /** 
     * 
     */ 
     private static final long serialVersionUID = 1L; 

     @Override 
     public Component getListCellRendererComponent(final JList list, final Object value, final int index, final boolean isSelected, final boolean cellHasFocus) 
     { 
      this.setToolTipText((String) value); 
      if (isSelected) 
      { 
       this.setBackground(Color.RED); 
       this.setForeground(Color.WHITE); 
      } 
      else 
      { 
       this.setBackground(Color.WHITE); 
       this.setForeground(Color.BLACK); 
      } 

      this.setText((String) value); 
      this.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); 

      return this; 
     } 
    } 

} 

答えて

0

http://www.coderanch.com/t/555124/GUI/java/JComboBox-background-colour-customer-renderer

EDIT: 正直に言うと、私はあなたのコードを読むのが面倒だけど、どのようにこの1について?あなたも、より多くの(無効の色をカスタマイズしたい場合は

UIManager.put("ComboBox.background", new ColorUIResource(UIManager.getColor("TextField.background"))); 
UIManager.put("ComboBox.foreground", new ColorUIResource(UIManager.getColor("TextField.foreground"))); 
UIManager.put("ComboBox.selectionBackground", new ColorUIResource(Color.GREEN)); 
UIManager.put("ComboBox.selectionForeground", new ColorUIResource(Color.WHITE)); 

http://www.coderanch.com/t/343835/GUI/java/custom-renderer-JComboBox

は、私は広い。この色のアプリケーションを持っていると思ったので、これが最善のアプローチでしたよりレンダラ指向:)

+0

あなたのリンクに続いて、JComboBox.setEditor()に関する情報が見つかりました。カスタムエディタを作成し、コンボボックスを編集可能に設定しました。私は編集不可能なエディタを使用し、編集可能なComboBoxを設定しなければならないので、これはうまくいきませんでした。それは今のところ素早い解決策ですが、さらなる助けに感謝します。 カスタムエディタについての情報は、http://www.coderanch.com/t/333756/GUI/java/creating-custom-ComboBoxEditor – haferblues

+0

で確認してください。 –

関連する問題