gpt4 book ai didi

java - 使用包含可编辑 JTextArea 的 JPanel 呈现 JTree 叶

转载 作者:行者123 更新时间:2023-11-29 09:11:13 26 4
gpt4 key购买 nike

我正在尝试呈现一个 JTree 叶以具有包含可编辑 JTextArea 的 JPanel,以便用户可以输入文本并能够在添加文本时向下滚动,

并且还使用包装器来限制文本的宽度,使其向下延伸以留出更多空间,而不是左右滚动。

到目前为止,我已经实现了以下内容,但它看起来很奇怪,当我运行代码时,好像有两片叶子,默认的和渲染的。非常感谢任何类型的帮助。

我只想知道我想做的事情是否可以实现,如果不能,我可以在哪里修改。

我不想更改图标,只更改叶子。

import java.awt.Component;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.ItemEvent;
import java.util.EventObject;
import javax.swing.*;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellEditor;
import javax.swing.tree.TreeCellRenderer;

/**
*
* @author Razaractor
*/
class LeafRenderer extends AbstractCellEditor implements
TreeCellRenderer, TreeCellEditor {

//Text area to be place on JPanel
JTextArea text = new JTextArea();
DefaultTreeCellRenderer nonLeafRenderer = new DefaultTreeCellRenderer();

@Override
public Object getCellEditorValue() {
return text.getText();
}

@Override
public boolean isCellEditable(final EventObject event) {
return true;
}

@Override
public Component getTreeCellRendererComponent(
final JTree tree, final Object value,
final boolean selected,
final boolean expanded,
final boolean leaf,
final int row,
final boolean hasFocus) {
//Default leaf
Component render = nonLeafRenderer.getTreeCellRendererComponent(tree,
value, selected, expanded, leaf, row, hasFocus);
if (selected && leaf) {
final JPanel panel = new JPanel();
BoxLayout layout = new BoxLayout(panel, BoxLayout.X_AXIS);
panel.setLayout(layout);
text.setColumns(20);
text.setLineWrap(true);
text.setRows(5);
text.setWrapStyleWord(true);
text = new JTextArea(String.valueOf(value));
text.addComponentListener(new ComponentListener() {

@Override
public void componentResized(ComponentEvent e) {
panel.setSize(panel.getPreferredSize());
}

@Override
public void componentShown(ComponentEvent e) {
}

@Override
public void componentMoved(ComponentEvent e) {
}

@Override
public void componentHidden(ComponentEvent e) {
}
});
JScrollPane scroll = new JScrollPane(text);
panel.add(scroll);
render = panel;
return panel;
}
return render;
}

@Override
public Component getTreeCellEditorComponent(
final JTree tree,
final Object value,
final boolean isSelected,
final boolean expanded,
final boolean leaf,
final int row) {
Component editor = nonLeafRenderer.getTreeCellRendererComponent(tree,
value, true, expanded, leaf, row, true);
final JPanel p = new JPanel();
if (editor instanceof JPanel) {
editor = (JPanel) editor;
}
return editor;
}

public void itemStateChanged(ItemEvent itemEvent) {
if (stopCellEditing()) {
fireEditingStopped();
}
}
}

enter image description here

enter image description here

最佳答案

我不知道您真正想要实现什么,但请尝试这些更改。

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.ItemEvent;
import java.util.EventObject;
import javax.swing.*;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellEditor;
import javax.swing.tree.TreeCellRenderer;

/**
*
* @author Razaractor
*/
class LeafRenderer extends AbstractCellEditor implements
TreeCellRenderer, TreeCellEditor {

//Text area to be place on JPanel
JTextArea text = new JTextArea();
DefaultTreeCellRenderer nonLeafRenderer = new DefaultTreeCellRenderer();

@Override
public Object getCellEditorValue() {
return text.getText();
}

@Override
public boolean isCellEditable(final EventObject event) {
return true;
}

@Override
public Component getTreeCellRendererComponent(
final JTree tree, final Object value,
final boolean selected,
final boolean expanded,
final boolean leaf,
final int row,
final boolean hasFocus) {
//Default leaf
Component render = nonLeafRenderer.getTreeCellRendererComponent(tree,
value, selected, expanded, leaf, row, hasFocus);
if (selected && leaf) {
final JPanel panel = new JPanel();
BoxLayout layout = new BoxLayout(panel, BoxLayout.X_AXIS);
panel.setLayout(layout);
//Changed code
//moved from
text = new JTextArea(String.valueOf(value));
text.setColumns(20);
text.setLineWrap(true);
text.setRows(5);
text.setWrapStyleWord(true);
//here. You were creating just ordinary JTextArea.
text.addComponentListener(new ComponentListener() {

@Override
public void componentResized(ComponentEvent e) {
panel.setSize(panel.getPreferredSize());
}

@Override
public void componentShown(ComponentEvent e) {
}

@Override
public void componentMoved(ComponentEvent e) {
}

@Override
public void componentHidden(ComponentEvent e) {
}
});
JScrollPane scroll = new JScrollPane(text);
//changed Code
//if you want to hide scrollbars uncomment one of these
//scroll.getVerticalScrollBar().setPreferredSize(new Dimension(0, 0));
//scroll.getHorizontalScrollBar().setPreferredSize(new Dimension(0, 0));
panel.add(scroll);
render = panel;
return panel;
}
return render;
}

@Override
public Component getTreeCellEditorComponent(
final JTree tree,
final Object value,
final boolean isSelected,
final boolean expanded,
final boolean leaf,
final int row) {
Component editor = nonLeafRenderer.getTreeCellRendererComponent(tree,
value, true, expanded, leaf, row, true);
final JPanel p = new JPanel();
if (editor instanceof JPanel) {
editor = (JPanel) editor;
}
return editor;
}

public void itemStateChanged(ItemEvent itemEvent) {
if (stopCellEditing()) {
fireEditingStopped();
}
}
}

关于java - 使用包含可编辑 JTextArea 的 JPanel 呈现 JTree 叶,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12342440/

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