如何解决如何在JComboBox列表中设置文本的不透明度?
当前正在尝试生成带有字符串列表的ComboBox,并希望这些String值具有一定的不透明度。 CombobBox本身应该保持正常,只有数据改变。
我可以通过实现自己的自定义类来使用图标的ComboBox完成此操作。
///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,y);
System.out.println("Painting 2");
}
}
上面的代码生成以下结果。
因为它是Final类,所以无法对Strings进行此操作,但是即使我无法使用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本身正常,需要做什么?
解决方法
控制ComboBox中显示的字符串的不透明度,同时保持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,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;
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。