gpt4 book ai didi

java - TreeCellRenderer 中的标签宽度不正确

转载 作者:行者123 更新时间:2023-12-01 20:20:29 27 4
gpt4 key购买 nike

我得到了一个带有自定义 TreeCellRendererJTree
该渲染器是一个包含复选框和标签的面板。
虽然每个节点的标签文本都是固定的(在 DefaultMutableTreeNode 的 UserObject 中指定),但该文本可能是粗体,也可能不是粗体。这取决于复选框的状态。
取消选中该复选框后,标签文本不再为粗体,但其宽度保持不变(太宽)。
类似地,当选择复选框时,文本会显示为粗体,但标签不会放大。
这会导致文本被截断。

现实生活中的情况有点复杂,但下面是一个完整的示例。
为了重现该问题:

  • 通过单击文本选择第一个节点
  • 按空格键取消选择该复选框。文本不是粗体,但标签太大。
  • 单击第一个节点上的展开按钮。调整标签宽度
  • 按空格键选择组合框。文本变为粗体,但被截断。

我尝试插入多个对 invalidaterepaint 等的调用,但没有解决问题。
该问题同时出现在默认外观和系统 (Windows) 外观中。

import java.awt.*;
import javax.swing.*;

@SuppressWarnings("serial")
public class TestFrame extends JFrame
{
public TestFrame()
{
getContentPane().setLayout(new GridBagLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Test TreeCellRenderer");

JScrollPane tree_pane;
tree_pane = new JScrollPane();
tree_pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
tree_pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
tree_pane.setPreferredSize(new Dimension(300, 200));

TestTree tree;
tree = new TestTree();
tree_pane.getViewport().add(tree, null);

GridBagConstraints constraints;
constraints = new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.NORTH,
GridBagConstraints.BOTH, new Insets(8, 8, 8, 8), 0, 0);
getContentPane().add(tree_pane, constraints);

pack();
setMinimumSize(getPreferredSize());

}

public static void main(String[] args)
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
TestFrame frame;
frame = new TestFrame();
frame.setVisible(true);
}
catch (Exception exception)
{
exception.printStackTrace();
}

} // main

} // class TestFrame

这个类实现了我的树:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.plaf.basic.BasicTreeUI;
import javax.swing.tree.*;

@SuppressWarnings("serial")
public class TestTree extends JTree
{
// The width of the checkbox within the renderer
// We need it when a node is clicked in order to check what part is exactly underneath the mouse
public int checkboxWidth;

public TestTree()
{
// Initialize object
super(getNodes());
setRootVisible(false);
setShowsRootHandles(true);
setCellRenderer(new MyCellRenderer());
addMouseListener(new TreeMouseManager());
addKeyListener(new TreeKeyManager());

} // constructor

private void toggleCheckBox(TreePath treePath)
{
// Determine node being toggled
Object[] path;
DefaultMutableTreeNode node;
NodeInfo info;
path = treePath.getPath();
node = (DefaultMutableTreeNode)path[path.length - 1];
info = (NodeInfo)node.getUserObject();

// Toggle selection
info.checked = !info.checked;
repaint();

} // toggleCheckBox

private class TreeMouseManager extends MouseAdapter
{
@Override
public void mouseClicked(MouseEvent event)
{
// Determine node corresponding to location
TreePath treePath;
treePath = getPathForLocation(event.getX(), event.getY());
if (treePath == null)
return;

// Manage only single click with left button
if ((event.getClickCount() != 1) || (event.getButton() != MouseEvent.BUTTON1))
return;

// Determine horizontal position of checkbox
BasicTreeUI ui;
int depth;
int leftIndent;
int rightIndent;
int checkboxLeft;
int checkboxRight;
ui = (BasicTreeUI)getUI();
depth = treePath.getPathCount();
leftIndent = ui.getLeftChildIndent();
rightIndent = ui.getRightChildIndent();
checkboxLeft = (depth - 1) * (leftIndent + rightIndent);
checkboxRight = checkboxLeft + checkboxWidth - 1;

// Ignore if not clicked on checkbox
int x;
x = event.getX();
if ((x < checkboxLeft) || (x > checkboxRight))
return;

// Toggle checkbox
toggleCheckBox(treePath);

} // mouseClicked

} // class TreeMouseManager

private class TreeKeyManager extends KeyAdapter
{
@Override
public void keyPressed(KeyEvent event)
{
// Determine selected element
TreePath treePath;
treePath = getSelectionPath();
if (treePath == null)
return;

// Manage event for this element
if (event.getKeyCode() == KeyEvent.VK_SPACE)
toggleCheckBox(treePath);

} // keyPressed

} // class TreeKeyManager

private class MyCellRenderer extends JPanel implements TreeCellRenderer
{
public MyCellRenderer()
{
// Create components
checkbox = new JCheckBox();
checkbox.setBorder(null);
checkbox.setOpaque(false);
label = new JLabel();
label.setBorder(new EmptyBorder(new Insets(0, 2, 0, 2)));

// Initialize panel
GridBagConstraints constraints;
setLayout(new GridBagLayout());
setOpaque(false);
constraints = new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST,
GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0);
add(checkbox, constraints);
constraints = new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST,
GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0);
add(label, constraints);

// Save the width of the checkbox
// We need it when the mouse is clicked on a node
checkboxWidth = (int)checkbox.getPreferredSize().getWidth();

} // constructor

