Swing 相当于 HTML的内容是什么?

发布于 2024-10-20 14:17:39 字数 404 浏览 2 评论 0原文

我希望我的 JComboBox 将多个选项组合在一起,类似于 HTML optgroup< /a>:

<select>  
 <optgroup label="A">  
  <option/>
  <option/>  
 </optgroup>
</select>  

我在 Swing 中找不到任何解决方案。操纵 Combobox 的 UI 渲染器似乎是一个坏主意,因为它的操作系统和界面都不同。依赖于 L&F(并且它们是私有的,因此无法扩展)。

I want my JComboBox to group multiple options together, similar to the HTML optgroup:

<select>  
 <optgroup label="A">  
  <option/>
  <option/>  
 </optgroup>
</select>  

I could not find any solution for this in Swing. Manipulating the UI-Renderer for the Combobox seems to be a bad idea, as it's OS & L&F-dependent (and they are private so cannot extend).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

述情 2024-10-27 14:17:39

考虑以下实现作为如何应用自定义样式和创建不可选择项的基本指南:

public class ExtendedComboBox extends JComboBox {

    public ExtendedComboBox() {
        setModel(new ExtendedComboBoxModel());
        setRenderer(new ExtendedListCellRenderer());
    }

    public void addDelimiter(String text) {
        this.addItem(new Delimiter(text));
    }

    private static class ExtendedComboBoxModel extends DefaultComboBoxModel {
        @Override
        public void setSelectedItem(Object anObject) {
            if (!(anObject instanceof Delimiter)) {
                super.setSelectedItem(anObject);
            } else {
                int index = getIndexOf(anObject);
                if (index < getSize()) {
                    setSelectedItem(getElementAt(index+1));
                }
            }
        }

    }

    private static class ExtendedListCellRenderer 
                    extends DefaultListCellRenderer {

        @Override
        public Component getListCellRendererComponent(JList list, Object value,
                        int index, boolean isSelected, boolean cellHasFocus) {
            if (!(value instanceof Delimiter)) {
                return super.getListCellRendererComponent(list, value, index,
                        isSelected, cellHasFocus);
            } else {
                JLabel label = new JLabel(value.toString());
                Font f = label.getFont();
                label.setFont(f.deriveFont(f.getStyle() 
                           | Font.BOLD | Font.ITALIC));
                return label;
            }
        }
    }

    private static class Delimiter {
        private String text;

        private Delimiter(String text) {
            this.text = text;
        }

        @Override
        public String toString() {
            return text.toString();
        }
    }
}

Consider the following implementation as a basic guide how to apply custom styling and create non-selectable items:

public class ExtendedComboBox extends JComboBox {

    public ExtendedComboBox() {
        setModel(new ExtendedComboBoxModel());
        setRenderer(new ExtendedListCellRenderer());
    }

    public void addDelimiter(String text) {
        this.addItem(new Delimiter(text));
    }

    private static class ExtendedComboBoxModel extends DefaultComboBoxModel {
        @Override
        public void setSelectedItem(Object anObject) {
            if (!(anObject instanceof Delimiter)) {
                super.setSelectedItem(anObject);
            } else {
                int index = getIndexOf(anObject);
                if (index < getSize()) {
                    setSelectedItem(getElementAt(index+1));
                }
            }
        }

    }

    private static class ExtendedListCellRenderer 
                    extends DefaultListCellRenderer {

        @Override
        public Component getListCellRendererComponent(JList list, Object value,
                        int index, boolean isSelected, boolean cellHasFocus) {
            if (!(value instanceof Delimiter)) {
                return super.getListCellRendererComponent(list, value, index,
                        isSelected, cellHasFocus);
            } else {
                JLabel label = new JLabel(value.toString());
                Font f = label.getFont();
                label.setFont(f.deriveFont(f.getStyle() 
                           | Font.BOLD | Font.ITALIC));
                return label;
            }
        }
    }

    private static class Delimiter {
        private String text;

        private Delimiter(String text) {
            this.text = text;
        }

        @Override
        public String toString() {
            return text.toString();
        }
    }
}
寄风 2024-10-27 14:17:39

您可以在自定义渲染器中执行此操作,如 中所述如何使用组合框:提供自定义渲染器

You can do this in a custom renderer, as discussed in How to Use Combo Boxes: Providing a Custom Renderer.

静谧 2024-10-27 14:17:39

我不相信有一种简单的方法可以做到这一点,但有一种方法可以做到这一点。

我将实现一个数据模型类来指示您上面描述的分组。将这些数据模型的实例放入 javax.swing.ComboBoxModel 实现实例。

然后,您可以实现 javax.swing.ListCellRenderer 根据需要设置文本数据缩进的输出格式。您可能只想扩展 javax.swing.DefaultListCellRenderer 或者可能从 Java 源代码借用它的实现。

