我已經寫了一個帶有表格的小程序,我試過並閱讀了很多文檔和awers,但是我真的不明白我做錯了什麼,因爲第四列是還是編輯,當我選擇的行不觸發事件...jtable selectionlistener不起作用
主要
public class Main extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
private JLabel banner;
private String[] tecnici;
private String[] columnNames;
private Object[][] data;
private JTable table;
Map<String,List<Cliente>> clientiList;
private Container pane = getContentPane();
GroupLayout gl = new GroupLayout(pane);
Main(){
init();
}
private void init(){
ImageIcon webIcon = new ImageIcon(Constants.WORK_GUI_LOGO);
setIconImage(webIcon.getImage());
setTitle(Constants.WORK_GUI_TITLE);
setSize(300, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
banner=new JLabel("", webIcon, JLabel.CENTER);
MenuBar menu=new MenuBar();
setJMenuBar(menu.getMenuBar());
System.out.println("Leggo i tecnici dal db");
tecnici=new String[] {"Tarzan","Cita","Jane"};
clientiList=new HashMap();
for(String tec:tecnici){
List<Cliente> l=new ArrayList<>();
Cliente c=new Cliente();
c.setConsultivoIncassi(1000);
c.setNome("Clnl ross");
c.setPreventivoIncassi(1000);
l.add(c);
c=new Cliente();
c.setConsultivoIncassi(2000);
c.setNome("Clnl ross");
c.setPreventivoIncassi(2000);
l.add(c);
clientiList.put(tec, l);
}
data=createTableFromMap(clientiList);
columnNames=new String[]{"Tecs","Customer","count","credit"};
table=new JTable(data, columnNames){
public boolean isCellEditable(int row, int col) {
//where i want to make editable only the 4th col
return col==4;
}
};
table.getSelectionModel().addListSelectionListener(new TableSelectionListener());
createWindowLayout(
banner,
new JLabel("Tecnici"),
new JCheckBox(tecnici[0]),
new JCheckBox(tecnici[1]),
new JCheckBox(tecnici[2]),
new JTable(data, columnNames));
}
public void createWindowLayout(JComponent... arg) {
pane = getContentPane();
gl = new GroupLayout(pane);
pane.setLayout(gl);
gl.setAutoCreateContainerGaps(true);
gl.setAutoCreateGaps(true);
gl.setHorizontalGroup(gl.createParallelGroup()
.addComponent(arg[0])
.addGroup(gl.createSequentialGroup()
.addComponent(arg[1])
.addGroup(gl.createParallelGroup(Alignment.LEADING)
.addComponent(arg[2])
.addComponent(arg[3])
.addComponent(arg[4])
)
)
.addComponent(arg[5])
);
gl.setVerticalGroup(gl.createSequentialGroup()
.addComponent(arg[0])
.addGroup(gl.createParallelGroup()
.addComponent(arg[1])
.addComponent(arg[2]))
.addComponent(arg[3])
.addComponent(arg[4])
.addComponent(arg[5])
);
pack();
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
Main ex = new Main();
ex.setVisible(true);
});
}
private Object[][] createTableFromMap(Map<String,List<Cliente>> clienti){
Iterator it= clienti.entrySet().iterator();
Object[][] tabella=new Object[clienti.size()][4];
int counter=0;
while(it.hasNext()){
Map.Entry entry=(Map.Entry) it.next();
for(Cliente c:(List<Cliente>)entry.getValue()){
tabella[counter]=new Object[]{entry.getKey(),
c.getNome(),
c.getPreventivoIncassi(),
c.getConsultivoIncassi()};
}
counter++;
}
return tabella;
}
class TableSelectionListener implements ListSelectionListener{
@Override
public void valueChanged(ListSelectionEvent arg0) {
String selectedData = null;
int[] selectedRow = table.getSelectedRows();
int[] selectedColumns = table.getSelectedColumns();
for (int i = 0; i < selectedRow.length; i++) {
for (int j = 0; j < selectedColumns.length; j++) {
selectedData = (String) table.getValueAt(selectedRow[i], selectedColumns[j]);
}
}
System.out.println("Selected: " + selectedData);
}
}
}
*「因爲第4列仍然是可編輯的,所以當我選擇行時不會觸發任何事件......」* SO不是一個幫助臺,而是一個問題解答站點,你沒有提出問題。那麼你希望在這個主題上處理哪個問題,以及你的問題是什麼? –
你沒有閱讀你自己的代碼,你有一個評論說:「只讓可編輯的第四行」在檢查第四列的行上面。你會得到幫助,因爲有很多人渴望業力或轉移(嘿,這也是我的!) - 但是這個問題顯示完全不尊重任何人的答案。 – fdreger
評論是錯誤的,我想編輯只有4rth列,但儘管我已經寫了一切都是stille可編輯。我不想缺乏對任何人的尊重,我想承擔什麼是錯誤的,如果你感到失望,很抱歉。 – mautrok