0

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();
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 1
    Do you have any code that changes the default behaviour of the table? Any Listeners, CellEditors, CellRenderers etc? – f1sh Apr 05 '16 at 14:50
  • `with a custom TableModel` - maybe the TableModel is wrong? Try using the DefaultTableModel with the same row/column selection and see what happens. This is a basic debugging technique. First try the code with the default classes. Then if your custom code doesn't work you know where the problem is. Same with the above suggestions. Use the default renders first. Then change one thing at a time until the problem occurs so you know which change caused the problem. – camickr Apr 05 '16 at 14:55
  • Welcome to StackOverflow! Thanks for your submission! When posting questions about code errors, please include the portions of your code that are relevant to the question. – gariepy Apr 05 '16 at 14:56
  • Thanks for your comments! I edited my post based on your answers. – benedikt262 Apr 05 '16 at 15:27
  • `Using the default cell renderer and a DefaultTableModel provokes the same issue` - then the code you post should use the default classes so we can execute the code to see if we have the same problem. That is you need to create a [SSCCE](http://sscce.org/) that demonstrates the problem. Then post the `SSCCE`. – camickr Apr 05 '16 at 15:34
  • Can't reproduce the bug using your code when running it in latest Eclipse with Java 8. Everything looks fine. – lkq Apr 05 '16 at 16:05
  • I also cannot reproduce the problem. I'm using JDK8_45 on Windows 7. – camickr Apr 05 '16 at 16:21
  • @KeqiangLi Which OS and JDK are you running? I'm running OpenJDK 8_66 on Ubuntu 15.10 – benedikt262 Apr 05 '16 at 16:31
  • @user3625541 I'm using Mac OS 10.11.4 with JDK8_77 – lkq Apr 05 '16 at 17:27

2 Answers2

1

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

1

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.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045