至于 L&F,您应该能够通过使用上述方法保持在正常指导范围内,并且您不必费力去弄清楚如何实施它。查看默认的 Swing 组件,它们将提供有关如何处理 L&F 的大量见解。

此外,我认为有一些机制(您必须原谅我,自从我完成完整的 Swing 开发以来已经很多年了)可以让您确定某个项目是否可选。

I don't believe that there is one simple way of doing this, but there is a way to do it.

I would implement a data model class that indicates the grouping that you've describe above. Place instances of those data models in your javax.swing.ComboBoxModel implementation instance.

You can then implement a javax.swing.ListCellRenderer to format the output as you like with indents for the text data. You may just want to extend the javax.swing.DefaultListCellRenderer or possibly borrow its implementation wholesale from the Java source.

As for the L&F you should be able to stay within normal guidelines by using the above methods and you won't have to fight with figuring out how to implement it. Look at the default Swing components they will provide a lot of insight in to how to deal with L&F.

Additionally, I think there are mechanisms (you'll have to forgive me, it's been YEARS since I've done full Swing development) to allow you to determine if an item is selectable or not.

安稳善良 2024-10-27 14:17:39

今天我自己就想要这个,并且我花了一天的时间来弄清楚如何将这些东西拼凑在一起,以使用 JList 而不是使用建议的 JComboBox 来实现类似的模型。我终于想出了一个使用 GlazedLists EventList 和 SeparatorList 以及相应的 DefaultEventListModel 的解决方案。我重写了 CellRenderer 和 DefaultListSelectionModel。最后,我对自己的问题发布了自己的答案:如何防止在 JList 中选择 SeparatorList.Separator?

这是我的最终工作代码:

public class MyFrame extends javax.swing.JFrame {
    private final EventList<BibleVersion> bibleVersions;
    private final SeparatorList<BibleVersion> versionsByLang;
    private boolean[] enabledFlags;

    public MyFrame(){
        bibleVersions = new BasicEventList<>();
        bibleVersions.add(new BibleVersion("CEI2008", "Testo della Conferenza Episcopale Italiana", "2008", "Italian"));
        bibleVersions.add(new BibleVersion("LUZZI", "Diodati Nuova Riveduta - Luzzi", "1927", "Italian"));
        bibleVersions.add(new BibleVersion("NVBSE", "Nova Vulgata - Bibliorum Sacrorum Editio", "1979", "Latin"));
        bibleVersions.add(new BibleVersion("NABRE", "New American Bible - Revised Edition", "2011", "English"));
        bibleVersions.add(new BibleVersion("KJV", "King James Version", "1611", "English"));
        versionsByLang = new SeparatorList<>(bibleVersions, new VersionComparator(),1, 1000);
        int listLength = versionsByLang.size();
        enabledFlags = new boolean[listLength];

        ListIterator itr = versionsByLang.listIterator();
        while(itr.hasNext()){
            enabledFlags[itr.nextIndex()] = !(itr.next().getClass().getSimpleName().equals("GroupSeparator"));
        }
        jList = new javax.swing.JList();
        jList.setModel(new DefaultEventListModel<>(versionsByLang));
        jList.setCellRenderer(new VersionCellRenderer());
        jList.setSelectionModel(new DisabledItemSelectionModel());
        ListSelectionModel listSelectionModel = jList.getSelectionModel();
        listSelectionModel.addListSelectionListener(new SharedListSelectionHandler());

    }

    public static class BibleVersion {
        private String abbrev;
        private String fullname;
        private String year;
        private String lang;

        public BibleVersion(String abbrev, String fullname, String year, String lang) {
            this.abbrev = abbrev;
            this.fullname = fullname;
            this.year = year;
            this.lang = lang;
        }        

        public String getAbbrev() {
            return abbrev;
        }

        public void setAbbrev(String abbrev) {
            this.abbrev = abbrev;
        }

        public String getFullname() {
            return fullname;
        }

        public void setFullname(String fullname) {
            this.fullname = fullname;
        }

        public String getYear() {
            return year;
        }

        public void setYear(String year) {
            this.year = year;
        }

        public String getLang() {
            return lang;
        }

        public void setLang(String lang) {
            this.lang = lang;
        }

        @Override
        public String toString() {
            return this.getAbbrev() + " — " + this.getFullname() + " (" + this.getYear() + ")"; //To change body of generated methods, choose Tools | Templates.
        }                

    }

    private static class VersionComparator implements Comparator<BibleVersion> {

        @Override
        public int compare(BibleVersion o1, BibleVersion o2) {
            return o1.getLang().compareTo(o2.getLang());
        }            

    }

    private static class VersionCellRenderer extends DefaultListCellRenderer{

        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);

            if (value instanceof SeparatorList.Separator) {
                SeparatorList.Separator separator = (SeparatorList.Separator) value;
                BibleVersion bibleversion = (BibleVersion)separator.getGroup().get(0);
                String lbl = "-- " + bibleversion.getLang() + " --";
                label.setText(lbl);
                label.setFont(label.getFont().deriveFont(Font.BOLD));
                label.setBackground(Color.decode("#004400"));
                label.setBorder(BorderFactory.createEmptyBorder(0,5,0,0));
                label.setEnabled(false);
            } else {
                label.setFont(label.getFont().deriveFont(Font.PLAIN));
                label.setBorder(BorderFactory.createEmptyBorder(0,15,0,0));
            }

            return label;
        }
    }

