benedikt262
benedikt262

Reputation: 11

Strange behaviour of JTable when selecting rows

I use a JTable inside a JScrollPane with a custom TableModel, cell-/column-selection disabled and only row-selection enabled (single selection). If I select a row, from time to time, the value in the cell on which I perform the click, appears in the neighbour columns too (and overwrites the values in there). Could anybody give me a hint, what I'm doing wrong or has anybody else faced this problem?

Thanks for your help in advance!

EDIT: I added a SSCCE. After a few selections, the issue should occur. You can accelerate the occurence of it by keeping the mouse pressed while hovering over the rows. If it occured once, it occurs during every selection.

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;

public class TableIssueSSCCE {

    public TableIssueSSCCE() {
        JFrame frame = new JFrame();
        frame.setSize(400, 400);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());

        JTable table = new JTable(new DefaultTableModel());
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        model.addColumn("Test1");
        model.addColumn("Test2");
        model.addColumn("Test3");
        model.addColumn("Test4");
        for (int i = 0; i < 1000; i++) {
            model.addRow(new String[]{
                "Column1" + i, "Column2" + i, "Column3" + i, "Column4" + i});
        }

        table.setFillsViewportHeight(true);
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        table.setCellSelectionEnabled(false);
        table.setColumnSelectionAllowed(false);
        table.setRowSelectionAllowed(true);
        table.setAutoCreateRowSorter(true);
        JScrollPane tableContainer = new JScrollPane(table);
        tableContainer.setVerticalScrollBar(new JScrollBar(JScrollBar.VERTICAL));
        tableContainer.setHorizontalScrollBar(new JScrollBar(JScrollBar.HORIZONTAL));
        frame.add(tableContainer, BorderLayout.CENTER);

        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new TableIssueSSCCE();
    }
}

Upvotes: 0

Views: 317

Answers (2)

trashgod
trashgod

Reputation: 205785

Some observations:

  • Whenever a Swing program misbehaves intermittently, I start looking for incorrect synchronization, sometimes resorting to the approach shown here. See also Initial Threads. Because such errors may be difficult to reproduce, it's better to be safe:

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            new TableIssueSSCCE();
        });
    }
    
  • Instead of setSize(), override getPreferredScrollableViewportSize(), as suggested here.

Upvotes: 1

benedikt262
benedikt262

Reputation: 11

I was able to avoid the problem by changing to Oracle's JRE. So this issue seems to be related to OpenJDK.

Upvotes: 1

Related Questions