Java Swing:如何定义 JTree 如何显示“用户对象”?

发布于 2024-08-27 07:02:46 字数 239 浏览 21 评论 0原文

使用JTree时,可以设置DefaultMutableTreeNode的“用户对象”。它可以是任何类型,但要显示它,需要使用其 toString() 值。这不是我需要的。

如何更改用户对象的显示方式?

注意:我的用户对象必须String不同才能维护树和用户对象之间的映射。

When using a JTree, a "user object" of a DefaultMutableTreeNode can be set. This can be of any kind, but to display it, its toString() value is used. This is not what I need.

How can I change the way a user object is displayed?

NOTE: My user object has to be something different than a String to be able to maintain mapping between the tree and the user objects.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

飘逸的'云 2024-09-03 07:02:46

我不明白你有什么问题。

DefaultMutableTreeNode 将在用户对象上使用 toString 方法,因为它有意义。 JTree 需要字符串来绘制对象,因此向您的对象询问其字符串表示形式就可以了。

如果您确实需要避免在对象上调用 toString ,您将需要一种方法来提供它的字符串表示形式,但您必须编写自己的 MutableTreeNode

class MyTreeNode implements MutableTreeNode
{
  UserObject yourObject;

  MyTreeNode(UserObject yourObject)
  {
    this.yourObject = yourObject;
  }

  // implement all needed methods to handle children and so on

  public String toString()
  {
    // then you can avoid using toString
    return yourObject.sringRapresentation();
  }
}

但是我真的不明白这样做的意义..此外,您可以尝试通过覆盖 toString 方法来扩展 DefaultMutableTreeNode ,但是您将需要对对象的额外引用或者需要一些沮丧

如果您确实需要与字符串不同的可视化效果,则必须编写自己的实现 TableCellRenderer 的渲染。

I don't get what's your problem.

The DefaultMutableTreeNode will use the toString method on the user object because it makes sense. The JTree needs strings to draw objects so asking to your object its string rapresentation is ok.

If you really need to avoid calling toString on your object you will need a way to provide a string rapresentation of it anyway, but you will have to write your own MutableTreeNode:

class MyTreeNode implements MutableTreeNode
{
  UserObject yourObject;

  MyTreeNode(UserObject yourObject)
  {
    this.yourObject = yourObject;
  }

  // implement all needed methods to handle children and so on

  public String toString()
  {
    // then you can avoid using toString
    return yourObject.sringRapresentation();
  }
}

But I really don't see the point of doing this.. in addition you can try extending the DefaultMutableTreeNode by overriding toString method, but you will need an additional reference to your object or some downcasts will be needed.

If you really need a different visualization than a string you will have to write your own rendered that implements TableCellRenderer.

江挽川 2024-09-03 07:02:46

重写用户对象上的 toString() 或提供 TreeCellRenderer基本示例

Override toString() on your user object OR provide a TreeCellRenderer, basic example

沦落红尘 2024-09-03 07:02:46

如果您只关心为用户对象显示的文本并且不想打扰 TreeCellRender,则还有另一种选择:扩展 JTree 并覆盖 convertValueToText 使用您自己的代码为该对象创建一个描述性字符串。

Another alternative if you just care about the text shown for the user object and don't want to bother with TreeCellRender: extend JTree and override convertValueToText with your own code that creates a descriptive string for that object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文