2016-11-13 25 views
-4

我已經寫了一個帶有表格的小程序,我試過並閱讀了很多文檔和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); 
     } 

    } 
} 
+3

*「因爲第4列仍然是可編輯的,所以當我選擇行時不會觸發任何事件......」* SO不是一個幫助臺,而是一個問題解答站點,你沒有提出問題。那麼你希望在這個主題上處理哪個問題,以及你的問題是什麼? –

+0

你沒有閱讀你自己的代碼,你有一個評論說:「只讓可編輯的第四行」在檢查第四列的行上面。你會得到幫助,因爲有很多人渴望業力或轉移(嘿,這也是我的!) - 但是這個問題顯示完全不尊重任何人的答案。 – fdreger

+0

評論是錯誤的,我想編輯只有4rth列,但儘管我已經寫了一切都是stille可編輯。我不想缺乏對任何人的尊重,我想承擔什麼是錯誤的,如果你感到失望,很抱歉。 – mautrok

回答

2
return col==4; 

你有沒有做任何基本的調試看到了 「山坳」 變量的值是什麼?您遇到問題時應該做的第一件事是使用System.out.println(...)顯示值,以便確定if條件是否正確。

如果你沒有,你會注意到兩兩件事:

  1. ,因爲永遠不會執行的代碼你不會得到任何輸出。
  2. 即使它執行它不會工作,因爲你的表只有4列和Java使用0基於偏移量,你應該使用「3」爲第4列。

代碼從未執行的原因是因爲您有兩個表。用重寫的isCellEditable()方法創建的第一個。但是你永遠不會把這張表添加到GUI中,這樣就是死代碼。然後,您創建添加到GUI第二個表:

new JCheckBox(tecnici[2]), 
new JTable(data, columnNames)); 

因此擺脫上述聲明的,只是使用:

new JCheckBox(tecnici[2]), 
//new JTable(data, columnNames)); 
table); 

由於表變量與自定義方法表。

+0

哦,謝謝你是唯一一個看到錯誤的人,是的,我正在重新初始化桌子,對不起。 – mautrok

2

仔細看你的代碼,並在這裏評論:

 public boolean isCellEditable(int row, int col) { 
      //where i want to make editable only the 4th row 
      return col==4; 
     } 

的意見不匹配合作他們呢?你的方法正在檢查列,當它應該檢查行,不是?例如: -

 public boolean isCellEditable(int row, int col) { 
      //where i want to make editable only the 4th row 
      return row == 4; // ****** note change ****** 
     } 

MCVE'ized代碼:

import java.awt.Container; 
import java.awt.EventQueue; 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.Iterator; 
import java.util.List; 
import java.util.Map; 

import javax.swing.*; 
import javax.swing.GroupLayout.Alignment; 
import javax.swing.event.*; 

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 row 
       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); 
     } 

    } 

    private static class Cliente { 

     private String nome; 
     private int preventivoIncassi; 
     private int consultivoIncassi; 

     public String getNome() { 
      return nome; 
     } 

     public void setPreventivoIncassi(int preventivoIncassi) { 
      this.preventivoIncassi = preventivoIncassi;    
     } 

     public void setNome(String nome) { 
      this.nome = nome; 
     } 

     public void setConsultivoIncassi(int consultivoIncassi) { 
      this.consultivoIncassi = consultivoIncassi; 
     } 

     public int getConsultivoIncassi() { 
      return consultivoIncassi; 
     } 

     public int getPreventivoIncassi() { 
      return preventivoIncassi; 
     } 

    } 

    private static class Constants { 

     public static final String WORK_GUI_LOGO = null; 
     public static final String WORK_GUI_TITLE = null; 

    } 

    private static class MenuBar { 

     public JMenuBar getMenuBar() { 
      // TODO Auto-generated method stub 
      return null; 
     } 

    } 
} 
+0

您可能在回答之前至少閱讀了問題。 – fdreger

+1

@fdreger *什麼*'問題'?複製/粘貼在這裏。我已經投票結束,因爲「你不清楚你問的是什麼」。既然你看起來很清楚,請賜教。 –

+0

@fdreger:不,我一直在潛心編碼,我MCVE'ized(見上文)。主知道爲什麼,但我想我覺得空虛無功。 –