2013-11-09 54 views
0

我在其他人的不同問題中搜索。但我沒有成功。 我想讓我的表可編輯。但我不知道如何調用「setValueAt()」。Java swing jtable setvalueAt不起作用

我有以下文件:

MovieManager.java

import java.util.*; 

public class MovieManager 
{ 
    private static List<Movie> movieList = new ArrayList<Movie>(); 

    public static void main(String args[]) 
    { 
     final Director david = new Director("David", "Finch", Gender.MALE); 
     final Movie film1 = new Movie("Fightclub", 140, "Amerika", "Best Movie ever!", david); 
     final Movie film2 = new Movie("Panic Room", 115, "Amerika", "Good Movie", david); 
     final Movie film3 = new Movie("Seven", 120, "Amerika", "Headless", david); 

     movieList.add(film1); 
     movieList.add(film2); 
     movieList.add(film3); 

     // start GUI 
     new MovieUI(); 
    } 

    public static List<Movie> getMovieList() 
    { 
      return movieList; 
    } 

    public static void setMovieList(List<Movie> movieList) 
    { 
     MovieManager.movieList = movieList; 
    } 
} 

MovieUI.java

import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class MovieUI extends JPanel 
{ 
    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 

    public MovieUI() 
    { 
     //Create and set up the window. 
     final JFrame frame = new JFrame("Movie Manager"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     //Create and set up the content pane. 
     final Table newContentPane = new Table(); 
     //newContentPane.setOpaque(true); //content panes must be opaque 
     frame.setContentPane(newContentPane); 

     //Display the window. 
     frame.pack(); 
     frame.setVisible(true); 
    } 
} 

Table.java

import javax.swing.JPanel; 
import javax.swing.JTable; 
import java.awt.BorderLayout; 
import java.awt.GridLayout; 

import javax.swing.table.JTableHeader; 
import javax.swing.table.TableRowSorter; 

public class Table extends JPanel 
{ 
    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 

    public Table() 
    { 
     super(new GridLayout(1,0)); 
     final MovieTableModel model = new MovieTableModel(); 
     final JTable table = new JTable(model); 

     final TableRowSorter<MovieTableModel> sorter = new TableRowSorter<MovieTableModel>(); 
     table.setRowSorter(sorter); 
     sorter.setModel(model); 

     final JTableHeader header = table.getTableHeader(); 
     setLayout(new BorderLayout()); 
     add(header, BorderLayout.PAGE_START); 
     add(table, BorderLayout.CENTER);  
    } 
} 

和 電影TableModel.java

import java.util.ArrayList; 
import java.util.List; 

import javax.swing.table.AbstractTableModel; 

class MovieTableModel extends AbstractTableModel 
{ 
    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 
    private final List<String[]> daten = new ArrayList<String[]>(); 
    private final List<Movie> datenMov = new ArrayList<Movie>(); 
    private final String[] columnNames = {"Id", "Name", "Time", "Language", "Description", "Place"}; 

    public MovieTableModel() 
    { 
     for (Movie movie: MovieManager.getMovieList()) 
     { 
      String[] list = {String.valueOf(movie.getNumber()), movie.getTitle(), String.valueOf(movie.getTime()), "DE", movie.getDescription(), movie.getPlace()}; 
      datenMov.add(movie); 
      daten.add(list); 
     } 
    } 

    public int getColumnCount() 
    { 
     return columnNames.length; 
    } 

    public int getRowCount() 
    { 
     return daten.size(); 
    } 

    public String getColumnName(int col) 
    { 
     return columnNames[col]; 
    } 

    public Object getValueAt(int row, int col) 
    { 
     return daten.get(row)[col]; 
    } 

    public boolean isCellEditable(int row, int col) 
    { 
     if (col < 1) 
     { 
      return false; 
     } 
     else 
     { 
      return true; 
     } 
    } 

    public void setValueAt(String value, int row, int col) 
    { 
     System.out.println("Ich werde aufgerufen"); 
     String[] list = daten.get(row); 
     list[col] = value; 
     daten.set(row, list); 
     System.out.println(daten.get(row)[col]); 

     List<Movie> movieliste = MovieManager.getMovieList(); 

     Movie mov = (Movie) movieliste.get(row); 
     switch(col) 
     { 
      case 1: 
       mov.setTitle(value); 
       break; 
      case 2: 
       int foo = Integer.parseInt(value); 
       mov.setTime(foo); 
       break; 
      case 4: 
       mov.setDescription(value); 
       break; 
      case 5: 
       mov.setPlace(value); 
       break; 
     } 

     movieliste.set(row, mov); 
     MovieManager.setMovieList(movieliste); 
    } 

} 

回答

3

TableModel太複雜。所有你需要做的就是存儲一個電影列表。然後你得到ValueAt()和setValueAt()方法應該訪問List。

你的構造函數應該簡單地:

public MovieTableModel(List movies) 
{ 
    datenMov = movies; 
} 

的getValueAt()應該是這樣的:

public Object getValueAt(int row, int column) 
{ 
    Movie movie = datenMov.get(row); 

    switch(column) 
    { 
     case 0: return movie.getNumber(); 
     case 1: return movie.getTitle(); 
     ... 
     default: return null; 
    } 
} 

和setValueAt()方法是這樣的:

@Override 
public void setValueAt(Object value, int row, int column) 
{ 
    Movie movie = get(row); 

    switch (column) 
    { 
     case 0: movie.setNumber((String)value); break; 
     case 1: movie.setTitle((String)value); break; 
     ... 
    } 
} 

編輯:

此外,在setValueAt()方法,你需要調用:

fireTableCellUpdated(row, column); 
+0

謝謝,現在它工作。 – progNewbie

+0

Hm奇怪,它的工作也沒有fireTableCellUpdated(行,列) – progNewbie

+1

@progNewFag,是的,如果你使用表編輯器改變數據,它會工作,因爲一旦編輯器關閉,單元格將被表格重新繪製。但是,如果你手動調用setValueAt(),那麼它不會工作。另外,如果您不調用該方法,則可能會將其添加到TableModel的TableModelListener的no事件將被觸發。 – camickr

1

我遇到過類似的情況,我終於解決了這個問題。

我發現,當我們雙擊單元格,它去:

JTable.getDefaultEditor(Class<?> columnClass)功能。

在我的情況下,去行:

return getDefaultEditor(columnClass.getSuperclass());

由於我重寫我的表ModelgetColumnClass()當回到int.class,所以它的編輯器返回null

總之,解決的辦法是,
覆蓋在你的臺式getColumnClass()時不返回任何基本數據類型!