如何设置JComboBox列表中文本的不透明度?

huangapple go评论69阅读模式
英文:

How to set the Opacity of Text in a JComboBox list?

问题

目前正尝试生成一个带有字符串列表的ComboBox并希望这些字符串值具有一定的透明度ComboBox本身应保持正常只有数据更改

我通过实现自己的自定义类来实现了ComboBox图标的这一目标

    	///当图标在JComboBox中使用时,这个方法效果很好
    	class MyImageIconObject extends ImageIcon
    	{
    		float x;
    		ImageIcon ic;
    		public MyImageIconObject(String iconLocation)
    		{
    			super(iconLocation);
    			this.ic = new ImageIcon(iconLocation); 
    		}
    	
    		@Override
    		public void paintIcon(Component c, Graphics g, int x, int y)
    		{
    			//super.paintIcon(c, g, x, y);
    			((Graphics2D) g).setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.25f));
    			ic.paintIcon(c, g, x, y);
    			System.out.println("Painting 2");
    		}
    	}
由上述代码生成以下结果

[![enter image description here][1]][1]
对于字符串无法这样做因为它是一个Final类即使我可以这样做也没有paint()类型的函数可以重写

    import java.awt.AlphaComposite;
    import java.awt.BorderLayout;
    import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.util.ArrayList;
    
    import javax.swing.ImageIcon;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    
    public class JComboBoxWithStrings extends JFrame
    {
    	private JComboBox comboBox;
    	JPanel topPanel;
        String[] arrayOfStrings = {"String 1", "Entry 2", "More data", "Entry 10000"};
    	
    	public JComboBoxWithStrings ()
    	{
    		
    	}
    	
    	public static void main(String[] args)  
    	{
    
    		JComboBoxWithStrings T = new JComboBoxWithStrings();
    	    T.createGUI();
    	    T.setVisible(true);        
    	}
    	public void createGUI(){
    
    	    setMinimumSize(new Dimension(500,500));
    	    setTitle("Demo");
    	    setLocation(200, 200);
    
    	    topPanel = new JPanel();
    	    getContentPane().add(topPanel, BorderLayout.CENTER);
    
    	    comboBox = new JComboBox(arrayOfStrings);
    	    
    	    topPanel.add(comboBox);
    
    	    super.addWindowListener(new WindowAdapter() {           
    	        public void windowClosing(WindowEvent e) {              
    	            dispose();          
    	        }           
    	    }); 
    	}
    }
由上述代码生成以下结果

[![enter image description here][2]][2]
如何使我能够在保持ComboBox本身正常的同时控制ComboBox中显示的字符串的不透明度
英文:

Currently attempting to generate a ComboBox with a list of Strings and want those String values to have a certain opacity. The CombobBox itself should remain normal and only the data change.

I was able accomplish this with my ComboBox of icons by implementing my own custom Class.

	///This works great when ICons are used within the JComboBox
class MyImageIconObject extends ImageIcon
{
float x;
ImageIcon ic;
public MyImageIconObject(String iconLocation)
{
super(iconLocation);
this.ic = new ImageIcon(iconLocation); 
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y)
{
//super.paintIcon(c, g, x, y);
((Graphics2D) g).setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.25f));
ic.paintIcon(c, g, x, y);
System.out.println("Painting 2");
}
}

The above code generates the following results.

如何设置JComboBox列表中文本的不透明度?

Can't do this for Strings since it is a Final class, but even if I could there isn't a paint() type function to override.

import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class JComboBoxWithStrings extends JFrame
{
private JComboBox comboBox;
JPanel topPanel;
String[] arrayOfStrings = {"String 1", "Entry 2", "More data", "Entry 10000"};
public JComboBoxWithStrings ()
{
}
public static void main(String[] args)  
{
JComboBoxWithStrings T = new JComboBoxWithStrings();
T.createGUI();
T.setVisible(true);        
}
public void createGUI(){
setMinimumSize(new Dimension(500,500));
setTitle("Demo");
setLocation(200, 200);
topPanel = new JPanel();
getContentPane().add(topPanel, BorderLayout.CENTER);
comboBox = new JComboBox(arrayOfStrings);
topPanel.add(comboBox);
super.addWindowListener(new WindowAdapter() {           
public void windowClosing(WindowEvent e) {              
dispose();          
}           
}); 
}
}

The above code generates the following :

如何设置JComboBox列表中文本的不透明度?

What is required so that I am able to control the opacity of the Strings displayed in the ComboBox while keeping the ComboBox itself normal?

答案1

得分: 1

使用一个自定义的渲染器来控制在保持组合框本身正常的情况下检查下拉列表或组合框的字符串显示的透明度

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.plaf.basic.*;

public class ComboBoxTransparent extends JFrame {
    public ComboBoxTransparent() {
        Object[] items = { Color.red, Color.green, Color.blue };
        JComboBox comboBox = new JComboBox(items);
        comboBox.setRenderer(new TransparentRenderer());
        getContentPane().add(comboBox, BorderLayout.NORTH);
        add(new JTextField(), BorderLayout.SOUTH);
    }

    public static void main(String[] args) {
        ComboBoxTransparent frame = new ComboBoxTransparent();
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    class TransparentRenderer extends BasicComboBoxRenderer {
        private Color transparent = new Color(0, 0, 0, 128);

        public Component getListCellRendererComponent(
            JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);

            if (index == -1)
                setForeground(Color.BLACK);
            else
                setForeground(transparent);

            return this;
        }
    }
}
英文:

> control the opacity of the Strings displayed in the ComboBox while keeping the ComboBox itself normal?

Use a custom renderer that checks if the rendering is for the dropdown or the
combobox:

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.plaf.basic.*;
public class ComboBoxTransparent extends JFrame
{
public ComboBoxTransparent()
{
Object[] items = { Color.red, Color.green, Color.blue };
JComboBox comboBox = new JComboBox( items );
comboBox.setRenderer( new TransparentRenderer() );
getContentPane().add( comboBox, BorderLayout.NORTH );
add( new JTextField(), BorderLayout.SOUTH);
}
public static void main(String[] args)
{
ComboBoxTransparent frame = new ComboBoxTransparent();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible( true );
}
class TransparentRenderer extends BasicComboBoxRenderer
{
private Color transparent = new Color(0, 0, 0, 128);
public Component getListCellRendererComponent(
JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
{
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (index == -1)
setForeground( Color.BLACK );
else
setForeground( transparent );
return this;
}
}
}

huangapple
  • 本文由 发表于 2020年9月16日 23:49:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63923679.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定