如何在 JComboBox 列表中设置文本的不透明度?
How to set the Opacity of Text in a JComboBox list?
目前正在尝试生成一个带有字符串列表的组合框,并希望这些字符串值具有一定的不透明度。 ComboBox 本身应该保持正常,只有数据发生变化。
我可以通过实现我自己的自定义 Class 来使用我的图标组合框来完成此操作 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");
}
}
以上代码生成以下结果。
不能对字符串执行此操作,因为它是最终的 class,但即使我可以,也没有要覆盖的 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();
}
});
}
}
以上代码生成以下内容:
我需要什么才能在保持 ComboBox 本身正常的同时控制 ComboBox 中显示的字符串的不透明度?
control the opacity of the Strings displayed in the ComboBox while keeping the ComboBox itself normal?
使用自定义渲染器来检查渲染是针对下拉菜单还是
组合框:
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;
}
}
}
目前正在尝试生成一个带有字符串列表的组合框,并希望这些字符串值具有一定的不透明度。 ComboBox 本身应该保持正常,只有数据发生变化。
我可以通过实现我自己的自定义 Class 来使用我的图标组合框来完成此操作 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");
}
}
以上代码生成以下结果。
不能对字符串执行此操作,因为它是最终的 class,但即使我可以,也没有要覆盖的 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();
}
});
}
}
以上代码生成以下内容:
我需要什么才能在保持 ComboBox 本身正常的同时控制 ComboBox 中显示的字符串的不透明度?
control the opacity of the Strings displayed in the ComboBox while keeping the ComboBox itself normal?
使用自定义渲染器来检查渲染是针对下拉菜单还是 组合框:
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;
}
}
}