RTLComboTest

import java.awt.BorderLayout; import java.awt.Component; import java.awt.ComponentOrientation; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.FlowLayout; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Locale; import java.util.Random; import java.util.SortedSet; import javax.swing.BorderFactory; import javax.swing.DefaultComboBoxModel; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.plaf.basic.BasicComboBoxRenderer; /** * @author John B. Matthews * copyright 2009; distribution per GPL * http://www.gnu.org/copyleft/gpl.html */ public class JComboTest { public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { //@Override public void run() { new JComboTest().createGUI(); } }); } private void createGUI() { final JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new GridLayout(0, 1)); frame.add(new ComboPanel("First")); frame.add(new ComboPanel("Second")); frame.add(new ComboPanel("Third")); frame.pack(); frame.setVisible(true); } } class ComboPanel extends JPanel { private static final int MAX = 10; private static final Random random = new Random(); private JComboBox combo; private SortedSet<String> set; private SortedComboBoxModel scbm; private Locale israel = new Locale("he", "IL"); private ComponentOrientation hebrewOrientation = ComponentOrientation.getOrientation(israel); public ComboPanel(String name) { this.setLayout(new BorderLayout()); this.setPreferredSize(new Dimension(240, 100)); this.setBorder(BorderFactory.createTitledBorder(name)); scbm = new SortedComboBoxModel(); for (int i = 0; i < MAX; i++) { scbm.addElement(randItem()); } combo = new JComboBox(scbm); combo.setComponentOrientation(hebrewOrientation); combo.setRenderer(new RTLComboBoxRenderer()); combo.setFont(new Font("Dialog", Font.PLAIN, 18)); combo.setSelectedIndex(random.nextInt(MAX)); this.add(combo, BorderLayout.CENTER); JPanel btnPanel = new JPanel(new FlowLayout()); JButton addBtn = new JButton(randItem()); addBtn.addActionListener(new ActionListener() { //@Override public void actionPerformed(ActionEvent e) { scbm.addElement(randItem()); } }); btnPanel.add(addBtn); this.add(btnPanel, BorderLayout.SOUTH); } private String randItem() { int base = 0x05d0; StringBuilder sb = new StringBuilder(); for (int i = 0; i < 10; i++) { sb.append((char) (random.nextInt(26) + base)); } return sb.toString(); } } class SortedComboBoxModel extends DefaultComboBoxModel { /** Create an empty model, so added elements get sorted. */ public SortedComboBoxModel() { super(); } /** Add elements by inserting in lexical order. */ @Override public void addElement(Object element) { this.insertElementAt(element, 0); } /** Insert in lexical order by name; ignore index. */ @Override public void insertElementAt(Object element, int index) { String name = element.toString(); for (index = 0; index < this.getSize(); index++) { String s = getElementAt(index).toString(); if (s.compareTo(name) > 0) { break; } } super.insertElementAt(element, index); } } class RTLComboBoxRenderer extends BasicComboBoxRenderer { @Override public Component getListCellRendererComponent( JList list, Object value, int index, boolean selected, boolean focused) { super.getListCellRendererComponent(list, value, index, selected, focused); this.setComponentOrientation(list.getComponentOrientation()); return this; } }
Comments