Java swing jtable setvalueAt不起作用

时间:2013-11-09 15:31:33

标签: swing jtable setvalue

我搜索了其他人的不同问题。但我没有成功。 我想让我的桌子可编辑。但我不知道怎么称呼“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);      
     }
}

和 MovieTableModel.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);
    }

}

2 个答案:

答案 0 :(得分:4)

你的TableModel太复杂了。您需要做的就是存储电影列表。然后getValueAt()和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);

答案 1 :(得分:1)

我遇到了类似的情况,我终于解决了。

我发现,当我们双击一个单元格时,它会转到:

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

在我的情况下,它就行了:

return getDefaultEditor(columnClass.getSuperclass());

由于我在覆盖表int.class中的getColumnClass()时返回了Model,因此它为编辑器返回null

简而言之,解决方案是, 在表格模型中覆盖getColumnClass()时不要返回任何原始数据类型