下面是代码中有关树操作的部分,一事件发生后触发 centerPanel.repaint(),但是为什么树的视图却不改变,(测试发现服务器回发的数据及别的都没问题,就只是树视图不更改)import com.borland.jbcl.layout.*;
private CenterPanel centerPanel;public class MainFrame extends JFrame {
       
       public MainFrame() { ...
        centerPanel = new CenterPanel();
((JPanel)this.getContentPane()).add(centerPanel, BorderLayout.CENTER);
        ...       } ...       class CenterPanel extends JPanel { public CenterPanel() { //设计布局为Borland的com.borland.jbcl.layout.XYLayout;
XYLayout xYLayout1 = new XYLayout();
xYLayout1.setWidth(this.getWidth());
xYLayout1.setHeight(this.getHeight()); this.setLayout(xYLayout1);

treeRefresh(); }
//创建树
public void treeRefresh() {
if (close) {
                                //发送数据到服务器取好友的状态(在线离线)信息
try {

if(socket == null || socket.isClosed()) socket = new Socket(InetAddress.getLocalHost()
.getHostAddress(), 1235);
in = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())));
} catch (IOException e) {
e.printStackTrace();
} String sqlOnline = "select count(*) from qq_friends where gonline = 'y' and hostno = "
+ myNo;
String sqlTotal = "select count(*) from qq_friends where hostno = "
+ myNo; out.println("getfriends");
out.println(sqlOnline);
out.println(sqlTotal);
out.flush(); String online = null;
String total = null;
try {
online = in.readLine();
total = in.readLine();
} catch (IOException e) {
e.printStackTrace();
}                                //树的根节点
DefaultMutableTreeNode root = new DefaultMutableTreeNode("qq");
DefaultMutableTreeNode friends = new DefaultMutableTreeNode(
"我的好友(" + online + "/" + total + ")"); String selectOnlineSql = "select guestno, guestname, guestip from qq_friends where hostno = "
+ myNo + "and gonline = 'y'";
out.println(selectOnlineSql);
String selectOfflineSql = "select guestno, guestname, guestip from qq_friends where hostno = "
+ myNo + "and gonline = 'n'";
out.println(selectOfflineSql);
out.flush(); try {
int onlineListSize = Integer.parseInt(in.readLine());
for (int i = 0; i < onlineListSize; i++) {
String onlineNname = in.readLine();
friends.add(new DefaultMutableTreeNode(onlineNname
+ "(在线)")); }
int offlineListSize = Integer.parseInt(in.readLine());
for (int i = 0; i < offlineListSize; i++) {
String offlineName = in.readLine();
friends.add(new DefaultMutableTreeNode(offlineName
+ "(离线)")); }
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} try {
out.close();
in.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
} root.add(friends); friendTree = new JTree(root);
friendTree.putClientProperty("JTree.lineStyle", "None");
friendTree.setRootVisible(false);
friendTree.setBackground(SystemColor.inactiveCaptionText);
friendTree.setAutoscrolls(true);
friendTree.setBorder(BorderFactory.createLoweredBevelBorder());
friendTree.setOpaque(true);
friendTree.setScrollsOnExpand(true);
friendTree.setShowsRootHandles(false);
friendTree.setForeground(SystemColor.desktop);

// 设置树的模型为单选模型
int mode = TreeSelectionModel.SINGLE_TREE_SELECTION;
friendTree.getSelectionModel().setSelectionMode(mode);
friendTree.addMouseListener(new TreeMouseListener());
friendTree.setVisible(true);
friendTree.updateUI();
friendTree.repaint();
this.add(friendTree, new XYConstraints(5, 5, 174, 319));
MainFrame.this.repaint(); }
}
protected void printComponent(Graphics g) {

super.printComponent(g);

treeRefresh();
}
}      ...      public void actionPerformed(ActionEvent e) {        centerPanel.repaint();      }}

解决方案 »

  1.   

    centerPanel.repaint()不能刷新树,建议写个DefaultTreeModel类建立树模型,在这里修改树是会自动更新的,另外JTree的updateUI()方法也可以
      

  2.   

    上面代码贴错了:protected void printComponent(Graphics g) {
                
                super.printComponent(g);
                
                treeRefresh();
            }应该是:public void paintComponent(Graphics g) {
                
                super.paintComponent(g);
                
                treeRefresh();
            }
    updateUI()方法没效果,如果用DefaultTreeModel要改不少代码,能不能有改的少一点的办法