I need to display account names for my program and I want to do this using JTree inside a JScrollPane.
我需要为我的程序显示帐户名称,我想在JScrollPane中使用JTree来完成这个操作。
Here is my code:
这是我的代码:
public void loadAccounts() {
accountsRoot = new DefaultMutableTreeNode("Accounts"); //create root
accountsRoot.add(new DefaultMutableTreeNode("Fred")); //add one element
//for testing
accounts = new JTree(accountsRoot);
accountsPane = new JScrollPane(accounts);
accountsPane.add(accounts); //don't think this is necessary
canvas.add(accountsPane);
accounts.setBounds(0, 0, accountsPane.getWidth(), accountsPane.getHeight());
accountsPane.setBounds(460, 270, 240, 410);
accounts.setVisible(true);
accountsPane.setVisible(true);
}
Because I am not using a layout I set the bounds manually.
因为我没有使用布局,所以我手动设置边界。
I can't seem to get it to show. I want to eventually end up loading the accounts from a while so I figure JTree would be pretty easy for that,
我好像看不出来。我想最终从一段时间开始加载这些账户所以我认为JTree会很简单,
1 个解决方案
#1
3
accountsPane = new JScrollPane(accounts);
accountsPane.add(accounts); //don't think this is necessary
Not only is that not necessary but it will mess things up as this in effect adds your accounts JTree to multiple containers -- to the JScrollPane's viewport (good) and to the JScrollPane itself (bad). Don't do that. Add it to the JScrollPane's viewport only either through the JScrollPane's constructor as shown on the first line above, or by calling setViewportView(...)
on the JScrollPane object after creating it.
这不仅是不必要的,而且还会把事情弄糟,因为这实际上将您的帐户JTree添加到多个容器中——添加到JScrollPane的viewport(很好)和JScrollPane本身(很糟糕)。不要这样做。仅通过JScrollPane的构造函数(如上面第一行所示)将其添加到JScrollPane的viewport,或者在创建JScrollPane对象之后调用setViewportView(…)。
Edit: another problem is your use of setBounds(...)
. You shouldn't be doing this but rather should be using layout managers to allow the proper viewing of your components. You will also need to call revalidate()
and repaint()
on whatever container is accepting the JScrollPane.
编辑:另一个问题是您使用setBounds(…)。您不应该这样做,而是应该使用布局管理器来允许正确地查看组件。您还需要在接受JScrollPane的任何容器上调用revalidate()和repaint()。
#1
3
accountsPane = new JScrollPane(accounts);
accountsPane.add(accounts); //don't think this is necessary
Not only is that not necessary but it will mess things up as this in effect adds your accounts JTree to multiple containers -- to the JScrollPane's viewport (good) and to the JScrollPane itself (bad). Don't do that. Add it to the JScrollPane's viewport only either through the JScrollPane's constructor as shown on the first line above, or by calling setViewportView(...)
on the JScrollPane object after creating it.
这不仅是不必要的,而且还会把事情弄糟,因为这实际上将您的帐户JTree添加到多个容器中——添加到JScrollPane的viewport(很好)和JScrollPane本身(很糟糕)。不要这样做。仅通过JScrollPane的构造函数(如上面第一行所示)将其添加到JScrollPane的viewport,或者在创建JScrollPane对象之后调用setViewportView(…)。
Edit: another problem is your use of setBounds(...)
. You shouldn't be doing this but rather should be using layout managers to allow the proper viewing of your components. You will also need to call revalidate()
and repaint()
on whatever container is accepting the JScrollPane.
编辑:另一个问题是您使用setBounds(…)。您不应该这样做,而是应该使用布局管理器来允许正确地查看组件。您还需要在接受JScrollPane的任何容器上调用revalidate()和repaint()。