@Override
public Component getTreeCellRendererComponent(JTree tree,
Object value,
boolean selected,
boolean expanded,
boolean leaf,
int row,
boolean hasFocus)
{
// Make data accessible
// Ignore if it's the root node
DefaultMutableTreeNode node;
NodeInfo info;
node = (DefaultMutableTreeNode)value;
if (node.getUserObject() instanceof NodeInfo)
info = (NodeInfo)node.getUserObject();
else
return (this);

// Determine font
Font font;
font = label.getFont();
if (info.checked)
font = font.deriveFont(font.getStyle() | Font.BOLD);
else
font = font.deriveFont(font.getStyle() & ~Font.BOLD);

// Configure components
checkbox.setSelected(info.checked);
label.setText(info.name);
label.setOpaque(selected);
label.setFont(font);
if (selected)
{
label.setBackground(SystemColor.textHighlight);
label.setForeground(SystemColor.textHighlightText);
}
else
{
label.setBackground(SystemColor.text);
label.setForeground(SystemColor.textText);
}

// Make sure everything is painted correctly
label.invalidate();
checkbox.invalidate();
invalidate();

// Done
return (this);

} // getTreeCellRendererComponent

private JCheckBox checkbox;
private JLabel label;

} // class MyCellRenderer

private static DefaultMutableTreeNode getNodes()
{
// Create root
DefaultMutableTreeNode root;
root = new DefaultMutableTreeNode("root");

// Create first level children
DefaultMutableTreeNode first;
DefaultMutableTreeNode second;
DefaultMutableTreeNode third;
NodeInfo info;
info = new NodeInfo();
info.name = "This is the first node";
info.checked = true;
first = new DefaultMutableTreeNode(info);
info = new NodeInfo();
info.name = "And this is the second";
info.checked = false;
second = new DefaultMutableTreeNode(info);
info = new NodeInfo();
info.name = "Finally, the third";
info.checked = false;
third = new DefaultMutableTreeNode(info);
root.add(first);
root.add(second);
root.add(third);

// Add second level children
info = new NodeInfo();
info.name = "Second level node";
info.checked = true;
first.add(new DefaultMutableTreeNode(info));
info = new NodeInfo();
info.name = "This is another one";
info.checked = false;
first.add(new DefaultMutableTreeNode(info));
info = new NodeInfo();
info.name = "And this is the last one";
info.checked = true;
first.add(new DefaultMutableTreeNode(info));

// Done
return (root);

} // getNodes

private static class NodeInfo
{
public String name;
public boolean checked;
}

} // class TestTree

更新
getTreeCellRendererComponent 中,我尝试获取首选大小。
他们看起来还不错。选择该复选框时,标签和面板本身的首选尺寸都会增加。取消选择该复选框时,它们会减少。

最佳答案

感谢此问题的回答Change JTree row height resizing behavior when rendering ,我自己解决了这个问题:

  private void toggleCheckBox(TreePath treePath)
{
// Determine node being toggled
Object[] path;
DefaultMutableTreeNode node;
NodeInfo info;
path = treePath.getPath();
node = (DefaultMutableTreeNode)path[path.length - 1];
info = (NodeInfo)node.getUserObject();

// Toggle selection
info.checked = !info.checked;

// Make sure tree recalculates width of the nodes
BasicTreeUI ui = (BasicTreeUI)getUI();
try
{
Method method = BasicTreeUI.class.getDeclaredMethod("configureLayoutCache");
method.setAccessible(true);
method.invoke(ui);
}
catch (Exception e1)
{
e1.printStackTrace();
}

} // toggleCheckBox

关于java - TreeCellRenderer 中的标签宽度不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58937928/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com