JComboBox自定义滚动条和去掉默认背景

Posted 小菜鸟yjm

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JComboBox自定义滚动条和去掉默认背景相关的知识,希望对你有一定的参考价值。

JComboBox在选择之后整个方框都会变成灰色,这一点在开发自己的界面,完全不能忍。

下面贴一下我在另外一个博客看到的解决办法,隐藏得比较深,希望能给以后遇到类似的人一些指导。传送门 点击打开链接


解决的思路就是:拿到JcomboBox的editor,然后改变JTextField的背景颜色。

private void disableFocusBackground(javax.swing.JComboBox combo)
	
		if (combo == null)
		
			return;
		

		java.awt.Component comp = combo.getEditor().getEditorComponent();

		if (comp instanceof javax.swing.JTextField)
		
			javax.swing.JTextField field = (javax.swing.JTextField) comp;

			field.setEditable(false);

			field.setSelectionColor(field.getBackground()/* java.awt.Color.WHITE */);

			combo.setEditable(true);
		
	

传入JComboBox即可。

下面简单介绍下怎么替换JComboBox丑陋的滑动条

新建一个类继承BasicScrollBarUI

package net.java.sip.communicator.impl.gui.message.yjm;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JScrollBar;
import javax.swing.plaf.basic.BasicArrowButton;
import javax.swing.plaf.basic.BasicScrollBarUI;

public class CBScrollBarUI extends BasicScrollBarUI

	public Dimension getPreferredSize(JComponent c)
	
		return new Dimension(16, 16);
	

	// 重绘滚动条的滑块
	public void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds)
	
		super.paintThumb(g, c, thumbBounds);
		int tw = thumbBounds.width;
		int th = thumbBounds.height;
		// 重定图形上下文的原点,这句一定要写,不然会出现拖动滑块时滑块不动的现象
		g.translate(thumbBounds.x, thumbBounds.y);
		Graphics2D g2 = (Graphics2D) g;
		GradientPaint gp = null;
		gp = new GradientPaint(0, 0, new Color(124, 124, 124), tw, 0,
				new Color(124, 124, 124));
		g2.setPaint(gp);
		g2.fillRoundRect(0, 0, tw - 1, th - 1, 5, 5);
		g2.setColor(new Color(124, 124, 124));
		g2.drawRoundRect(0, 0, tw - 1, th - 1, 5, 5);
	

	// 重绘滑块的滑动区域背景
	public void paintTrack(Graphics g, JComponent c, Rectangle trackBounds)
	
		Graphics2D g2 = (Graphics2D) g;
		GradientPaint gp = null;
		gp = new GradientPaint(0, 0, new Color(196, 195, 194),
				trackBounds.width, 0, new Color(196, 195, 194));
		g2.setPaint(gp);
		g2.fillRect(trackBounds.x, trackBounds.y, trackBounds.width,
				trackBounds.height);
		g2.setColor(new Color(196, 195, 194));
		g2.drawRect(trackBounds.x, trackBounds.y, trackBounds.width - 1,
				trackBounds.height - 1);
		if (trackHighlight == BasicScrollBarUI.DECREASE_HIGHLIGHT)
			this.paintDecreaseHighlight(g);
		if (trackHighlight == BasicScrollBarUI.INCREASE_HIGHLIGHT)
			this.paintIncreaseHighlight(g);
	

	// 重绘当鼠标点击滑动到向上或向左按钮之间的区域
	protected void paintDecreaseHighlight(Graphics g)
	
		g.setColor(Color.green);
		int x = this.getTrackBounds().x;
		int y = this.getTrackBounds().y;
		int w = 0, h = 0;
		if (this.scrollbar.getOrientation() == JScrollBar.VERTICAL)
		
			w = this.getThumbBounds().width;
			h = this.getThumbBounds().y - y;

		
		if (this.scrollbar.getOrientation() == JScrollBar.HORIZONTAL)
		
			w = this.getThumbBounds().x - x;
			h = this.getThumbBounds().height;
		
		g.fillRect(x, y, w, h);
	

	// 重绘当鼠标点击滑块至向下或向右按钮之间的区域
	protected void paintIncreaseHighlight(Graphics g)
	
		g.setColor(Color.blue);
		int x = this.getThumbBounds().x;
		int y = this.getThumbBounds().y;
		int w = this.getTrackBounds().width;
		int h = this.getTrackBounds().height;
		g.fillRect(x, y, w, h);
	

	protected JButton createIncreaseButton(int orientation)
	
		return new BasicArrowButton(orientation)
		
			// 重绘按钮的三角标记
			public void paintTriangle(Graphics g, int x, int y, int size,
					int direction, boolean isEnabled)
			
				FileInputStream is;
				BufferedImage img = null;
				try
				
					is = new FileInputStream("resources/images/message/群发/上三角常态.png");
					img=ImageIO.read(is);
				 catch (Exception e)
				
					// TODO Auto-generated catch block
					e.printStackTrace();
				 
				 
				Graphics2D g2 = (Graphics2D) g;
				GradientPaint gp = null;
				switch (this.getDirection())
				
				case BasicArrowButton.SOUTH:
					gp = new GradientPaint(0, 0,Color.white,
							getWidth(), 0,Color.white);
					break;
				case BasicArrowButton.EAST:
					gp = new GradientPaint(0, 0, Color.white, 0,
							getHeight(), Color.white);
					break;
				
				g2.setPaint(gp);
				g2.fillRect(0, 0, getWidth(), getHeight());
				g2.setColor(Color.white);
				g2.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
				g2.drawImage(img, (getWidth() - 2) / 2 - 5,
						(getHeight() - 2) / 2 - 5, 13, 13, null);
			
		;
	

	protected JButton createDecreaseButton(int orientation)
	
		return new BasicArrowButton(orientation)
		
			public void paintTriangle(Graphics g, int x, int y, int size,
					int direction, boolean isEnabled)
			
				FileInputStream is;
				BufferedImage img = null;
				try
				
					is = new FileInputStream("resources/images/message/群发/下三角常态.png");
					img=ImageIO.read(is);
				 catch (Exception e)
				
					// TODO Auto-generated catch block
					e.printStackTrace();
				 
				Graphics2D g2 = (Graphics2D) g;
				GradientPaint gp = null;
				switch (this.getDirection())
				
				case BasicArrowButton.NORTH:
					gp = new GradientPaint(0, 0,Color.white,
							getWidth(), 0, Color.white);
					break;
				case BasicArrowButton.WEST:
					gp = new GradientPaint(0, 0,Color.white, 0,
							getHeight(),Color.white);
					break;
				
				g2.setPaint(gp);
				g2.fillRect(0, 0, getWidth(), getHeight());
				g2.setColor(Color.white);
				g2.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
				g2.drawImage(img, (getWidth() - 2) / 2 - 5,
						(getHeight() - 2) / 2 - 5, 13, 13, null);
			
		;
	


