【问题标题】:JComboBox how to show the right end of the item?JComboBox如何显示item的右端?
【发布时间】:2014-09-13 11:54:07
【问题描述】:

我有以下 Java 代码:

import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JComboBox;
import javax.swing.JFrame;

public class EnumsRightVisible {

    public void show() {
        JFrame frame = new JFrame("Combo box");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        String[] items = {"aaaaaaaaa__________zzzzzzzzz",
                          "aaaaaaaaa__________zzzzzzzzz",
                          "aaaaaaaaa__________zzzzzzzzz"};
        JComboBox combo = new JComboBox(items);
        combo.setPreferredSize(new Dimension(100,20));
        frame.add(combo, BorderLayout.CENTER);
        frame.setLocation(600, 100);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String args[]) {
        EnumsRightVisible enumsRightVisible = new EnumsRightVisible();
        enumsRightVisible.show();
    }
}

运行它可以看到可见文本是左向的。

请注意,这段代码并不能解决我的问题(它将文本向右对齐,但仅在组合框展开时):

((JLabel)comboBox.getRenderer()).setHorizontalAlignment(JLabel.RIGHT);

如何在同一窗口中显示文本的右端 (...__zzzzzz)? 提前致谢!

【问题讨论】:

    标签: java swing alignment jcombobox renderer


    【解决方案1】:

    方法一

    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    public class ComboBoxDemo extends JFrame {
    
        public ComboBoxDemo() {
            JComboBox comboBox = new JComboBox();
            
            ((JLabel)comboBox.getRenderer()).setHorizontalAlignment(JLabel.RIGHT);
            
            comboBox.addItem("Apple");
            comboBox.addItem("Orange");
            comboBox.addItem("Mango");
            
            getContentPane().add(comboBox, "North");
            setSize(200, 100);
            this.setLocationRelativeTo(null);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
    
        public static void main(String[] args) {
                    new ComboBoxDemo().setVisible(true);
        }
    }
    

    方法2(显然,首先更好)

    import java.awt.Component;
    import java.awt.ComponentOrientation;
    import javax.swing.DefaultListCellRenderer;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JList;
    import javax.swing.ListCellRenderer;
    
    public class ComboBoxDemo extends JFrame {
    
        public ComboBoxDemo() {
            JComboBox comboBox = new JComboBox();
    
            setListCellRendererOf(comboBox);
            
            comboBox.addItem("Apple");
            comboBox.addItem("Orange");
            comboBox.addItem("Mango");
    
            getContentPane().add(comboBox, "North");
            setSize(200, 100);
            this.setLocationRelativeTo(null);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
    
        private void setListCellRendererOf(JComboBox comboBox) {
            comboBox.setRenderer(new ListCellRenderer() {
                
                @Override
                public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
    
                    Component component = new DefaultListCellRenderer()
                            .getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
    
                    component.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
                    return component;
                }
            });
        }
        public static void main(String[] args) {
                    new ComboBoxDemo().setVisible(true);
        }
    }
    

    【讨论】:

    • 感谢您提供的示例,但它们并不能解决我的问题 - 当组合框太小而无法显示整个文本时 - 仅显示最左侧的部分,而不是文本的最后部分。
    【解决方案2】:

    使用自定义渲染器,该渲染器使用 FontMetrics 来测量字符串的宽度并手动执行省略号 (...)。更多信息在这里:http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html#renderer

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-17
      • 1970-01-01
      • 1970-01-01
      • 2012-08-18
      相关资源
      最近更新 更多