在更新JLabel上防止JComboBox呈现

时间:2013-06-12 16:25:35

标签: java swing jcombobox vlcj

我正在做一个播放视频的应用程序,而在JLabel我正在显示视频的时间码。还有一个JCombobox,允许更改视频的字幕。 JCombobox有一个ListCellRenderer,可以更改每个组合框的默认输出。 问题是,每次JLabel更改其值时,JCombobox都会再次渲染。

我认为这是浪费资源还有一些改变这种行为的方法吗?

有相对于JComboBox的代码。 JLabel每秒都会被swing.Timer修改。

JComboBox comboBox = new JComboBox<>();

comboBox.setEditable(false);


    DefaultComboBoxModel<File> defaultComboBox = new DefaultComboBoxModel<>();
    for (File f : Player.capitulo.getFicheros()) {

        if (f.getAbsolutePath().endsWith(".srt")) {
            System.out.println(f.getAbsolutePath());
            defaultComboBox.addElement(f);
        }
    }
 comboBox.setModel(defaultComboBox);
 comboBox.setRenderer(new ListRenderer());


public class ListRenderer implements ListCellRenderer<File> {
protected DefaultListCellRenderer defaultRenderer = new DefaultListCellRenderer();

@Override
public Component getListCellRendererComponent(JList<? extends File> list,
        File value, int index, boolean isSelected, boolean cellHasFocus) {
    JLabel renderer = new JLabel();
    // JLabel renderer = (JLabel) defaultRenderer
    // .getListCellRendererComponent(list, value, index, isSelected,
    // cellHasFocus);
    if (value != null) {
        Path p = value.toPath();
        System.out.println(p.getParent());
        String language = value.getName().trim().replace(".srt", "");
        language = language.substring(language.lastIndexOf(".") + 1,
                language.length());

        // System.out.println(language.length());
        if (language.length() == 3) {
            renderer.setText(language);
        } else {
            renderer.setText("Subtitulo " + (index + 1));
        }

    }

    return renderer;
}
}

更新:以下是重现问题的示例

好的,这是SSCCE代码。做这个例子我注意到当JLabobob在同一行的JCombobox中重现同样的问题但是在其他行中的时候不会发生。

public class Example extends JFrame {

    private JPanel contentPane;
    private JLabel lblNewLabel;
    private JComboBox<String> comboBox;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Example frame = new Example();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Example() {
        initGUI();
    }
    private void initGUI() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 428, 362);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        lblNewLabel = new JLabel("New label");
        contentPane.add(lblNewLabel, BorderLayout.WEST);
        Timer t = new Timer(1000, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Random r = new Random();
                lblNewLabel.setText(String.valueOf(r.nextInt()));

            }
        });
        t.start();
        comboBox = new JComboBox<>();
        comboBox.setRenderer(new ListCellRenderer<String>() {

            @Override
            public Component getListCellRendererComponent(
                    JList<? extends String> list, String value, int index,
                    boolean isSelected, boolean cellHasFocus) {
                JLabel label = new JLabel("");
                System.out.println("Pass");
                return label;
            }
        });
        contentPane.add(comboBox, BorderLayout.CENTER);
    }

}

0 个答案:

没有答案