JLabel закрашивает новый текст поверх старого, после того как установленный текст называется

У меня есть диалоговое окно выполнения, которое содержит 3 JComponents: JLabel, JProgressBar, JButton, которое используется как диалоговое окно по умолчанию в разных частях приложения из разных потоков. Поэтому, когда я пытаюсь изменить значение метки, фон под ней не очищается, он просто рисует новый текст поверх старого. Класс-оболочка не отменяет никаких методов, он просто делегирует вызовы методов компонентам, которые он содержит.

Вот код:

  public void setNote(String note) {
        this.note = note;
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
               label.setText(ProgressDialog.this.note);
            }
         });
    }

Фактический результат аналогичен http://www.daniweb.com/forums/post1073367 .html # post1073367 But that solution was not appropriate for me.

Has anyone faced problem like this?

Thanks.

This is cuted version of the class. But as I said i could not make it work incorrectly. Hope this helps.

    public class Tesssst {

    public static void main(String [] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        ProgressDialog dialog = new ProgressDialog(frame, "Title", "Message");
        dialog.showDialog(true);

    }
}

class ProgressDialog extends JComponent {
    /**
     *
     */
    private JProgressBar progressBar;
    private JLabel label;
    private JFrame parentComponent;
    private String title;
    private String note;
    private boolean canceled;
    private boolean cancelEnabled;
    private JButton btnCancel;
    private JPanel contentPanel;

    public ProgressDialog(JFrame parentComponent, String title, String message) {
        this.parentComponent = parentComponent;
        this.title = title;
        progressBar = new JProgressBar();
        label = new JLabel();
        contentPanel =new JPanel();
        canceled = false;
        cancelEnabled = true;
        setNote(message);
        setOpaque(true);

    }
    public void setNote(String note) {
        this.note = note;
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                label.setText(ProgressDialog.this.note);
            }
         });
    }

    public String getNote() {
        return note;
    }

    protected void initDialog() {
        setBorder(new EmptyBorder(6, 6, 6, 6));
        contentPanel = new JPanel();
        contentPanel.setOpaque(true);
        setLayout(new BorderLayout());
        add(contentPanel);
        btnCancel = new JButton("Cancel");
        btnCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                label.setText("ololo");
            }

        });

        contentPanel.setLayout(new GridBagLayout());
        {
        GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.fill = GridBagConstraints.NONE;
            gbc.anchor = GridBagConstraints.NORTHWEST;
            gbc.insets = new Insets(2, 0, 0, 0);
            label.setOpaque(true);
            contentPanel.add(label, gbc);
        } // label

        {
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 1;
            gbc.anchor = GridBagConstraints.NORTH;
            gbc.weightx = 1;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.insets = new Insets(4, 0, 4, 0);
            contentPanel.add(progressBar, gbc);
        } // progressBar

        {
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 2;
            gbc.anchor = GridBagConstraints.NORTH;
            gbc.fill = GridBagConstraints.NONE;
            gbc.insets = new Insets(4, 0, 4, 0);
            contentPanel.add(btnCancel, gbc);
            btnCancel.setEnabled(cancelEnabled);
        } // cancel*/
    } // funciton

    public boolean isCanceled() {
        return canceled;
    }

    public void showDialog() {
        showDialog(false);
    }

    public void showDialog(boolean modal) {
        JDialog dialog = new JDialog(parentComponent, true);
        initDialog();
        dialog.getContentPane().add(contentPanel);
        dialog.setSize(400,400);
        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        if (modal) {
            dialog.setAlwaysOnTop(true);
        }
        dialog.setVisible(true);
        dialog.toFront();
    }

    public void cancel() {
        canceled = true;
    }

}

6
задан kiamlaluno 28 September 2011 в 12:09
поделиться