当我单击JTable上的行时,我需要选择一行。默认行为是当按下鼠标时,该行被选中。如何更改此行为?我的期望是::
鼠标按下-->鼠标释放==> selected
鼠标按下-->鼠标拖动-- >鼠标释放==>未选中
鼠标单击选中的==>行
当鼠标被拖动时,我想做一些其他的事情,但不想改变在该操作上的前一行选择。
发布于 2011-02-18 15:57:14
import java.awt.event.*;
import javax.swing.*;
/**
*
* @author Jigar
*/
public class JTableDemo extends MouseAdapter {
int selection;
public static void main(String[] args) throws Exception
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String[] headers = {"A", "B", "C"};
Object[][] data = {{1, 2, 3}, {4, 5, 6}};
JTable table = new JTable(data, headers);
JScrollPane scroll = new JScrollPane();
scroll.setViewportView(table);
frame.add(scroll);
frame.pack();
frame.setVisible(true);
table.addMouseListener(new JTableDemo());
scroll.addMouseListener(new JTableDemo());
}
@Override
public void mousePressed(MouseEvent e)
{
JTable jtable = (JTable) e.getSource();
selection= jtable.getSelectedRow();
jtable.clearSelection();
}
@Override
public void mouseReleased(MouseEvent e){
JTable jtable = (JTable) e.getSource();
//now you need to select the row here check below link
}
}发布于 2011-10-15 08:52:30
我并不觉得这很容易。我试图在其中突出显示行的表不是当前活动的组件,因此您需要类似以下内容:
// get the selection model
ListSelectionModel tableSelectionModel = table.getSelectionModel();
// set a selection interval (in this case the first row)
tableSelectionModel.setSelectionInterval(0, 0);
// update the selection model
table.setSelectionModel(tableSelectionModel);
// repaint the table
table.repaint();发布于 2013-03-20 19:15:58
它是"stange“,但对我来说是有效的:
table.setDragEnabled(true); https://stackoverflow.com/questions/5038781
复制相似问题