Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3500954
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:47:55+00:00 2026-05-18T12:47:55+00:00

i have a JTree, and when u click leafs, it changes the JTable model

  • 0

i have a JTree, and when u click leafs, it changes the JTable model and displays a new table (much like an email system). the problem arises when i try to select the columns of the new Table model, it always gives -1, no matter what. NOTE that this works perfectly for the 1st JTable model, but after the changed table,

System.out.println(table.getSelectedColumn());

always returns -1.

EDIT: well, i dont know which code snippet to post here to be honest.

    //constructor
    public TreeSection() {
    super();

    //TREE
    top = new DefaultMutableTreeNode("EMAIL");
    createNodes(top);
    //Create a tree that allows one selection at a time.
    tree = new JTree(top);
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    //Listen for when the selection changes.
    tree.addTreeSelectionListener(this);

    //TABLE
    tableModel = new MyTableModel();
    table = new JTable(tableModel);
    table.setPreferredScrollableViewportSize(new Dimension(500, 70));
    table.setFillsViewportHeight(true);
    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    selectionModel = table.getSelectionModel();
    selectionModel.addListSelectionListener(this);


    //SCROLL PANE
    treeView = new JScrollPane(tree);
    tableView = new JScrollPane(table);

    //SPLIT PANE
    splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    splitPane.setTopComponent(treeView);
    splitPane.setBottomComponent(tableView);
    splitPane.setDividerLocation(400);
    splitPane.setPreferredSize(new Dimension(200, 700));

    //Add the split pane to this panel.
    panel.add(splitPane,BorderLayout.WEST);
}

/** Required by TreeSelectionListener interface. */
public void valueChanged(TreeSelectionEvent e) {
    DefaultMutableTreeNode node = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();

    if (node == null) return;

    Object nodeInfo = node.getUserObject();

    if (node.isLeaf()) {
        MailObject mObj = (MailObject)nodeInfo;
        table.removeAll();
        tableModel.setTableData(mObj.tableD);//changes tree model
        table = new JTable(tableModel);

        panel.repaint();
    }
}

public void valueChanged(ListSelectionEvent event) {
    // Get the data model for this table
    //table.changeSelection(table.getSelectedColumn(), table.getSelectedRow(), false, false);
    TableModel model = (TableModel)table.getModel();
    System.out.println(table.getSelectedColumn());
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T12:47:55+00:00Added an answer on May 18, 2026 at 12:47 pm

    getSelectedColumn returns -1 when there is no column selected.
    If you are sure that a column is being selected then perhaps when you are creating the new table you are calling getSelectedColumn on the old table?

    Edit after snippet posted:
    You’re creating a new JTable which not only doesn’t have the settings (such as FillsViewPortHeight but also doesn’t have the selection model. Try replacing

    table = new JTable(tableModel);
    

    in valueChanged with

    table.setModel(tableModel);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JTree which displays a JPopupMenu when I right click nodes in
I have a JTree, a JTable and a JList which displays the same set
I have a JTree with multiple roots (Of course, I actually have an invisible
I have a dialog where each entry in a JTree has its corresponding options
Have you used VS.NET Architect Edition's Application and System diagrams to start designing a
As the question states, I'd like to set a mouse listener to my JTree
Alright, so heres my problem: I have an application I am working with that
I have a Jstree populating a list of items. When I click a node
I create a JTree and model for it out in a class separate to
I have a script that appends some rows to a table. One of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.