[英]get node value from JTree and display it in JTable using JButton
I know it's simple and stupid questions, since I am newbie don't know how to link. 我知道这是一个简单而愚蠢的问题,因为我是新手,所以不知道如何链接。
I am trying to get a node value (file with path), and to push the node value into JTable using JButton - 'Add Files', have posted the code so far have tried, it doesn't give any error but it is not performing what I wanted, please give me directions on this, thanks. 我正在尝试获取节点值 (带有路径的文件),并使用JButton-“添加文件”将节点值推入JTable中,到目前为止已发布了代码,但并未给出任何错误,但事实并非如此。执行我想要的内容,谢谢。
How to get a node value from one function to another function, thanks 如何从一个函数到另一个函数获取节点值,谢谢
obtaining node value from JTree 从JTree获取节点值
File root = new File(System.getProperty("user.home"));
FileTreeModel model = new FileTreeModel(root);
JTree tree = new JTree(model);
JScrollPane scrollpane = new JScrollPane(tree);
scrollpane.setBounds(10, 9, 304, 730);
frmViperManufacturingRecord.getContentPane().add(scrollpane);
tree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
File node = (File)e.getPath().getLastPathComponent();
//System.out.println("You selected " + node);
if (!node.isFile()) {
JFrame frame = new JFrame();
JOptionPane.showConfirmDialog(frame, "Please select the valid file to Add Files", "Not Valid File",JOptionPane.PLAIN_MESSAGE );
//if (result == JOptionPane.CANCEL_OPTION);
}
//else
}
});
trying push the node value into JTable 尝试将节点值推入JTable
//table just below Add Files button
table_2 = new JTable();
table_2.setBounds(324, 43, 713, 121);
frmViperManufacturingRecord.getContentPane().add(table_2);
using JButton 使用JButton
// Add files button
JButton btnAddFiles_1 = new JButton("Add Files");
btnAddFiles_1.setMnemonic('A');
btnAddFiles_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
Object rowData[][] = { { "01.", node, }};
Object columnNames[] = { "Sl.no", "File name"};
JTable table = new JTable(rowData, columnNames);
}
});
btnAddFiles_1.setFont(new Font("Calibri", Font.BOLD, 12));
btnAddFiles_1.setBounds(324, 9, 89, 23);
frmViperManufacturingRecord.getContentPane().add(btnAddFiles_1);
Here: 这里:
btnAddFiles_1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
Object rowData[][] = { { "01.", node, }};
Object columnNames[] = { "Sl.no", "File name"};
JTable table = new JTable(rowData, columnNames); // new JTable that is not placed in the content pane
}
});
Don't re-create the table itself as table_2
has been created and placed already. 不要重新创建表本身,因为已经创建并放置了table_2
。 Update its table model instead. 而是更新其表模型。 For example: 例如:
btnAddFiles_1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
Object rowData[][] = { { "01.", node, }};
Object columnNames[] = { "Sl.no", "File name"};
table_2.setModel(new DefaultTableModel(rowData, columnNames));
}
});
About this line: 关于这条线:
table_2.setBounds(324, 43, 713, 121);
Swing is designed to work with LayoutManagers and methods like setBounds(...)
, setLocation(...)
and setXxxSize(...)
are discouraged. Swing设计为可与LayoutManager配合使用,不建议使用setBounds(...)
, setLocation(...)
和setXxxSize(...)
方法。 See Laying Out Components Within a Container 请参见在容器中布置组件
In addition (thanks to @mKorbel comments): 另外(感谢@mKorbel的评论):
frmViperManufacturingRecord.getContentPane().add(table_2);
Tables should be placed within a JScrollPane
just like your tree is: 表应该放在JScrollPane
,就像您的树一样:
JScrollPane scrollPane = new JScrollPane(table_2);
frmViperManufacturingRecord.getContentPane().add(scrollPane);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.