Search code examples
javaswingjtablejbuttonjtree

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.

How to get a node value from one function to another function, thanks

obtaining node value from 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

    //table just below Add Files button
    table_2 = new JTable();
    table_2.setBounds(324, 43, 713, 121);
    frmViperManufacturingRecord.getContentPane().add(table_2);

using 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);

Solution

  • 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. 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));
       }
    });
    

    Side notes

    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. See Laying Out Components Within a Container

    In addition (thanks to @mKorbel comments):

    frmViperManufacturingRecord.getContentPane().add(table_2);
    

    Tables should be placed within a JScrollPane just like your tree is:

    JScrollPane scrollPane = new JScrollPane(table_2);
    frmViperManufacturingRecord.getContentPane().add(scrollPane);