我在ScrollPane中有一个JTable,它显示从数据库中检索的数据。我想要实现的是在右键单击时突出显示一行。但是,当我单击右键时,没有任何反应。任何帮助将不胜感激。以下是代码片段:
[ItemTableModel.java]:
class ItemTableModel extends AbstractTableModel {
private final String[] modelColumnNames = {"Kod handlowy", "Nazwa", "Pojemność", "Napięcie", "Tolerancja", "Temp. min.", "Temp. maks.", "Ilość", "Cena", "Stan magazynowy"};
public Class[] modelColumnTypes = {String.class, String.class, Integer.class, Integer.class, Integer.class, Integer.class, Integer.class, Integer.class, Integer.class, Integer.class};
protected static ItemTableModel itemTableModel;
protected static JTable itemTable;
ArrayList<Item> modelData = new ArrayList<>();
public ItemTableModel(ArrayList<Item> dataArray) {
super();
modelData = dataArray;
}
@Override
public int getColumnCount() {
return modelColumnNames.length;
}
@Override
public int getRowCount() {
return modelData.size();
}
@Override
public void setValueAt(Object value, int row, int col) {
Item data = modelData.get(row);
switch (col) {
case 0:
data.setCommercialCode((String) value);
break;
case 1:
data.setName((String) value);
break;
case 2:
data.setCapacity((Integer) value);
break;
case 3:
data.setVoltage((Integer) value);
break;
case 4:
data.setTolerance((Integer) value);
break;
case 5:
data.setTempMin((Integer) value);
break;
case 6:
data.setTempMax((Integer) value);
break;
case 7:
data.setQuantity((Integer) value);
break;
case 8:
data.setPrice((Integer) value);
break;
case 9:
data.setAvailability((Integer) value);
break;
}
}
@Override
public String getColumnName(int col) {
return modelColumnNames[col];
}
@Override
public Class getColumnClass(int col) {
return modelColumnTypes[col];
}
@Override
public Object getValueAt(int row, int col) {
Item data = modelData.get(row);
switch (col) {
case 0:
return data.getCommercialCode();
case 1:
return data.getName();
case 2:
return data.getCapacity();
case 3:
return data.getVoltage();
case 4:
return data.getTolerance();
case 5:
return data.getTempMin();
case 6:
return data.getTempMax();
case 7:
return data.getQuantity();
case 8:
return data.getPrice();
case 9:
return data.getAvailability();
}
return new String();
}
protected static void CreateTable() {
itemTableModel = new ItemTableModel(Retrieve.allItems);
itemTable = new JTable(itemTableModel) {
@Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component component = super.prepareRenderer(renderer, row, column);
int rendererWidth = component.getPreferredSize().width;
TableColumn tableColumn = getColumnModel().getColumn(column);
tableColumn.setPreferredWidth(Math.max(rendererWidth + getIntercellSpacing().width, tableColumn.getPreferredWidth()));
return component;
}
};
itemTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
itemTable.addMouseListener(new MouseAdapter(){
public void MousePressed(MouseEvent me){
if(SwingUtilities.isRightMouseButton(me)){
int row = itemTable.rowAtPoint(me.getPoint());
itemTable.getSelectionModel().setSelectionInterval(row,row);
}
}
});
JScrollPane scrollPane = new JScrollPane(itemTable);
Dimension d = itemTable.getPreferredSize();
scrollPane.setPreferredSize(new Dimension(d.width, itemTable.getRowHeight() * 10 + 1));
MainWindow.TABLE.add(scrollPane);
MainWindow.MAINFRAME.revalidate();
MainWindow.getPasswordFrame().DisposePasswordFrame();
}
}
这里是调用方法的地方:
[PasswordForm.java]
JButton loginButton = new JButton("Zaloguj");
loginButton.addActionListener((ActionEvent e) -> {
if (usernameField != null && passwordField != null) {
Retrieve.USER = usernameField.getText();
Retrieve.PASS = passwordField.getText();
Retrieve.Connect();
ItemTableModel.CreateTable();
}
});
答案 0 :(得分:2)
public void MousePressed(MouseEvent me){
发布的代码看起来很合理,但是你要覆盖错误的方法。方法是mousePressed
。 Java区分大小写。
这就是在重写方法时应始终使用@Override
注释的原因。然后编译器可以为你捕捉拼写错误:
@Override
public void mousePressed(...)