JTable:没有行,高度大小错误

时间:2014-07-12 14:48:58

标签: java swing jtable

我的JTable有问题。它是新的,没有排,但是当我运行项目时,我看到了这一点:

Image

并不明白为什么它是方形的。

这是JPanel

public class PanelTF extends JPanel implements Serializable, ActionListener, MouseListener
{
private JPanel pan_4 = new JPanel();
private JPanel pan_g = new JPanel();    
private TableModel tModel;
private JTable table;
private JScrollPane JSPTTable;

public PanelTF()
{
/* ... */
tModel=new TableModel(sinTv.getListTv());
table=new JTable(tModel);
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);

for (int i=0; i<table.getColumnCount(); i++)
{
TableColumn column = table.getColumnModel().getColumn(i);
}

table.setBackground(Color.MAGENTA);
table.setFillsViewportHeight(true);
JSPTTable = new JScrollPane(table);
pan_4.setBackground(Color.yellow);
pan_4.add(JSPTTable);
pan_g.setLayout(new BoxLayout(pan_g,BoxLayout.Y_AXIS));
pan_g.add(pan_4);
}

这是我的TableModel班级

import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
public class TableModel extends AbstractTableModel 
{
    private ArrayList<IFS> listaMista;

    public TableModel(ArrayList<IFS> listaMista) 
    {
        this.listaMista = listaMista;
    }

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

    public int getColumnCount() 
    {
        return 9;
    }

    public String getColumnName(int column) 
    {
        switch (column) 
        {
            case 0: return "Tipo";
            case 1: return "Titolo";
            case 2: return "Alias";
            case 3: return "Regista";
            case 4: return "Num. Ep";
            case 5: return "Ep1";
            case 6: return "Ep2";
            case 7: return "Ep3";
            case 8: return "A. inizio";
            case 9: return "A. fine";           
        }
        return "";
    }

    public Class getColumnClass(int column) 
    {
        switch (column) 
        {
            case 0: return IFS.class;   // tipo
            case 1: return String.class;    // titolo
            case 2: return String.class;    // alias
            case 3: return String.class;    // regista  
            case 4: return Number.class;    // numEp
            case 5: return String.class;    // ep1  
            case 6: return String.class;    // ep2
            case 7: return String.class;    // ep3
            case 8: return Number.class;    // anno inizio 
            case 9: return Number.class;    // anno fine 
        }
        return Object.class;
    }

    public boolean isCellEditable(int row, int column) 
    {
        return true;
    }

    public Object getValueAt(int row, int column)
    {
        IFS ifs = listaMista.get(row);
        if(ifs instanceof SerieTv)
        {
            SerieTv serie=(SerieTv) ifs;
            switch (column) 
            {
                case 1: return serie.getTitolo();
                case 2: return serie.getAlias();
                case 3: break;  // auto-boxing!
                case 4: return serie.getNumEp();    // auto-boxing!
                case 5: return serie.getEp1();  // auto-boxing!
                case 6: return serie.getEp2();  // auto-boxing!
                case 7: return serie.getEp3();  // auto-boxing!
                case 8: return serie.getAnno(); // auto-boxing!
                case 9: return serie.getAnnoFine(); // auto-boxing!
            }
        }
        if(ifs instanceof FilmTv)
        {
            FilmTv filmtv=(FilmTv) ifs;
            switch (column)
            {
                case 1: return filmtv.getTitolo();
                case 2: return filmtv.getAlias();
                case 3: return filmtv.getRegista(); 
                case 8: return filmtv.getAnno();
            }
        }
        if(ifs instanceof Film)
        {
            Film film=(Film) ifs;
            switch (column)
            {
                case 1: return film.getTitolo();
                case 2: return film.getAlias();
                case 3: return film.getRegista();   
                case 8: return film.getAnno();
            }
        }
        return null;
    }

    public void setValueAt(Object value, int row, int column) 
    {
        IFS ifs = listaMista.get(row);
        if(ifs instanceof SerieTv)
        {
            SerieTv serie=(SerieTv) ifs;
            switch (column) 
            {
                case 1: serie.setTitolo((String) value); break;
                case 2: serie.setAlias((String) value); break;
                case 3: break;
                case 4: serie.setNumEp((Integer) value); break;
                case 5: serie.setEp1((String) value); break;
                case 6: serie.setEp2((String) value); break;
                case 7: serie.setEp3((String) value); break;
                case 8: serie.setAnno((Integer) value); break;
                case 9: serie.setAnnoFine((Integer) value); break;
            }
        }
        if(ifs instanceof FilmTv)
        {
            FilmTv filmtv=(FilmTv) ifs;
            switch (column) 
            {
                case 1: filmtv.setTitolo((String) value); break;
                case 2: filmtv.setAlias((String) value); break;
                case 3: filmtv.setRegista((String) value); break;
                case 8: filmtv.setAnno((Integer) value); break;
            }
        }
        if(ifs instanceof Film)
        {
            Film film=(Film) ifs;
            switch (column) 
            {
                case 1: film.setTitolo((String) value); break;
                case 2: film.setAlias((String) value); break;
                case 3: film.setRegista((String) value); break;
                case 8: film.setAnno((Integer) value); break;
            }
        }
    }

    public void aggiungi(IFS ifs) 
    {
        listaMista.add(ifs);
        int row = listaMista.size() - 1;
        fireTableRowsInserted(row, row); 
    }
}

有什么想法要解决吗?

1 个答案:

答案 0 :(得分:2)

这是因为你用表包裹了JScrollPane。执行此操作时,窗格将获得默认为450x400的preferredViewportSize

如果你不想那么大,你可以覆盖它

JTable table = new JTable() {
    @Override
    public Dimension getPreferredScrollableViewportSize() {
        return new Dimension(300, 300);
    }
};

您还可以将表格标题和表格分别添加到面板。这样,表格将没有容器的首选大小。像

这样的东西
String[] cols = {"col", "col", "cols" };
JPanel panel = new JPanel(new BorderLayout());
JTable table = new JTable(new Object[][]{}, cols);
panel.add(table.getTableHeader(), BorderLayout.PAGE_START);
panel.add(table);

这种方法的问题是,您需要考虑表的动态性质。添加行时,需要包含面板以继续调整大小。出于这个原因,JScrollPane是大小。因此,添加行时,布局不会搞砸。因此,首选视图端口大小,因此您的布局已设置,您不必担心动态调整大小