到这里只能算完成了最重要的三分之二的任务。在网上找了资料,的确看到有人这么做,继承BasicScrollBarUI。但是困惑我好久的是怎么把这个类给传到JCombBox中去。下面简单介绍下应用这个“继承者”的方法。

新建一个类

MyComboBoxUI extends BasicComboBoxUI

public class MyComboBoxUI extends BasicComboBoxUI

	public static ComponentUI createUI()
	
		return new MyComboBoxUI();
	

	@Override
<span style="white-space:pre">	</span>//替换默认的箭头
	protected JButton createArrowButton()
	
		// JButton button = new BasicArrowButton(BasicArrowButton.EAST);
		CommonButton button = new CommonButton(
				"resources/images/message/群发/下拉箭头常态.png",
				"resources/images/message/群发/下拉箭头掠过.png",
				"resources/images/message/群发/下拉箭头掠过.png");
		return button;
	
<span style="white-space:pre">	</span>//自定义点击右边小箭头之后的下拉框,这个方法被我忽略了很久。。。
	@Override
	protected ComboPopup createPopup()
	
		
		ComboPopup popup = new BasicComboPopup(comboBox)
		
			protected JScrollPane createScroller()
			
<span style="white-space:pre">				</span>//新建一个<span style="font-family: Arial, Helvetica, sans-serif;">JScrollPane,禁止横向滑动</span>
				JScrollPane sp = new JScrollPane(list,
						ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
						ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
				//sp.getVerticalScrollBar().setUI(new MyScrollBarUI());
<span style="white-space:pre">				</span>//传入上面的继承BasicScrollBarUI的类
				sp.getVerticalScrollBar().setUI(new CBScrollBarUI());
				return sp;
			
		;
		return popup;
	

最后一步就是传入MyComboBoxUI

buildingBox.setUI((ComboBoxUI) MyComboBoxUI.createUI());

好久都没有写博客了,因为在网上找这方面有点难找,贴出来希望能给大家有点帮助。



以上是关于JComboBox自定义滚动条和去掉默认背景的主要内容,如果未能解决你的问题,请参考以下文章

连接自定义滚动条和 Flickable

如何在 MS Word 文档中显示代码片段,因为它在 *** 中显示(滚动条和灰色背景)

CSS滚动条

去掉iframe默认滚动条后影响正常滚动以及js解决高度自适应。

加载 twitter 引导模式对话框时,如何防止正文滚动条和移位?

安卓listview的item点击时候的背景颜色怎么去掉或者设置成透明的呢?