private class DisabledItemSelectionModel extends DefaultListSelectionModel {

    private static final long serialVersionUID = 1L;

    @Override
    public void setSelectionInterval(int index0, int index1) {
        if(index0 < index1){
            for (int i = index0; i <= index1; i++){
                if(enabledFlags[i]){
                    super.addSelectionInterval(i, i);
                }
            }
        }
        else if(index1 < index0){
            for (int i = index1; i <= index0; i++){
                if(enabledFlags[i]){
                    super.addSelectionInterval(i, i);
                }
            }
        }
        else if(index0 == index1){
            if(enabledFlags[index0]){ super.setSelectionInterval(index0,index0); }
        }
    }

    @Override
    public void addSelectionInterval(int index0, int index1) {
        if(index0 < index1){
            for (int i = index0; i <= index1; i++){
                if(enabledFlags[i]){
                    super.addSelectionInterval(i, i);
                }
            }
        }
        else if(index1 < index0){
            for (int i = index1; i <= index0; i++){
                if(enabledFlags[i]){
                    super.addSelectionInterval(i, i);
                }
            }
        }
        else if(index0 == index1){
            if(enabledFlags[index0]){ super.addSelectionInterval(index0,index0); }
        }
    }        

}

private class SharedListSelectionHandler implements ListSelectionListener {
    @Override
    public void valueChanged(ListSelectionEvent e) {
        ListSelectionModel lsm = (ListSelectionModel)e.getSource();
        StringBuilder output = new StringBuilder();
        int firstIndex = e.getFirstIndex();
        int lastIndex = e.getLastIndex();
        boolean isAdjusting = e.getValueIsAdjusting();
        output.append("Event for indexes ");
        output.append(firstIndex);
        output.append(" - ");
        output.append(lastIndex);
        output.append("; isAdjusting is ");
        output.append(isAdjusting);
        output.append("; selected indexes:");

        if (lsm.isSelectionEmpty()) {
            output.append(" <none>");
        } else {
            // Find out which indexes are selected.
            int minIndex = lsm.getMinSelectionIndex();
            int maxIndex = lsm.getMaxSelectionIndex();
            for (int i = minIndex; i <= maxIndex; i++) {
                if (lsm.isSelectedIndex(i)) {
                    output.append(" ");
                    output.append(i);
                }
            }
        }
        output.append(System.getProperty("line.separator"));
        System.out.println(output.toString());
    }
}


}

I was wanting this myself today, and I've spent the day figuring it out piecing things together to implement a similar model with a JList rather than with the suggested JComboBox. I've finally come up with a solution using GlazedLists EventList and SeparatorList with the corresponding DefaultEventListModel. I override the CellRenderer and the DefaultListSelectionModel. In the end I posted my own answer to my own question on this: How to prevent selection of SeparatorList.Separator in a JList?

Here's my final working code:

public class MyFrame extends javax.swing.JFrame {
    private final EventList<BibleVersion> bibleVersions;
    private final SeparatorList<BibleVersion> versionsByLang;
    private boolean[] enabledFlags;

    public MyFrame(){
        bibleVersions = new BasicEventList<>();
        bibleVersions.add(new BibleVersion("CEI2008", "Testo della Conferenza Episcopale Italiana", "2008", "Italian"));
        bibleVersions.add(new BibleVersion("LUZZI", "Diodati Nuova Riveduta - Luzzi", "1927", "Italian"));
        bibleVersions.add(new BibleVersion("NVBSE", "Nova Vulgata - Bibliorum Sacrorum Editio", "1979", "Latin"));
        bibleVersions.add(new BibleVersion("NABRE", "New American Bible - Revised Edition", "2011", "English"));
        bibleVersions.add(new BibleVersion("KJV", "King James Version", "1611", "English"));
        versionsByLang = new SeparatorList<>(bibleVersions, new VersionComparator(),1, 1000);
        int listLength = versionsByLang.size();
        enabledFlags = new boolean[listLength];

        ListIterator itr = versionsByLang.listIterator();
        while(itr.hasNext()){
            enabledFlags[itr.nextIndex()] = !(itr.next().getClass().getSimpleName().equals("GroupSeparator"));
        }
        jList = new javax.swing.JList();
        jList.setModel(new DefaultEventListModel<>(versionsByLang));
        jList.setCellRenderer(new VersionCellRenderer());
        jList.setSelectionModel(new DisabledItemSelectionModel());
        ListSelectionModel listSelectionModel = jList.getSelectionModel();
        listSelectionModel.addListSelectionListener(new SharedListSelectionHandler());

    }

