package Custom;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.ComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import javax.swing.plaf.basic.BasicComboBoxRenderer;
import javax.swing.plaf.basic.BasicComboPopup;
import javax.swing.plaf.basic.ComboPopup;
import javax.swing.plaf.metal.MetalComboBoxUI;
public class ControlableComboBox extends JComboBox {
protected int popupWidth;
protected boolean orientation;
private ControlableComboBoxUI controlableComboBoxUI;
public ControlableComboBox(ComboBoxModel aModel) {
super(aModel);
this.controlableComboBoxUI = new ControlableComboBoxUI();
setUI(this.controlableComboBoxUI);
popupWidth = 0;
}
public ControlableComboBox(Object[] items) {
super(items);
this.controlableComboBoxUI = new ControlableComboBoxUI();
setUI(this.controlableComboBoxUI);
popupWidth = 0;
}
public ControlableComboBox(Vector items) {
super(items);
this.controlableComboBoxUI = new ControlableComboBoxUI();
setUI(this.controlableComboBoxUI);
popupWidth = 0;
}
public void setPopupWidth(int width) {
popupWidth = width;
}
public Dimension getPopupSize() {
Dimension size = getSize();
if (popupWidth < 1) {
popupWidth = size.width;
}
return new Dimension(popupWidth, size.height);
}
public boolean isOrientation() {
return orientation;
}
public void setOrientation(boolean orientation) {
this.orientation = orientation;
}
private Color buttonBackgroundColor;
public Color getButtonBackground() {
return buttonBackgroundColor;
}
public void setButtonBackground(Color backgroundColor) {
this.buttonBackgroundColor = backgroundColor;
this.controlableComboBoxUI.setButtonBackground(backgroundColor);
}
public class ControlableComboBoxUI extends MetalComboBoxUI {
public void setButtonBackground(Color color){
if (buttonBackgroundColor!=null){
Dimension dimension = this.getDisplaySize();
BufferedImage brighterImage1 = new BufferedImage(dimension.height, dimension.height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = brighterImage1.createGraphics();
graphics.setColor(buttonBackgroundColor);
graphics.fillRect(0, 0, dimension.height, dimension.height);
graphics.drawImage(brighterImage1, 0, 0, null);
arrowButton.setIcon(new ImageIcon(brighterImage1));
BufferedImage brighterImage2 = new BufferedImage(dimension.height, dimension.height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics1 = brighterImage2.createGraphics();
graphics1.setColor(buttonBackgroundColor.darker());
graphics1.fillRect(0, 0, dimension.height, dimension.height);
graphics1.drawImage(brighterImage2, 0, 0, null);
arrowButton.setPressedIcon(new ImageIcon(brighterImage2));
}
}
protected JButton createArrowButton(){
arrowButton = super.createArrowButton();
arrowButton.setMargin(new Insets(0, 0, 0, 0));
arrowButton.setBorderPainted(false);
return arrowButton;
}
public void paintCurrentValue(Graphics g, Rectangle bounds, boolean hasFocus) {
bounds.x -= 2;
bounds.width += 3;
if (arrowButton != null) {
Insets buttonInsets = arrowButton.getInsets();
bounds.y -= buttonInsets.top;
bounds.height += (buttonInsets.top + buttonInsets.bottom);
}else {
bounds.x += 2;
bounds.width -= 4;
}
if (bounds.width==0){
bounds.width = 1;
}
if (bounds.height==0){
bounds.height = 1;
}
super.paintCurrentValue(g, bounds, hasFocus);
}
protected ComboPopup createPopup() {
BasicComboPopup basicComboPopup = new BasicComboPopup(comboBox) {
public void show() {
Dimension popupSize = ((ControlableComboBox) comboBox).getPopupSize();
popupSize.setSize(popupSize.width, getPopupHeightForRowCount(comboBox.getMaximumRowCount()));
Rectangle popupBounds = computePopupBounds(0, comboBox.getBounds().height, popupSize.width, popupSize.height);
scroller.setPreferredSize(popupBounds.getSize());
// scroller.setMaximumSize(popupBounds.getSize());
// scroller.setMinimumSize(popupBounds.getSize());
// list.invalidate();
// int selectedIndex = comboBox.getSelectedIndex();
// if (selectedIndex == -1) {
// list.clearSelection();
// } else {
// list.setSelectedIndex(selectedIndex);
// }
// list.ensureIndexIsVisible(list.getSelectedIndex());
// setLightWeightPopupEnabled(comboBox.isLightWeightPopupEnabled());
int x = 0;
if (comboBox instanceof ControlableComboBox){
ControlableComboBox box = (ControlableComboBox) comboBox;
if (box.orientation){
// x += 1;
}else{
x = popupSize.width-comboBox.getWidth();
}
}
int y = popupBounds.y;
if (y<0){
y-=2;
}
show(comboBox, popupBounds.x-x-1, y);
}
};
basicComboPopup.getAccessibleContext().setAccessibleParent(comboBox);
return basicComboPopup;
}
}
public static void main(String[] args){
JDialog dialog = new JDialog();
ControlableComboBox comboBox = new ControlableComboBox(new String[]{"fewfwe", "43r43r43"});
comboBox.setBorder(BorderFactory.createEmptyBorder());
comboBox.setPopupWidth(200);
comboBox.setPreferredSize(new Dimension(21, 23));
comboBox.setOpaque(false);
// comboBox.setButtonBackground(Color.RED);
BasicComboBoxRenderer defaultListCellRenderer = new BasicComboBoxRenderer(){
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
list.setOpaque(false);
return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
}};
comboBox.setRenderer(defaultListCellRenderer);
comboBox.addPopupMenuListener(new PopupMenuListener(){
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
JComboBox comboBox = (JComboBox) e.getSource();
if (comboBox.getSelectedIndex()==1){
System.out.println(comboBox.getSelectedItem().toString());
}
}
public void popupMenuCanceled(PopupMenuEvent e) {}
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {}
});
dialog.add(comboBox);
dialog.setSize(50, 50);
dialog.setLocation(150, 150);
dialog.setVisible(true);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
}
}