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 7677323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:23:48+00:00 2026-05-31T17:23:48+00:00

So I am trying to write a UI in swing. In short, I have

  • 0

So I am trying to write a UI in swing. In short, I have a JTable where I listen for selection events, and after a user selects an item, the code takes a bit of a path through the program’s code, after which it invokes a table “redraw”; it empties the table, and adds the rows again.

This process seems to work fine for clicking on rows 2-4 (see code bit below), but when I click on the first row, I get a stack overflow I can not explain.

I know it isn’t exactly the best solution, as the table practically does not require a redraw, but I would first of all like to keep my code clean and logically structured (which is what causes the redraw), and second I have been trying to get what causes this so long that I would really just like to know what happens.

Here is a runnable class that reproduces the problem:

import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

public class Tester implements ListSelectionListener {
private DefaultTableModel model;

public Tester(DefaultTableModel model) {
    this.model = model;
}

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setSize(200, 200);
    frame.setVisible(true);

    JTable table = new JTable();
    DefaultTableModel model = new DefaultTableModel();
    table.setModel(model);
    table.getSelectionModel().addListSelectionListener(new Tester(model));
    model.setColumnCount(3);
    model.setColumnIdentifiers(new String[]{"col1", "col2", "col3"});
    frame.add(table);

    model.addRow(new Object[]{"1", "2", "3"});
    model.addRow(new Object[]{"4", "2", "3"});
    model.addRow(new Object[]{"3", "2", "3"});
    model.addRow(new Object[]{"2", "2", "3"});
}

public void valueChanged(ListSelectionEvent event) {
    model.setRowCount(0);
    model.addRow(new Object[]{"1", "2", "3"});
    model.addRow(new Object[]{"4", "2", "3"});
    model.addRow(new Object[]{"3", "2", "3"});
    model.addRow(new Object[]{"2", "2", "3"});
}
}
  • 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-31T17:23:50+00:00Added an answer on May 31, 2026 at 5:23 pm

    The reason for the stackoverflow is that when you remove all rows and start adding rows you trigger the listener again. I am not a big fan of changing the model in a selection listener (think of the situation where another selection listener is attached to the table … it will receive completely screwed up events. Possible solution for this is to at least use an invokeLater call).

    But to avoid your stackoverflow exception, use

      @Override
      public void valueChanged(ListSelectionEvent event) {
        ListSelectionModel listSelectionModel = ( ListSelectionModel ) event.getSource();
        listSelectionModel.removeListSelectionListener( this );
        model.setRowCount(0);
        model.addRow(new Object[]{"1", "2", "3"});
        model.addRow(new Object[]{"4", "2", "3"});
        model.addRow(new Object[]{"3", "2", "3"});
        model.addRow(new Object[]{"2", "2", "3"});
        listSelectionModel.addListSelectionListener( this );
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In trying to write more testable Java code, I have been using the Model-View-Presenter
I am trying to write an OSGi based desktop application. I have a Swing
Trying to write a code at the moment that basically tests to see if
What's up y'all, I am trying to write some code in Java that will
For my Eee pad transformer, i am trying to write general Java code. But
I have a query. I have a database, and I am trying to write
I've been trying to write a Table-Valued function that takes value pairs as a
I'm new to Java GUI and trying to work with jTable's in SWING. I
I am trying to write a program in Java Swing that outputs a 10
I am trying to write a code to generate a graph like this: http://www.mathgoodies.com/lessons/graphs/images/line_example1.jpg

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.