JComboBox einträge rechts ausrichten ...
-
JcomboBox einträge rechts ausrichten ...
in dem unteren Beispiel werden die einträge im ComboBox automatisch links ausgerichtet
comboBox.addItem("1000"); comboBox.addItem("10"); comboBox.addItem("1"); comboBox.addItem("100'000"); comboBox.addItem("10'000"); comboBox.addItem("1'000");
wie kann ich die Item einträge „Rechts“ bzw. „Zentral“ ausrichten
Mit freundlichen Grüßen.
Christine.
-
Hi!
Das geht mit einem eigenen ComboBoxRenderer. Hier hast du ein etwas größeres Beispiel dafür:
/* * ListRendererDemo.java * * Created on 4. März 2003, 16:00 * by Cengiz Sahin (http://www.javaCore.de) */ package de.javacore.gui; import java.awt.Font; import java.awt.GraphicsEnvironment; import javax.swing.DefaultComboBoxModel; /** Demo for ListRenderers * * @author Sahin Cengiz (javaCore) */ public class ListRendererDemo extends javax.swing.JFrame { /** Creates a new form ListRendererDemo */ public ListRendererDemo() { initComponents(); DefaultComboBoxModel mcbm = new DefaultComboBoxModel(); GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); Font fonts[] = ge.getAllFonts(); for (int i=0; i<fonts.length-1; i++) { mcbm.addElement(fonts[i]); } jComboBox1.setModel(mcbm); jComboBox1.setRenderer(new ComboBoxRenderer()); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ private void initComponents() { jPanel1 = new javax.swing.JPanel(); jPanel2 = new javax.swing.JPanel(); jPanel3 = new javax.swing.JPanel(); jPanel4 = new javax.swing.JPanel(); jPanel5 = new javax.swing.JPanel(); jPanel6 = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); jComboBox1 = new javax.swing.JComboBox(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); setTitle("ListRendererDemo (FontSelector)"); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent evt) { exitForm(evt); } }); getContentPane().add(jPanel1, java.awt.BorderLayout.NORTH); getContentPane().add(jPanel2, java.awt.BorderLayout.EAST); getContentPane().add(jPanel3, java.awt.BorderLayout.SOUTH); getContentPane().add(jPanel4, java.awt.BorderLayout.WEST); jPanel5.setLayout(new java.awt.BorderLayout()); jPanel6.setLayout(new java.awt.BorderLayout(5, 5)); jLabel1.setText("Font:"); jPanel6.add(jLabel1, java.awt.BorderLayout.WEST); jComboBox1.addItemListener(new java.awt.event.ItemListener() { public void itemStateChanged(java.awt.event.ItemEvent evt) { jComboBox1ItemStateChanged(evt); } }); jPanel6.add(jComboBox1, java.awt.BorderLayout.CENTER); jPanel5.add(jPanel6, java.awt.BorderLayout.NORTH); jLabel2.setText("the quick brown fox jumps over the lazy dog"); jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); jPanel5.add(jLabel2, java.awt.BorderLayout.CENTER); jLabel3.setText("0 1 2 3 4 5 6 7 8 9"); jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); jPanel5.add(jLabel3, java.awt.BorderLayout.SOUTH); getContentPane().add(jPanel5, java.awt.BorderLayout.CENTER); pack(); } /** Called whenever the value in the combobox has changed */ private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) { Font theFont = (Font)jComboBox1.getSelectedItem(); jComboBox1.setFont(new java.awt.Font(theFont.getName(), theFont.getStyle(), 14)); jLabel2.setFont(jComboBox1.getFont()); jLabel3.setFont(jComboBox1.getFont()); } /** Exit the Application */ private void exitForm(java.awt.event.WindowEvent evt) { System.exit(0); } /** * @param args the command line arguments */ public static void main(String args[]) { new ListRendererDemo().show(); } // Variables declaration - do not modify private javax.swing.JComboBox jComboBox1; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel2; private javax.swing.JPanel jPanel6; private javax.swing.JLabel jLabel1; private javax.swing.JPanel jPanel5; private javax.swing.JPanel jPanel4; private javax.swing.JPanel jPanel3; private javax.swing.JPanel jPanel2; private javax.swing.JPanel jPanel1; // End of variables declaration } /** Benutzerdefinierter Renderer um in der ComboBox diverse Fonts anzeigen zu können */ class ComboBoxRenderer extends javax.swing.JLabel implements javax.swing.ListCellRenderer { public ComboBoxRenderer() { setOpaque(true); setHorizontalAlignment(CENTER); // Hier kann die Ausrichtung eingestellt werden setVerticalAlignment(CENTER); // dto. } public java.awt.Component getListCellRendererComponent( javax.swing.JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { Font theFont = (Font)value; // Vorsicht! Es muss nicht immer ein String sein! if (isSelected) { setBackground(list.getSelectionBackground()); setForeground(list.getSelectionForeground()); } else { setBackground(list.getBackground()); setForeground(list.getForeground()); } String fontName = theFont.getName(); if (theFont.hasUniformLineMetrics()) setText(fontName); else { setFont(new Font("Arial", Font.PLAIN, 16)); setText(fontName); } // Eigentlich passiert alles hier ... setFont(new Font(theFont.getName(), theFont.getStyle(), 14)); return this; } }
-
Danke der Ansatz ist GUTTTTT... ES FUNKT.
christine30.