控制HTML JLabel的重新验证

时间:2013-10-24 00:10:41

标签: java swing jlabel jtree gridbaglayout

我有一个Swing GUI挑战,我已经提炼到以下SSCCE。问题是当更新窗口底部的JLabel以反映所选节点时,JTree / JScrollPane以令人不安的方式跳转。

查看此行为的最佳方法是运行SSCCE,单击JTree,然后按住键盘的向下箭头以快速遍历JTree。 GUI闪烁,好像JLabel被删除然后再次添加。但事实并非如此 - 我只是更新了JLabel的文本。

我想控制重绘GUI,这样就不会那么刺耳了。我需要保留“状态栏”的自动换行和动态高度 - 使用固定高度组件可以解决跳跃问题,但会破坏设计理念。

我尝试了几种不同的方法来更新“状态栏”区域(您可以在SSCCE的TreeSelectionListener中看到的方法之一),但我尝试的所有方法都会导致高度重新计算不当或闪烁

enter image description here

import java.awt.*;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;

public class JTreePlusStatus extends JFrame {
    JPanel _contentPane;
    JScrollPane _jScrollPane;
    JTree _jTree;
    JLabel _jLabelDescription;
    GridBagConstraints _gbcJLabelDescription;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    String sysLaf = UIManager.getSystemLookAndFeelClassName();
                    UIManager.setLookAndFeel(sysLaf);
                    JFrame frame = new JTreePlusStatus();
                    frame.setSize(300, 300);
                    frame.setLocationRelativeTo(null);
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public JTreePlusStatus() {
        initComponents();
        initCustom();
    }

    void initComponents() {
        // Boilerplate layout busywork
        _contentPane = new JPanel();
        GridBagLayout gblContentPane = new GridBagLayout();
        _contentPane.setLayout(gblContentPane);
        setContentPane(_contentPane);

        _jScrollPane = new JScrollPane();
        GridBagConstraints gbcJScrollPane = new GridBagConstraints();
        gbcJScrollPane.fill = GridBagConstraints.BOTH;
        gbcJScrollPane.weighty = 1.0;
        gbcJScrollPane.weightx = 1.0;
        gbcJScrollPane.gridx = 0;
        gbcJScrollPane.gridy = 0;
        _contentPane.add(_jScrollPane, gbcJScrollPane);

        _jTree = new JTree();
        _jScrollPane.setViewportView(_jTree);
        _jTree.addTreeSelectionListener(new TreeSelectionListenerImpl());

        _jLabelDescription = new JLabel(" ");
        _gbcJLabelDescription = new GridBagConstraints();
        _gbcJLabelDescription.fill = GridBagConstraints.HORIZONTAL;
        _gbcJLabelDescription.gridx = 0;
        _gbcJLabelDescription.gridy = 1;
        _contentPane.add(_jLabelDescription, _gbcJLabelDescription);
    }

    void initCustom() {
        // Set sample data onto the JTree
        _jTree.setModel(createSampleModel());

        // Expand entire tree
        for (int i = 0; i < _jTree.getRowCount(); i++) {
            _jTree.expandRow(i);
        }
    }

    public DefaultTreeModel createSampleModel() {
        DefaultMutableTreeNode top = new DefaultMutableTreeNode("Music");
        for (int i = 0; i < 100; i++) {
            String s = "Classical - Concertos - Beethoven "
                    + "- S No. 5 - E-Flat Major " + i;
            top.add(new DefaultMutableTreeNode(s));
        }
        DefaultTreeModel defaultTreeModel = new DefaultTreeModel(top);
        return defaultTreeModel;
    }

    class TreeSelectionListenerImpl implements TreeSelectionListener {

        public void valueChanged(TreeSelectionEvent e) {
            TreePath selectionPath = _jTree.getSelectionPath();
            String s = selectionPath.getLastPathComponent().toString();
            String newText = "<html><b>Name:</b> " + s 
                    + " <br><b>Description: </b>" + s + "</html>";

            /** Tried this -- causes the JTree to jump around */
            _jLabelDescription.setText(newText);

            /**
             * OK, try making a new JLabel and "set" it into the parent,
             * hoping that there won't be an intermediate state where label's
             * space in the GUI collapses temporarily
             */
            //final JLabel newJLabel = new JLabel(newText);
            //final Container parent = _jLabelDescription.getParent();
            //parent.add(newJLabel, _gbcJLabelDescription, 1);
            //parent.remove(_jLabelDescription);
            //if (parent instanceof JComponent) {
            //    JComponent jc = (JComponent) parent;
            //    jc.validate(); // must revalidate after adding or removing
            //}
            //_jLabelDescription = newJLabel;

            // No luck, JTree/JScrollPane still jumps around
        }
    }
}

1 个答案:

答案 0 :(得分:4)

使用JTextPane而不是JLabel时似乎工作正常:

    _jLabelDescription = new JTextPane();
    _jLabelDescription.setContentType( "text/html" );