Olá, através de muitos exemplos na internet eu elaborei esta classe:
`
package Pacote;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.geom.Area;
import java.awt.geom.Path2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.util.ArrayList;
import java.util.Objects;
import javax.accessibility.Accessible;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;
import javax.swing.JWindow;
import javax.swing.ListCellRenderer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.AbstractBorder;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.basic.BasicComboBoxUI;
import javax.swing.plaf.basic.BasicComboPopup;
public class Teste {
private static final Color BACKGROUND = Color.GREEN;
private static final Color FOREGROUND = Color.BLUE;
private static final Color SELECTIONFOREGROUND = Color.RED;
public Teste() {
UIManager.put("ComboBox.border", new borda());
UIManager.put("ComboBox.foreground", FOREGROUND);
UIManager.put("ComboBox.background", BACKGROUND);
UIManager.put("ComboBox.selectionForeground", SELECTIONFOREGROUND);
UIManager.put("ComboBox.selectionBackground", BACKGROUND);
UIManager.put("ComboBox.buttonDarkShadow", BACKGROUND);
UIManager.put("ComboBox.buttonBackground", FOREGROUND);
UIManager.put("ComboBox.buttonHighlight", FOREGROUND);
UIManager.put("ComboBox.buttonShadow", FOREGROUND);
UIManager.put("ComboBox.font",new Font("Comic Sans MS",Font.BOLD,30));
JComboBox<String> combo1 = new JComboBox<>();
combo1.addItem("Thallysson");
combo1.addItem("Kein");
combo1.addItem("Flor");
combo1.setUI(new BasicComboBoxUI());
Object o = combo1.getAccessibleContext().getAccessibleChild(0);
if (o instanceof JComponent) {
JComponent c = (JComponent) o;
c.setBorder(new RoundedCornerBorder());
c.setFont(new Font("Comic Sans MS",Font.BOLD,30));
c.setForeground(FOREGROUND);
c.setBackground(BACKGROUND);
}
combo1.addPopupMenuListener(new HeavyWeightContainerListener());
JFrame j = new JFrame("Combo");
j.setLayout(new FlowLayout());
String[] s = {"Thallysson","Kein","Flor"};
j.add(combo1);
j.setSize(500,500);
j.setVisible(true);
j.setLocationRelativeTo(null);
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
Teste s = new Teste();
}
}
class borda extends AbstractBorder {
@Override public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int r = 12;
int w = width - 1;
int h = height - 1;
Area round = new Area(new RoundRectangle2D.Double(x, y, w, h, r, r));
Container parent = c.getParent();
if (Objects.nonNull(parent)) {
g2.setPaint(parent.getBackground());
Area corner = new Area(new Rectangle2D.Double(x, y, width, height));
corner.subtract(round);
g2.fill(corner);
}
g2.setPaint(c.getForeground());
g2.draw(round);
g2.dispose();
}
@Override public Insets getBorderInsets(Component c) {
return new Insets(4, 8, 4, 8);
}
@Override public Insets getBorderInsets(Component c, Insets insets) {
insets.set(4, 8, 4, 8);
return insets;
}
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
boolean cellHasFocus) {
return null;
}
}
class HeavyWeightContainerListener implements PopupMenuListener {
@Override public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
EventQueue.invokeLater(new Runnable() {
@Override public void run() {
JComboBox combo = (JComboBox) e.getSource();
Accessible a = combo.getUI().getAccessibleChild(combo, 0);
if (a instanceof BasicComboPopup) {
BasicComboPopup pop = (BasicComboPopup) a;
Container top = pop.getTopLevelAncestor();
if (top instanceof JWindow) {
//http://ateraimemo.com/Swing/DropShadowPopup.html
System.out.println("HeavyWeightContainer");
((JWindow) top).setBackground(new Color(0x0, true));
}
}
}
});
}
@Override public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {}
@Override public void popupMenuCanceled(PopupMenuEvent e) {}
}
class RoundedCornerBorder extends AbstractBorder {
protected static final int ARC = 12;
@Override public void paintBorder(
Component c, Graphics g, int x, int y, int width, int height) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
int r = ARC;
int w = width - 1;
int h = height - 1;
Area round = new Area(new RoundRectangle2D.Float(x, y, w, h, r, r));
if (c instanceof JPopupMenu) {
g2.setPaint(c.getBackground());
g2.fill(round);
} else {
Container parent = c.getParent();
if (Objects.nonNull(parent)) {
g2.setPaint(parent.getBackground());
Area corner = new Area(new Rectangle2D.Float(x, y, width, height));
corner.subtract(round);
g2.fill(corner);
}
}
g2.setPaint(c.getForeground());
g2.draw(round);
g2.dispose();
}
@Override public Insets getBorderInsets(Component c) {
return new Insets(4, 8, 4, 8);
}
@Override public Insets getBorderInsets(Component c, Insets insets) {
insets.set(4, 8, 4, 8);
return insets;
}
}
class RoundedCornerBorder1 extends RoundedCornerBorder {
//http://ateraimemo.com/Swing/RoundedComboBox.html
@Override public void paintBorder(
Component c, Graphics g, int x, int y, int width, int height) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
int r = ARC;
int w = width - 1;
int h = height - 1;
Area round = new Area(new RoundRectangle2D.Float(x, y, w, h, r, r));
Rectangle b = round.getBounds();
b.setBounds(b.x, b.y + r, b.width, b.height - r);
round.add(new Area(b));
Container parent = c.getParent();
if (Objects.nonNull(parent)) {
g2.setPaint(parent.getBackground());
Area corner = new Area(new Rectangle2D.Float(x, y, width, height));
corner.subtract(round);
g2.fill(corner);
}
g2.setPaint(c.getForeground());
g2.draw(round);
g2.dispose();
}
}
class RoundedCornerBorder2 extends RoundedCornerBorder {
@Override public void paintBorder(
Component c, Graphics g, int x, int y, int width, int height) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
int r = ARC;
int w = width - 1;
int h = height - 1;
Path2D.Float p = new Path2D.Float();
p.moveTo(x, y);
p.lineTo(x, y + h - r);
p.quadTo(x, y + h, x + r, y + h);
p.lineTo(x + w - r, y + h);
p.quadTo(x + w, y + h, x + w, y + h - r);
p.lineTo(x + w, y);
p.closePath();
Area round = new Area(p);
g2.setPaint(c.getBackground());
g2.fill(round);
g2.setPaint(c.getForeground());
g2.draw(round);
g2.setPaint(c.getBackground());
g2.drawLine(x + 1, y, x + width - 2, y);
g2.dispose();
}
}
`
E o meu JComboBox ficou assim:
Porém eu queria retirar do JComboBox esse negócio azul e deixar somente a seta da cor preta, então eu mudei essa parte:
’
UIManager.put("ComboBox.buttonDarkShadow", BACKGROUND);
UIManager.put("ComboBox.buttonBackground", FOREGROUND);
UIManager.put("ComboBox.buttonHighlight", FOREGROUND);
UIManager.put("ComboBox.buttonShadow", FOREGROUND);'
E deixei assim:
’
UIManager.put("ComboBox.buttonDarkShadow", Color.BLACK);
UIManager.put("ComboBox.buttonBackground", BACKGROUND);
’
Porém ficou com essa borda:
É possível retirar essa borda ou fazer de outra maneira que fique como eu quero?