- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前在使用 JTree 的单元格编辑器时遇到问题。我的 JTree 中有两个不同的对象:Users 和 Books。
所以用户是 Bianca,书籍是节点。这些节点内的标签(眼睛、油漆标签和垃圾桶)也是可单击的。但是,当我设置单元格编辑器时,它给了我一个空异常。我认为这是因为我正在使用两个不同的对象,所以我无法正确设置单元格编辑器。这是我认为我有问题的具体方法:
public Component getTreeCellEditorComponent(JTree tree, Object value, boolean isSelected, boolean expanded,
boolean leaf, int row) {
Component returnValue = null;
System.out.println("loo");
if ((value != null) && (value instanceof DefaultMutableTreeNode)) {
Object userObject = ((DefaultMutableTreeNode) value)
.getUserObject();
System.out.println("b");
if(userObject instanceof Book){
Book feature = (Book) userObject;
title.setText(feature.getTitulo());
renderer.setEnabled(tree.isEnabled());
returnValue = renderer;
System.out.println("hi!!");
}
}
if (returnValue == null) {
System.out.println("hi1");
returnValue = defaultEditor.getTreeCellEditorComponent(tree, value, isSelected, expanded, leaf, row);
System.out.println(defaultEditor);
}
return returnValue;
}
当节点类型不是Book时,我不知道如何返回DefaultCellEditor。
如果有人想复制这个问题,下面是我的完整代码:
类书籍:
public class Book {
String titulo;
public String getTitulo() {
return titulo;
}
public void setTitulo(String titulo) {
this.titulo = titulo;
}
public Book(String titulo){
this.titulo = titulo;
}
}
类用户
public class User {
String nomeUser;
public String getNomeUser() {
return nomeUser;
}
public void setNomeUser(String nomeUser) {
this.nomeUser = nomeUser;
}
public User(String nome){
this.nomeUser = nome;
}
}
Render类(用于设置JTree的单元格编辑器和单元格渲染)
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import javax.swing.AbstractCellEditor;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellEditor;
import javax.swing.tree.TreeCellRenderer;
public class Render{
public static class NodeCellRenderer implements TreeCellRenderer{
private JLabel nomeRadar;
private JPanel renderer;
private JLabel btnVisible;
private JLabel btnPaint;
private JLabel btnTrash;
DefaultTreeCellRenderer defaultRenderer = new DefaultTreeCellRenderer();
public NodeCellRenderer() throws IOException{
renderer = new JPanel(new FlowLayout(5, 5,5));
nomeRadar = new JLabel();
renderer.add(nomeRadar);
btnVisible = new JLabel();
btnPaint = new JLabel();
btnTrash = new JLabel();
InputStream visible = getClass().getResourceAsStream("/eye_visible-16.png");
InputStream invisible = getClass().getResourceAsStream("/eye_invisible-16.png");
InputStream paint = getClass().getResourceAsStream("/paint-16.png");
InputStream trash = getClass().getResourceAsStream("/trash-16.png");
btnVisible.setIcon(new ImageIcon(ImageIO.read(visible)));
btnPaint.setIcon(new ImageIcon(ImageIO.read(paint)));
btnTrash.setIcon(new ImageIcon(ImageIO.read(trash)));
nomeRadar.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
System.out.println("clicou no nome");
}
});
btnVisible.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
System.out.println("clicou no olho");
}
});
btnPaint.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
System.out.println("clicou no paint");
}
});
btnTrash.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
System.out.println("clicou no trash");
}
});
renderer.add(nomeRadar);
renderer.add(btnVisible);
renderer.add(btnPaint);
renderer.add(btnTrash);
}
@Override
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf,
int row, boolean hasFocus) {
Component returnValue = null;
// TODO Auto-generated method stub
if ((value != null) && (value instanceof DefaultMutableTreeNode)) {
Object userObject = ((DefaultMutableTreeNode) value)
.getUserObject();
if(userObject instanceof Book){
Book feature = (Book) userObject;
nomeRadar.setText(feature.getTitulo());
renderer.setEnabled(tree.isEnabled());
returnValue = renderer;
}
}
if (returnValue == null) {
returnValue = defaultRenderer.getTreeCellRendererComponent(tree,
value, selected, expanded, leaf, row, hasFocus);
}
return returnValue;
}
}
public static class ButtonCellEditor extends AbstractCellEditor implements TreeCellEditor, ActionListener, MouseListener
{
/**
*
*/
private static final long serialVersionUID = 1L;
TreeCellEditor defaultEditor = JTreeOfObjects.currentStyle;
JLabel title;
JPanel renderer;
JLabel btnVisible;
JLabel btnPaint;
JLabel btnTrash;
private Object value;
public ButtonCellEditor() throws IOException{
renderer = new JPanel(new FlowLayout(5, 5,5));
System.out.println("ulo");
title = new JLabel();
renderer.add(title);
btnVisible = new JLabel();
btnPaint = new JLabel();
btnTrash = new JLabel();
InputStream visible = getClass().getResourceAsStream("/eye_visible-16.png");
InputStream invisible = getClass().getResourceAsStream("/eye_invisible-16.png");
InputStream paint = getClass().getResourceAsStream("/paint-16.png");
InputStream trash = getClass().getResourceAsStream("/trash-16.png");
btnVisible.setIcon(new ImageIcon(ImageIO.read(visible)));
btnPaint.setIcon(new ImageIcon(ImageIO.read(paint)));
btnTrash.setIcon(new ImageIcon(ImageIO.read(trash)));
System.out.println("nossa 1");
title.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
System.out.println("clicou no nome");
stopCellEditing();
}
});
btnVisible.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
System.out.println("clicou no olho");
stopCellEditing();
}
});
btnPaint.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
System.out.println("clicou no paint");
stopCellEditing();
}
});
btnTrash.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
System.out.println("clicou no trash");
stopCellEditing();
}
});
renderer.add(title);
renderer.add(btnVisible);
renderer.add(btnPaint);
renderer.add(btnTrash);
}
@Override
public Object getCellEditorValue() {
// TODO Auto-generated method stub
//return value.toString();
return null;
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("clicou no b");
stopCellEditing();
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("clicou no d");
stopCellEditing();
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println("clicou no a");
stopCellEditing();
}
@Override
public Component getTreeCellEditorComponent(JTree tree, Object value, boolean isSelected, boolean expanded,
boolean leaf, int row) {
Component returnValue = null;
System.out.println("loo");
if ((value != null) && (value instanceof DefaultMutableTreeNode)) {
Object userObject = ((DefaultMutableTreeNode) value)
.getUserObject();
System.out.println("b");
if(userObject instanceof Book){
Book feature = (Book) userObject;
title.setText(feature.getTitulo());
renderer.setEnabled(tree.isEnabled());
returnValue = renderer;
System.out.println("hi!!");
}
}
if (returnValue == null) {
System.out.println("hi1");
returnValue = defaultEditor.getTreeCellEditorComponent(tree, value, isSelected, expanded, leaf, row);
System.out.println(defaultEditor);
}
return returnValue;
}
}
}
JTreeOfObjects 类
public class JTreeOfObjects {
public static TreeCellRenderer renderer;
private JFrame frame;
public static JTree tree;
private DefaultTreeModel model;
private DefaultMutableTreeNode parent;
JLabel nomeRadar;
JLabel btnVisible;
JLabel btnPaint;
JLabel btnTrash;
public static TreeCellEditor currentStyle;
public void initialize() throws IOException{
frame = new JFrame("Árvore de Navegação");
parent = new DefaultMutableTreeNode("Library",true);
//DefaultMutableTreeNode undefined = new DefaultMutableTreeNode("Indefinida");
//undefined.add(new DefaultMutableTreeNode("!"));
System.out.println(parent.getUserObject());
//parent.add(undefined);
tree = new JTree(parent);
System.out.println("Current"+tree.getCellEditor());
//currentStyle = tree.getCellEditor();
renderer = new Render.NodeCellRenderer();
model = (DefaultTreeModel) tree.getModel();
tree.setEditable(true);
tree.setCellRenderer(renderer);
tree.setCellEditor(new Render.ButtonCellEditor());
//frame.getContentPane().add(tree);
nomeRadar = new JLabel();
btnVisible = new JLabel();
btnPaint = new JLabel();
btnTrash = new JLabel();
InputStream visible = getClass().getResourceAsStream("/eye_visible-16.png");
InputStream invisible = getClass().getResourceAsStream("/eye_invisible-16.png");
InputStream paint = getClass().getResourceAsStream("/paint-16.png");
InputStream trash = getClass().getResourceAsStream("/trash-16.png");
btnVisible.setIcon(new ImageIcon(ImageIO.read(visible)));
btnPaint.setIcon(new ImageIcon(ImageIO.read(paint)));
btnTrash.setIcon(new ImageIcon(ImageIO.read(trash)));
frame.getContentPane().add(btnPaint);
frame.getContentPane().add(btnVisible);
frame.getContentPane().add(btnTrash);
frame.getContentPane().add(tree);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setUndecorated(true);
frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
frame.setSize(200, 200);
frame.setVisible(true);
}
public void adicionarNoCamada(String nomePessoa){
//DefaultTreeModel model = (DefaultTreeModel)tree.getModel();
DefaultMutableTreeNode noCamada = new DefaultMutableTreeNode(nomePessoa);
parent.add(noCamada);
}
public void adicionarLivro(String nomeCamada,Book feature) throws IOException{
//tree.get
DefaultMutableTreeNode node = null;
Enumeration e = parent.breadthFirstEnumeration();
while(e.hasMoreElements()){
node = (DefaultMutableTreeNode) e.nextElement();
if(nomeCamada.equals(node.getUserObject().toString())){
node.add(new DefaultMutableTreeNode(feature));
break;
}
else{
}
}
tree.setCellEditor(new Render.ButtonCellEditor());
}
}
类主应用
public class MainApp {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Book b1 = new Book("The Book of Life");
Book b2 = new Book ("The Book of Death");
User u1 = new User("Bianca");
JTreeOfObjects object = new JTreeOfObjects();
object.initialize();
object.adicionarNoCamada(u1.getNomeUser());
object.adicionarLivro(u1.getNomeUser(), b1);
object.adicionarLivro(u1.getNomeUser(), b2);
}
}
任何帮助将不胜感激。它不是重复的,因为我知道如何修复 NullPointer 异常(必须实例化对象)。我只是不知道在 TreeCellEditor 的情况下要返回什么,如何返回其默认值。
最佳答案
您可能需要覆盖 CellEditor#isCellEditable(...)
和 CellEditor#getCellEditorValue()
,但我不确定这会给您带来您想要的结果.
@Override public boolean isCellEditable(EventObject e) {
Object source = e.getSource();
if (!(source instanceof JTree) || !(e instanceof MouseEvent)) {
return false;
}
JTree tree = (JTree) source;
Point p = ((MouseEvent) e).getPoint();
Object node = tree.getPathForLocation(p.x, p.y).getLastPathComponent();
if (!(node instanceof DefaultMutableTreeNode)) {
return false;
}
Object userObject = ((DefaultMutableTreeNode) node).getUserObject();
if (!(userObject instanceof Book)) {
return false;
}
return super.isCellEditable(e);
}
@Override
public Object getCellEditorValue() {
return new Book(renderer.nomeRadar.getText());
}
这是一个简单的示例:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.tree.*;
public class MainApp2 {
public JComponent makeUI() {
JTree tree = new JTree(makeModel());
tree.setEditable(true);
tree.setCellRenderer(new NodeCellRenderer());
tree.setCellEditor(new ButtonCellEditor());
JPanel p = new JPanel(new BorderLayout());
p.add(new JScrollPane(tree));
return p;
}
private static DefaultMutableTreeNode makeModel() {
Book b1 = new Book("The Book of Life");
Book b2 = new Book("The Book of Death");
User u1 = new User("Bianca");
DefaultMutableTreeNode noCamada = new DefaultMutableTreeNode(u1);
noCamada.add(new DefaultMutableTreeNode(b1));
noCamada.add(new DefaultMutableTreeNode(b2));
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Library", true);
root.add(noCamada);
return root;
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
JFrame f = new JFrame("Arvore de Navegacao");
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new MainApp2().makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
});
}
}
class NodePanel extends JPanel {
public final JLabel nomeRadar = new JLabel();
public final JLabel btnVisible = new JLabel(new ColorIcon(Color.RED));
public final JLabel btnPaint = new JLabel(new ColorIcon(Color.GREEN));
public final JLabel btnTrash = new JLabel(new ColorIcon(Color.BLUE));
protected NodePanel() {
super(new FlowLayout(FlowLayout.LEADING, 5, 5));
add(nomeRadar);
add(btnVisible);
add(btnPaint);
add(btnTrash);
}
}
class NodeCellRenderer implements TreeCellRenderer {
private final DefaultTreeCellRenderer defaultRenderer = new DefaultTreeCellRenderer();
private final NodePanel renderer = new NodePanel();
@Override public Component getTreeCellRendererComponent(
JTree tree, Object value, boolean selected, boolean expanded,
boolean leaf, int row, boolean hasFocus) {
Component returnValue = null;
if (value instanceof DefaultMutableTreeNode) {
Object userObject = ((DefaultMutableTreeNode) value).getUserObject();
if (userObject instanceof Book) {
Book feature = (Book) userObject;
renderer.nomeRadar.setText(feature.getTitulo());
returnValue = renderer;
}
}
if (returnValue == null) {
returnValue = defaultRenderer.getTreeCellRendererComponent(
tree, value, selected, expanded, leaf, row, hasFocus);
}
return returnValue;
}
}
class ButtonCellEditor extends AbstractCellEditor implements TreeCellEditor {
private final DefaultTreeCellRenderer defaultRenderer = new DefaultTreeCellRenderer();
private final NodePanel renderer = new NodePanel();
public ButtonCellEditor() {
renderer.setBackground(Color.ORANGE);
renderer.nomeRadar.addMouseListener(new MouseAdapter() {
@Override public void mousePressed(MouseEvent e) {
System.out.println("clicou no nome");
stopCellEditing();
}
});
renderer.btnVisible.addMouseListener(new MouseAdapter() {
@Override public void mousePressed(MouseEvent e) {
System.out.println("clicou no olho");
stopCellEditing();
}
});
renderer.btnPaint.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
System.out.println("clicou no paint");
stopCellEditing();
}
});
renderer.btnTrash.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
System.out.println("clicou no trash");
stopCellEditing();
}
});
renderer.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
System.out.println("renderer");
stopCellEditing();
}
});
}
@Override public boolean isCellEditable(EventObject e) {
Object source = e.getSource();
if (!(source instanceof JTree) || !(e instanceof MouseEvent)) {
return false;
}
JTree tree = (JTree) source;
Point p = ((MouseEvent) e).getPoint();
TreePath path = tree.getPathForLocation(p.x, p.y);
if (Objects.isNull(path)) {
return false;
}
Object node = path.getLastPathComponent();
if (!(node instanceof DefaultMutableTreeNode)) {
return false;
}
Object userObject = ((DefaultMutableTreeNode) node).getUserObject();
if (!(userObject instanceof Book)) {
return false;
}
return super.isCellEditable(e);
}
@Override
public Object getCellEditorValue() {
return new Book(renderer.nomeRadar.getText());
}
@Override
public Component getTreeCellEditorComponent(
JTree tree, Object value, boolean isSelected, boolean expanded,
boolean leaf, int row) {
Component returnValue = null;
if (value instanceof DefaultMutableTreeNode) {
Object userObject = ((DefaultMutableTreeNode) value).getUserObject();
if (userObject instanceof Book) {
Book feature = (Book) userObject;
renderer.nomeRadar.setText(feature.getTitulo());
returnValue = renderer;
}
}
if (returnValue == null) {
returnValue = defaultRenderer.getTreeCellRendererComponent(
tree, value, isSelected, expanded, leaf, row, true);
}
return returnValue;
}
}
class Book {
String titulo;
public String getTitulo() {
return titulo;
}
public void setTitulo(String titulo) {
this.titulo = titulo;
}
public Book(String titulo) {
this.titulo = titulo;
}
}
class User {
String nomeUser;
public String getNomeUser() {
return nomeUser;
}
public void setNomeUser(String nomeUser) {
this.nomeUser = nomeUser;
}
public User(String nome) {
this.nomeUser = nome;
}
@Override public String toString() {
return nomeUser;
}
}
class ColorIcon implements Icon {
private final Color color;
protected ColorIcon(Color color) {
this.color = color;
}
@Override public void paintIcon(Component c, Graphics g, int x, int y) {
Graphics2D g2 = (Graphics2D) g.create();
g2.translate(x, y);
g2.setPaint(color);
g2.fillRect(0, 0, getIconWidth(), getIconHeight());
g2.dispose();
}
@Override public int getIconWidth() {
return 16;
}
@Override public int getIconHeight() {
return 16;
}
}
关于java - 在同一 JTree 中使用两个不同对象时,JTree 和 CellEditor 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42819453/
SQLite、Content provider 和 Shared Preference 之间的所有已知区别。 但我想知道什么时候需要根据情况使用 SQLite 或 Content Provider 或
警告:我正在使用一个我无法完全控制的后端,所以我正在努力解决 Backbone 中的一些注意事项,这些注意事项可能在其他地方更好地解决......不幸的是,我别无选择,只能在这里处理它们! 所以,我的
我一整天都在挣扎。我的预输入搜索表达式与远程 json 数据完美配合。但是当我尝试使用相同的 json 数据作为预取数据时,建议为空。点击第一个标志后,我收到预定义消息“无法找到任何内容...”,结果
我正在制作一个模拟 NHL 选秀彩票的程序,其中屏幕右侧应该有一个 JTextField,并且在左侧绘制弹跳的选秀球。我创建了一个名为 Ball 的类,它实现了 Runnable,并在我的主 Draf
这个问题已经有答案了: How can I calculate a time span in Java and format the output? (18 个回答) 已关闭 9 年前。 这是我的代码
我有一个 ASP.NET Web API 应用程序在我的本地 IIS 实例上运行。 Web 应用程序配置有 CORS。我调用的 Web API 方法类似于: [POST("/API/{foo}/{ba
我将用户输入的时间和日期作为: DatePicker dp = (DatePicker) findViewById(R.id.datePicker); TimePicker tp = (TimePic
放宽“邻居”的标准是否足够,或者是否有其他标准行动可以采取? 最佳答案 如果所有相邻解决方案都是 Tabu,则听起来您的 Tabu 列表的大小太长或您的释放策略太严格。一个好的 Tabu 列表长度是
我正在阅读来自 cppreference 的代码示例: #include #include #include #include template void print_queue(T& q)
我快疯了,我试图理解工具提示的行为,但没有成功。 1. 第一个问题是当我尝试通过插件(按钮 1)在点击事件中使用它时 -> 如果您转到 Fiddle,您会在“内容”内看到该函数' 每次点击都会调用该属
我在功能组件中有以下代码: const [ folder, setFolder ] = useState([]); const folderData = useContext(FolderContex
我在使用预签名网址和 AFNetworking 3.0 从 S3 获取图像时遇到问题。我可以使用 NSMutableURLRequest 和 NSURLSession 获取图像,但是当我使用 AFHT
我正在使用 Oracle ojdbc 12 和 Java 8 处理 Oracle UCP 管理器的问题。当 UCP 池启动失败时,我希望关闭它创建的连接。 当池初始化期间遇到 ORA-02391:超过
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve
引用这个plunker: https://plnkr.co/edit/GWsbdDWVvBYNMqyxzlLY?p=preview 我在 styles.css 文件和 src/app.ts 文件中指定
为什么我的条形这么细?我尝试将宽度设置为 1,它们变得非常厚。我不知道还能尝试什么。默认厚度为 0.8,这是应该的样子吗? import matplotlib.pyplot as plt import
当我编写时,查询按预期执行: SELECT id, day2.count - day1.count AS diff FROM day1 NATURAL JOIN day2; 但我真正想要的是右连接。当
我有以下时间数据: 0 08/01/16 13:07:46,335437 1 18/02/16 08:40:40,565575 2 14/01/16 22:2
一些背景知识 -我的 NodeJS 服务器在端口 3001 上运行,我的 React 应用程序在端口 3000 上运行。我在 React 应用程序 package.json 中设置了一个代理来代理对端
我面临着一个愚蠢的问题。我试图在我的 Angular 应用程序中延迟加载我的图像,我已经尝试过这个2: 但是他们都设置了 src attr 而不是 data-src,我在这里遗漏了什么吗?保留 d
我是一名优秀的程序员,十分优秀!