    public static class BibleVersion {
        private String abbrev;
        private String fullname;
        private String year;
        private String lang;

        public BibleVersion(String abbrev, String fullname, String year, String lang) {
            this.abbrev = abbrev;
            this.fullname = fullname;
            this.year = year;
            this.lang = lang;
        }        

        public String getAbbrev() {
            return abbrev;
        }

        public void setAbbrev(String abbrev) {
            this.abbrev = abbrev;
        }

        public String getFullname() {
            return fullname;
        }

        public void setFullname(String fullname) {
            this.fullname = fullname;
        }

        public String getYear() {
            return year;
        }

        public void setYear(String year) {
            this.year = year;
        }

        public String getLang() {
            return lang;
        }

        public void setLang(String lang) {
            this.lang = lang;
        }

        @Override
        public String toString() {
            return this.getAbbrev() + " — " + this.getFullname() + " (" + this.getYear() + ")"; //To change body of generated methods, choose Tools | Templates.
        }                

    }

    private static class VersionComparator implements Comparator<BibleVersion> {

        @Override
        public int compare(BibleVersion o1, BibleVersion o2) {
            return o1.getLang().compareTo(o2.getLang());
        }            

    }

    private static class VersionCellRenderer extends DefaultListCellRenderer{

        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);

            if (value instanceof SeparatorList.Separator) {
                SeparatorList.Separator separator = (SeparatorList.Separator) value;
                BibleVersion bibleversion = (BibleVersion)separator.getGroup().get(0);
                String lbl = "-- " + bibleversion.getLang() + " --";
                label.setText(lbl);
                label.setFont(label.getFont().deriveFont(Font.BOLD));
                label.setBackground(Color.decode("#004400"));
                label.setBorder(BorderFactory.createEmptyBorder(0,5,0,0));
                label.setEnabled(false);
            } else {
                label.setFont(label.getFont().deriveFont(Font.PLAIN));
                label.setBorder(BorderFactory.createEmptyBorder(0,15,0,0));
            }

            return label;
        }
    }

private class DisabledItemSelectionModel extends DefaultListSelectionModel {

    private static final long serialVersionUID = 1L;

    @Override
    public void setSelectionInterval(int index0, int index1) {
        if(index0 < index1){
            for (int i = index0; i <= index1; i++){
                if(enabledFlags[i]){
                    super.addSelectionInterval(i, i);
                }
            }
        }
        else if(index1 < index0){
            for (int i = index1; i <= index0; i++){
                if(enabledFlags[i]){
                    super.addSelectionInterval(i, i);
                }
            }
        }
        else if(index0 == index1){
            if(enabledFlags[index0]){ super.setSelectionInterval(index0,index0); }
        }
    }

    @Override
    public void addSelectionInterval(int index0, int index1) {
        if(index0 < index1){
            for (int i = index0; i <= index1; i++){
                if(enabledFlags[i]){
                    super.addSelectionInterval(i, i);
                }
            }
        }
        else if(index1 < index0){
            for (int i = index1; i <= index0; i++){
                if(enabledFlags[i]){
                    super.addSelectionInterval(i, i);
                }
            }
        }
        else if(index0 == index1){
            if(enabledFlags[index0]){ super.addSelectionInterval(index0,index0); }
        }
    }        

}

private class SharedListSelectionHandler implements ListSelectionListener {
    @Override
    public void valueChanged(ListSelectionEvent e) {
        ListSelectionModel lsm = (ListSelectionModel)e.getSource();
        StringBuilder output = new StringBuilder();
        int firstIndex = e.getFirstIndex();
        int lastIndex = e.getLastIndex();
        boolean isAdjusting = e.getValueIsAdjusting();
        output.append("Event for indexes ");
        output.append(firstIndex);
        output.append(" - ");
        output.append(lastIndex);
        output.append("; isAdjusting is ");
        output.append(isAdjusting);
        output.append("; selected indexes:");

        if (lsm.isSelectionEmpty()) {
            output.append(" <none>");
        } else {
            // Find out which indexes are selected.
            int minIndex = lsm.getMinSelectionIndex();
            int maxIndex = lsm.getMaxSelectionIndex();
            for (int i = minIndex; i <= maxIndex; i++) {
                if (lsm.isSelectedIndex(i)) {
                    output.append(" ");
                    output.append(i);
                }
            }
        }
        output.append(System.getProperty("line.separator"));
        System.out.println(output.toString());
    }
}


}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文