如何使 JTable 单元格不可编辑但应该能够 select 并复制当前单元格中的值
how to make JTable cell non editable but should be able to select and copy the value in current cell
我正在尝试使 JTable 单元格不可编辑,但如果我这样做,我将无法 select 单个单元格值,而不是当我尝试复制整行时 selected
我只想复制 selected 单元格值而不是整个 row.Is 有办法吗?
public class EmployeeWin extends JFrame {
DefaultTableModel model = new DefaultTableModel() {
@Override
public boolean isCellEditable(int row, int column){
return false;
}
};
Container cont = this.getContentPane();
JTable tab = new JTable(model);
private TableRowSorter<TableModel> rowSorter = new TableRowSorter<>(model);
private final JTextField searchFilter = new JTextField();
public EmpDataWin(List<EmployeeDTO> pEmployeeDTO) {
initialize(pEmployeeDTO);
}
public void initialize(List<EmployeeDTO> pEmployeeDTOList) {
JPanel panelParent = new JPanel(new BorderLayout());
// Add Header
model.addColumn("Employee Name");
model.addColumn("Department");
model.addColumn("Details");
// Add data row to table
for (EmployeeDTO aEmployeeDTO : pEmployeeDTOList) {
model.addRow(new Object[] { aEmployeeDTO.getEmployee_Name(), aEmployeeDTO.getDepartment(),
aEmployeeDTO.getDetails()});
}
tab.setRowSorter(rowSorter);
tab.setAutoCreateRowSorter(true);
JPanel panel = new JPanel(new BorderLayout());
panel.add(new JLabel(UIConstants.SEARCH), BorderLayout.WEST);
JTextField searchFilter = SearchFilter.createRowFilter(tab);
panel.add(searchFilter, BorderLayout.CENTER);
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
tab.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
JScrollPane sp = new JScrollPane(tab,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
panelParent.add(panel,BorderLayout.NORTH);
panelParent.add(sp,BorderLayout.CENTER);
panelParent.setBorder(BorderFactory.createEmptyBorder(10 , 10, 10, 10));
cont.add(panelParent);
this.pack();
}
public static void main(String[] args) {
EmployeeDAO dao = new EmployeeDAO();
List<EmployeeDTO> dto = dao.getemployeeData();
JFrame frame = new EmployeeDataWin(dto);
}
}
when i try to copy the entire row is getting selected I want to copy only the selected cell value instead of entire row
Ctrl+C
键的默认 Action
是复制整行。如果您只想要当前所选单元格的数据,则需要将默认 Action
替换为自定义 Action
.
逻辑类似于:
Action copyCell = new AbstractAction()
{
@Override
public void actionPerformed(ActionEvent e)
{
JTable table = (JTable)e.getSource();
int row = table.getSelectedRow();
int column = table.getSelectedColumn();
Object value = table.getValueAt(row, column);
// copy the data to the clipboard
Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
StringSelection testData = new StringSelection( value.toString() );
c.setContents(testData, testData);
}
};
table.getActionMap().put("copy", copyCell);
以上代码将创建自定义 Action
并将其替换为 JTable
的 ActionMap
。参见 Key Bindings。 link中提供的程序显示了所有默认操作和每个操作的关键字。
我正在尝试使 JTable 单元格不可编辑,但如果我这样做,我将无法 select 单个单元格值,而不是当我尝试复制整行时 selected 我只想复制 selected 单元格值而不是整个 row.Is 有办法吗?
public class EmployeeWin extends JFrame {
DefaultTableModel model = new DefaultTableModel() {
@Override
public boolean isCellEditable(int row, int column){
return false;
}
};
Container cont = this.getContentPane();
JTable tab = new JTable(model);
private TableRowSorter<TableModel> rowSorter = new TableRowSorter<>(model);
private final JTextField searchFilter = new JTextField();
public EmpDataWin(List<EmployeeDTO> pEmployeeDTO) {
initialize(pEmployeeDTO);
}
public void initialize(List<EmployeeDTO> pEmployeeDTOList) {
JPanel panelParent = new JPanel(new BorderLayout());
// Add Header
model.addColumn("Employee Name");
model.addColumn("Department");
model.addColumn("Details");
// Add data row to table
for (EmployeeDTO aEmployeeDTO : pEmployeeDTOList) {
model.addRow(new Object[] { aEmployeeDTO.getEmployee_Name(), aEmployeeDTO.getDepartment(),
aEmployeeDTO.getDetails()});
}
tab.setRowSorter(rowSorter);
tab.setAutoCreateRowSorter(true);
JPanel panel = new JPanel(new BorderLayout());
panel.add(new JLabel(UIConstants.SEARCH), BorderLayout.WEST);
JTextField searchFilter = SearchFilter.createRowFilter(tab);
panel.add(searchFilter, BorderLayout.CENTER);
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
tab.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
JScrollPane sp = new JScrollPane(tab,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
panelParent.add(panel,BorderLayout.NORTH);
panelParent.add(sp,BorderLayout.CENTER);
panelParent.setBorder(BorderFactory.createEmptyBorder(10 , 10, 10, 10));
cont.add(panelParent);
this.pack();
}
public static void main(String[] args) {
EmployeeDAO dao = new EmployeeDAO();
List<EmployeeDTO> dto = dao.getemployeeData();
JFrame frame = new EmployeeDataWin(dto);
}
}
when i try to copy the entire row is getting selected I want to copy only the selected cell value instead of entire row
Ctrl+C
键的默认 Action
是复制整行。如果您只想要当前所选单元格的数据,则需要将默认 Action
替换为自定义 Action
.
逻辑类似于:
Action copyCell = new AbstractAction()
{
@Override
public void actionPerformed(ActionEvent e)
{
JTable table = (JTable)e.getSource();
int row = table.getSelectedRow();
int column = table.getSelectedColumn();
Object value = table.getValueAt(row, column);
// copy the data to the clipboard
Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
StringSelection testData = new StringSelection( value.toString() );
c.setContents(testData, testData);
}
};
table.getActionMap().put("copy", copyCell);
以上代码将创建自定义 Action
并将其替换为 JTable
的 ActionMap
。参见 Key Bindings。 link中提供的程序显示了所有默认操作和每个操作的关键字。