swingset.ComboBoxDemo Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of swingset3-demos Show documentation
Show all versions of swingset3-demos Show documentation
Demonstrating the abilities of the Swing UI Toolkit swingset2 and swingx aka swingset3
The newest version!
/* Copyright (c) 2004 Sun Microsystems, Inc. All Rights Reserved.
Copyright notice, list of conditions and disclaimer see LICENSE file
*/
package swingset;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Hashtable;
import java.util.logging.Logger;
import javax.accessibility.AccessibleRelation;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import javax.swing.plaf.basic.BasicComboBoxRenderer;
import org.jdesktop.swingx.JXComboBox;
import org.jdesktop.swingx.JXFrame;
import org.jdesktop.swingx.JXFrame.StartPosition;
import org.jdesktop.swingx.JXLabel;
import org.jdesktop.swingx.JXPanel;
import org.jdesktop.swingx.decorator.HighlighterFactory;
import org.jdesktop.swingx.demos.svg.FeatheRuser;
import org.jdesktop.swingx.icon.SizingConstants;
/**
* JComboBox Demo
*
* @author Jeff Dinkins (inception)
* @author EUG https://github.com/homebeaver (reorg)
*/
public class ComboBoxDemo extends AbstractDemo implements ActionListener {
/**
* this is used in DemoAction to build the demo toolbar
*/
public static final String ICON_PATH = "toolbar/JComboBox.gif";
private static final long serialVersionUID = 6157959394784801204L;
private static final Logger LOG = Logger.getLogger(ComboBoxDemo.class.getName());
Face face;
JXLabel xfaceLabel;
/**
* main method allows us to run as a standalone demo.
* @param args params
*/
public static void main(String[] args) {
SwingUtilities.invokeLater( () -> {
JXFrame controller = new JXFrame("controller", exitOnClose);
AbstractDemo demo = new ComboBoxDemo(controller);
JXFrame frame = new JXFrame("demo", exitOnClose);
frame.setStartPosition(StartPosition.CenterInScreen);
//frame.setLocationRelativeTo(controller);
frame.getContentPane().add(demo);
frame.pack();
frame.setVisible(true);
controller.getContentPane().add(demo.getControlPane());
controller.pack();
controller.setVisible(true);
});
}
/**
* A subclass of BasicComboBoxRenderer to override the renderer property
* - border of the Label : EmptyNoFocusBorder,
* of the List : EtchedBorder
*/
@SuppressWarnings("serial")
class MyComboBoxRenderer extends BasicComboBoxRenderer {
static Border etchedNoFocusBorder = BorderFactory.createEtchedBorder();
public MyComboBoxRenderer() {
super();
// Alternativ via IconHighlighter
// setIcon(FeatheRuser.of(SizingConstants.SMALL_ICON, SizingConstants.SMALL_ICON));
}
Border getEmptyNoFocusBorder() {
return BasicComboBoxRenderer.noFocusBorder;
}
@Override
public Component getListCellRendererComponent(JList> list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
// index -1 when called in BasicComboBoxUI.getDisplaySize()
// >=0 when called in BasicListUI.updateLayoutState()
setBorder(index == -1 ? getEmptyNoFocusBorder() : etchedNoFocusBorder);
if (isSelected) {
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
} else {
setBackground(list.getBackground());
setForeground(list.getForeground());
}
setFont(list.getFont());
if (value instanceof Icon) {
setIcon((Icon) value);
} else {
setText((value == null) ? "" : value.toString());
}
return this;
}
}
/**
* ComboBoxDemo Constructor
*
* @param controllerFrame controller Frame
*/
public ComboBoxDemo(Frame controllerFrame) {
super(new BorderLayout());
super.setPreferredSize(PREFERRED_SIZE);
super.setBorder(new BevelBorder(BevelBorder.LOWERED));
controllerFrame.setTitle(getBundleString("name"));
face = new Face();
xfaceLabel = new JXLabel(face);
super.add(xfaceLabel, BorderLayout.CENTER);
getComboPane(); // creates ComboBoxes hairCB, ... and presetCB
JXPanel presetsPanel = new JXPanel();
presetsPanel.setLayout(new BoxLayout(presetsPanel, BoxLayout.Y_AXIS));
JXLabel l = new JXLabel(getBundleString("presets"));
l.setAlignmentX(JXLabel.LEFT_ALIGNMENT);
presetsPanel.add(l);
presetsPanel.add(presetCB);
presetsPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
presetCB.setRenderer(new MyComboBoxRenderer());
presetCB.setComboBoxIcon(FeatheRuser.of(SizingConstants.SMALL_ICON, SizingConstants.SMALL_ICON));
l.setLabelFor(presetCB);
super.add(presetsPanel, BorderLayout.NORTH);
// to hold hairCB, eyesCB, mouthCB
@SuppressWarnings("serial")
JXPanel sidePanel = new JXPanel() {
public Dimension getMaximumSize() {
return new Dimension(getPreferredSize().width, super.getMaximumSize().height);
}
public Dimension getPreferredSize() {
return new Dimension(PREFERRED_SIZE.width/3, super.getMaximumSize().height);
}
};
sidePanel.setLayout(new BoxLayout(sidePanel, BoxLayout.Y_AXIS));
l = new JXLabel(getBundleString("hair_description"));
l.setAlignmentX(JXLabel.LEFT_ALIGNMENT);
sidePanel.add(l);
hairCB.setAlignmentX(JXComboBox.LEFT_ALIGNMENT);
hairCB.addHighlighter(HighlighterFactory.createSimpleStriping(HighlighterFactory.NOTEPAD));
sidePanel.add(hairCB);
l.setLabelFor(hairCB);
sidePanel.add(Box.createRigidArea(VGAP15));
l = new JXLabel(getBundleString("eyes_description"));
l.setAlignmentX(JXLabel.LEFT_ALIGNMENT);
sidePanel.add(l);
eyesCB.setAlignmentX(JXComboBox.LEFT_ALIGNMENT);
eyesCB.addHighlighter(HighlighterFactory.createSimpleStriping(HighlighterFactory.FLORAL_WHITE));
sidePanel.add(eyesCB);
l.setLabelFor(eyesCB);
sidePanel.add(Box.createRigidArea(VGAP15));
l = new JXLabel(getBundleString("mouth_description"));
l.setAlignmentX(JXLabel.LEFT_ALIGNMENT);
sidePanel.add(l);
mouthCB.setAlignmentX(JXComboBox.LEFT_ALIGNMENT);
mouthCB.addHighlighter(HighlighterFactory.createSimpleStriping(HighlighterFactory.LINE_PRINTER));
sidePanel.add(mouthCB);
l.setLabelFor(mouthCB);
sidePanel.add(Box.createRigidArea(VGAP15));
// Fill up the remaining space
sidePanel.add(new JPanel(new BorderLayout()));
sidePanel.setBorder(BorderFactory.createEmptyBorder(20, 0, 20, 20));
super.add(sidePanel, BorderLayout.EAST);
getControlPane();
}
JXPanel comboBoxPanel = null;
JXComboBox hairCB;
JXComboBox eyesCB;
JXComboBox mouthCB;
JXComboBox presetCB;
// @SuppressWarnings("rawtypes") // parts can be or reverse or
private Hashtable
© 2015 - 2025 Weber Informatics LLC | Privacy Policy