0

我正在做一个播放视频的应用程序,并在 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 代码。做这个例子我注意到,当 JCombobox 的同一行中的 JLabel es 重现相同的问题但在另一行中的时候不会发生。

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);
    }

}
4

0 回答 0