gpt4 book ai didi

java - 如何使 JTable 单元格不可编辑,但应该能够选择并复制当前单元格中的值

转载 作者:行者123 更新时间:2023-12-02 09:02:04 25 4
gpt4 key购买 nike

我试图使 JTable 单元格不可编辑,但如果我这样做,我将无法选择单个单元格值,而是当我尝试复制整个行时被选中我只想复制选定的单元格值而不是整个行。有办法做到这一点吗?

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 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并将其替换在JTableActionMap中。请参阅Key Bindings 。链接中提供的程序显示了所有默认操作以及每个操作的关键字。

关于java - 如何使 JTable 单元格不可编辑,但应该能够选择并复制当前单元格中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60086679/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com