Skip to content

Instantly share code, notes, and snippets.

@resarahadian
Created January 31, 2014 00:29
Show Gist options
  • Save resarahadian/8723117 to your computer and use it in GitHub Desktop.
Save resarahadian/8723117 to your computer and use it in GitHub Desktop.
Separator pada JComboBox
import java.awt.EventQueue;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
public class frameComboSeparator extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
@SuppressWarnings("rawtypes")
private JComboBox comboBox;
private JLabel lblWall;
private JLabel lblIcon;
/**
* Create the frame.
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public frameComboSeparator()
{
setTitle("ComboBox Separator");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 593, 394);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
Vector v = new Vector();
v.addElement("Nasi Goreng");
v.addElement("Mie Goreng");
v.addElement("Pecel Lele");
v.addElement("Gudeg");
v.addElement(new JSeparator());
v.addElement("Es Teh Manis");
v.addElement("Es Jeruk");
v.addElement("Susu Coklat");
v.addElement(new JSeparator());
v.addElement("Ice Cream");
comboBox = new SeparatorComboBox(v);
comboBox.setBounds(86, 128, 220, 33);
contentPane.add(comboBox);
lblIcon = new JLabel("");
lblIcon.setIcon(new ImageIcon(frameComboSeparator.class.getResource("/comboBoxSeparator/pencil.png")));
lblIcon.setBounds(407, 68, 152, 141);
contentPane.add(lblIcon);
lblWall = new JLabel("");
lblWall.setIcon(new ImageIcon(frameComboSeparator.class.getResource("/comboBoxSeparator/wall.jpg")));
lblWall.setBounds(0, -12, 591, 374);
contentPane.add(lblWall);
setLocationRelativeTo(null);
}
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
//Nimbus Look and Feel
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
//GTK Look and Feel
/*for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
{
if ("com.sun.java.swing.plaf.gtk.GTKLookAndFeel".equals(info.getClassName()))
{
try
{
javax.swing.UIManager.setLookAndFeel(info.getClassName());
}
catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (InstantiationException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IllegalAccessException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (UnsupportedLookAndFeelException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
}*/
frameComboSeparator frame = new frameComboSeparator();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.plaf.basic.*;
@SuppressWarnings({ "serial", "rawtypes" })
public class SeparatorComboBox extends JComboBox implements KeyListener
{
private boolean released = true;
private boolean separatorSelected = false;
public SeparatorComboBox()
{
super();
init();
}
@SuppressWarnings("unchecked")
public SeparatorComboBox(ComboBoxModel model)
{
super(model);
init();
}
@SuppressWarnings("unchecked")
public SeparatorComboBox(Object[] items)
{
super(items);
init();
}
@SuppressWarnings("unchecked")
public SeparatorComboBox(Vector<?> items)
{
super(items);
init();
}
@SuppressWarnings("unchecked")
private void init()
{
setRenderer( new SeparatorRenderer() );
addKeyListener(this);
}
@Override
public void setSelectedIndex(int index)
{
Object value = getItemAt(index);
if (value instanceof JSeparator)
{
if (released)
{
separatorSelected = true;
return;
}
int current = getSelectedIndex();
index += (index > current) ? 1 : -1;
if (index == -1 || index >= dataModel.getSize())
return;
}
super.setSelectedIndex(index);
}
@Override
public void setPopupVisible(boolean visible)
{
if (separatorSelected)
{
separatorSelected = false;
return;
}
super.setPopupVisible(visible);
}
public void keyPressed(KeyEvent e)
{
released = false;
}
public void keyReleased(KeyEvent e)
{
released = true;
}
public void keyTyped(KeyEvent e) {}
class SeparatorRenderer extends BasicComboBoxRenderer
{
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus)
{
super.getListCellRendererComponent(
list, value, index, isSelected, cellHasFocus);
if (value instanceof JSeparator)
return (JSeparator)value;
return this;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment