我想更改JTable中的选择。一种典型的方法是按照建议的here清除并重新选择:
ListSelectionModel model = table.getSelectionModel();
model.clearSelection();
model.addSelectionInterval(1, 1);
model.addSelectionInterval(18, 18);
遗憾的是,clearSelection立即更新了UI。因此,如果您单步执行此代码,选择将消失,然后再次返回,这可能导致闪烁。
我试图通过忽略更改来防止UI更新。但是,以下方法不起作用:
model.setValueIsAdjusting( true );
model.clearSelection();
model.setValueIsAdjusting( false );
在用户界面中似乎忽略了该标志。另外,分别在clearSelection无效之前和之后删除和重新添加ListSelectionListeners。用户界面仍处于更新状态,并且可能会闪烁。
是否有更好的方法来更改具有多行的JTable的完全选择,而可能没有连续选择?
对我来说,这不是主要问题,因为您确实需要查找闪烁。但是,这不是最佳选择。
答案 0 :(得分:0)
仅当您不在事件调度线程(EDT)上时,才会出现此问题。感谢Sascha的提示。
示例:
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
public class JTableExample extends JFrame
{
private static final long serialVersionUID = 1L;
private final JTable table;
public JTableExample()
{
// headers and data for the table
final String[] columns = new String[]{"Id", "Name"};
final Object[][] data = new Object[][]{{1, "Alice"}, {2, "Bob"}, {3, "Charlie"},};
// create table with data
table = new JTable( data, columns );
this.add( new JScrollPane( table ) );
// Add key listener to trigger re-selection of columns
table.addKeyListener( new KeyAdapter()
{
@Override
public void keyReleased( final KeyEvent e )
{
// Re-select on event dispatch thread (EDT)
if( e.getKeyCode() == KeyEvent.VK_1 )
{
reselectTable();
}
// Re-select on a thread that is not the EDT
if( e.getKeyCode() == KeyEvent.VK_2 )
{
new Thread( () -> reselectTable() ).start();
}
// Re-select on a thread that is not the EDT, but using invokeLater to get back to the EDT
if( e.getKeyCode() == KeyEvent.VK_3 )
{
new Thread( () -> SwingUtilities.invokeLater( () -> reselectTable() ) ).start();
}
}
} );
// Setup JFrame
setTitle( "JTable Example" );
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
pack();
setVisible( true );
}
public static void main( final String[] args )
{
SwingUtilities.invokeLater( new Runnable()
{
@Override
public void run()
{
new JTableExample();
}
} );
}
private void reselectTable()
{
System.out.println( "isEventDispatchThread:" + SwingUtilities.isEventDispatchThread() );
final ListSelectionModel selectionModel = table.getSelectionModel();
selectionModel.clearSelection();
// PUT BREAKPOINT HERE
// Selection in rendered GUI is gone here when not in the EDT
selectionModel.addSelectionInterval( 0, 0 );
selectionModel.addSelectionInterval( 2, 2 );
}
}