2

我目前正在ListCellRendererJComboBox. 为此,我使用系统在 L&F 更改时获取新的渲染器,并将方法委托给它。这适用于所有 L&F。但是,当我将此组件放在面板中时(因此我可以添加更多组件),它在 Metal 和 Windows L&F 中运行良好,但JComboBox文本在 Motif 中消失了。见截图和代码:

第三个渲染器的问题

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class TextDemo extends JPanel implements ActionListener {

    private static JFrame frame;

    public TextDemo() {
        super(new GridBagLayout());

        JComboBox correct = new JComboBox(new String[]{"One", "Two", "Three"});

        JComboBox alsoCorrect = new JComboBox(new String[]{"One", "Two", "Three"});
        alsoCorrect.setRenderer(new MyRenderer());

        JComboBox incorrect = new JComboBox(new String[]{"One", "Two", "Three"});
        incorrect.setRenderer(new NotWorkingRenderer());

        JButton button = new JButton("Change LnF");
        button.addActionListener(this);

        add(correct, getConstraints(0));
        add(alsoCorrect, getConstraints(1));
        add(incorrect, getConstraints(2));
        add(button, getConstraints(3));
    }

    private GridBagConstraints getConstraints(int y) {
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0; c.gridy = y;
        c.insets = new Insets(4,8,4,8);
        c.weightx = 1.0; c.weighty = 1.0;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.FIRST_LINE_START;
        return c;
    }

    @Override
    public void actionPerformed(ActionEvent ev) {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
            SwingUtilities.updateComponentTreeUI(frame);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private static void createAndShowGUI() {
        frame = new JFrame("TextDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new TextDemo());
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

class MyRenderer implements ListCellRenderer {
    protected static ListCellRenderer delegate;
    static {
        refreshRenderers();
        UIManager.addPropertyChangeListener(new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                if (evt.getPropertyName().equals("lookAndFeel")) {
                    refreshRenderers();
                }
            }
        });
    }
    protected static void refreshRenderers() {
        delegate = new JComboBox().getRenderer();
    }
    @Override
    public Component getListCellRendererComponent(JList list, Object value,
            int index, boolean isSelected, boolean cellHasFocus) {
        return delegate.getListCellRendererComponent(list, value,
                index, isSelected, cellHasFocus);
    }
}

class NotWorkingRenderer extends MyRenderer {
    private JPanel panel = new JPanel();
    public NotWorkingRenderer() {
        panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
    }
    @Override
    public Component getListCellRendererComponent(JList list, Object value,
            int index, boolean isSelected, boolean cellHasFocus) {
        Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        c.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
                    panel.removeAll();
        panel.add(c);
        return panel;
    }
}

任何有助于理解为什么会发生这种情况的帮助将不胜感激!

4

2 回答 2

1

不是答案,

  • 但是看看会发生什么,用 JPanel 作为渲染器 JComboBox 的 JComponents

  • 你确定带有字符串值的JPanel是正确的方法吗,请问目标是什么,

  • 默认的 JLabel, (J)Component 而不是 JPanel 是否有同样的效果

在此处输入图像描述在此处输入图像描述在此处输入图像描述

从代码

import java.awt.*;
import java.awt.event.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.*;

public class TextDemo extends JPanel implements ActionListener {

    private static JFrame frame;

    public TextDemo() {
        super(new GridBagLayout());
        JComboBox correct = new JComboBox(new String[]{"One", "Two", "Three"});
        JComboBox alsoCorrect = new JComboBox(new String[]{"One", "Two", "Three"});
        alsoCorrect.setRenderer(new MyRenderer());
        JComboBox incorrect = new JComboBox(new String[]{"One", "Two", "Three"});
        incorrect.setRenderer(new NotWorkingRenderer());
        JButton button = new JButton("Change LnF");
        button.addActionListener(this);
        add(incorrect, getConstraints(0));
        add(correct, getConstraints(1));
        add(alsoCorrect, getConstraints(2));
        add(button, getConstraints(3));
    }

    private GridBagConstraints getConstraints(int y) {
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = y;
        c.insets = new Insets(4, 8, 4, 8);
        c.weightx = 1.0;
        c.weighty = 1.0;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.FIRST_LINE_START;
        return c;
    }

    @Override
    public void actionPerformed(ActionEvent ev) {
        try {
            UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
            SwingUtilities.updateComponentTreeUI(frame);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private static void createAndShowGUI() {
        frame = new JFrame("TextDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new TextDemo());
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
            //SwingUtilities.updateComponentTreeUI(frame);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

class MyRenderer implements ListCellRenderer {

    protected static ListCellRenderer delegate;

    static {
        refreshRenderers();
        UIManager.addPropertyChangeListener(new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                if (evt.getPropertyName().equals("lookAndFeel")) {
                    refreshRenderers();
                }
            }
        });
    }

    protected static void refreshRenderers() {
        delegate = new JComboBox().getRenderer();
    }

    @Override
    public Component getListCellRendererComponent(JList list, Object value,
            int index, boolean isSelected, boolean cellHasFocus) {
        return delegate.getListCellRendererComponent(list, value,
                index, isSelected, cellHasFocus);
    }
}

class NotWorkingRenderer extends MyRenderer {

    private JPanel panel = new JPanel();

    public NotWorkingRenderer() {
        panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
    }

    @Override
    public Component getListCellRendererComponent(JList list, Object value,
            int index, boolean isSelected, boolean cellHasFocus) {
        Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        c.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
        panel.add(c);
        return panel;
    }
}

EDIT_1st。

  • 所有标准 L&F 不包括 ModifL&F 正确显示

  • 前进一步???,代码行incorrect.setEditable(true);生成

在此处输入图像描述

  • 我不知道 ModifL&F 和 non_editable JComboBox 的正确方法

EDIT_2nd。

  • 我瞎class NotWorkingRenderer extends MyRenderer {了,点...点...点...

  • phaaa 我从来没有参加过add/ remove/ modifya JComponentin Xxx(Xxx)Renderer,但是关于是否可能,

在此处输入图像描述

class NotWorkingRenderer extends BasicComboBoxRenderer {

    private JPanel panel = new JPanel();

    public NotWorkingRenderer() {
        panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
        //panel.setOpaque(false);
    }

    @Override
    public Component getListCellRendererComponent(JList list, Object value,
            int index, boolean isSelected, boolean cellHasFocus) {
        JLabel c = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        c.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));        
        panel.removeAll();
        panel.add(c);
        panel.revalidate();
        panel.repaint();
        return panel;
    }
}
于 2013-08-30T15:25:44.410 回答
1

您正在返回panel而不是c

Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        c.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
                    panel.removeAll();
        panel.add(c);
        return c;
于 2013-08-30T15:26:00.587 回答