0

我正在尝试创建一个在 JTable 上显示每周承诺的程序,但我遇到了一个问题,我在 JTable 单元格中添加的第一个对象“impegno”(英文commintment)是可滚动的......以下不是. 您如何在这里看到: http://tinypic.com/r/2uqyeyh/5 单元格 5,2 不可滚动,当我尝试单击它时,没有任何反应。

这是扩展 JPanel 并包含 JTable 的类

    package view;

import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import model.ImpegniTableModel;

public class TablePanel extends JPanel
{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private ImpegniTableModel tableModel;
    private JTable table;
    public TablePanel ( ImpegniTableModel tableModel  )
    {
        this.tableModel = tableModel;
        createTable ( );
    }
    private void createTable() 
    {
        setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));

        table = new JTable ( tableModel.getData(), tableModel.getColumnNames() );
        table.setRowHeight(35);
        table.setAutoResizeMode(5);
        table.setFillsViewportHeight(true);

        ImpegniTableCellEditor editor = new ImpegniTableCellEditor();
        ImpegniCellRenderer render = new ImpegniCellRenderer();
            for ( int j = 0; j < tableModel.getColumnCount()-1;j++)
            {
                table.getColumnModel().getColumn(j).setCellRenderer(render);
                table.getColumnModel().getColumn(j).setCellEditor(editor);
            }

        JScrollPane scrollPane = new JScrollPane( table );

        add( scrollPane );

    }


}

这是扩展 TableCellEditor 的类

package view;

import java.awt.Component;
import java.util.EventObject;

import javax.swing.AbstractCellEditor;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.event.CellEditorListener;
import javax.swing.table.TableCellEditor;

public class ImpegniTableCellEditor extends AbstractCellEditor implements TableCellEditor {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private JTextArea area;
    public Component getTableCellEditorComponent(JTable table, Object value, boolean   isSelected, int row, int column) {
        area = new JTextArea();
        area.setText( value.toString());

        JScrollPane pane = new JScrollPane(area);

        return pane;
    }

    @Override
    public void addCellEditorListener(CellEditorListener l) {
        // TODO Auto-generated method stub

    }

    @Override
    public void cancelCellEditing() {
        // TODO Auto-generated method stub

    }

    @Override
    public Object getCellEditorValue() {
        // TODO Auto-generated method stub
        return area.getText();
    }

    @Override
    public boolean isCellEditable(EventObject anEvent) {

        return true;
    }

    @Override
    public void removeCellEditorListener(CellEditorListener l) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean shouldSelectCell(EventObject anEvent) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean stopCellEditing() {
        // TODO Auto-generated method stub
        return false;
    }

}

扩展 DefaultTableCellRenderer 的类

package view;

import java.awt.Component;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.table.DefaultTableCellRenderer;

public class ImpegniCellRenderer extends DefaultTableCellRenderer {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;


    private JTextArea textArea;
    private JScrollPane scrollPane;
    private JPanel panel;

    public ImpegniCellRenderer( ) 
    {

        panel = new JPanel();
        textArea = new JTextArea();

    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column)
    {


              if ( value != null )
              {
              textArea.setText(value.toString());
              textArea.revalidate();
              panel.add(textArea);
              scrollPane = new JScrollPane(panel);


              return scrollPane;
              }

              return null;
    }


}

这是 MainFrame

package view;


import java.awt.BorderLayout;



import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.DayOfWeek;
import model.Impegno;
import model.SimpleTime;
import model.TipoImpegno;

import controller.BadFileFormatException;
import controller.MyController;




public class MainFrame extends JFrame implements ActionListener
{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private TablePanel tablePanel;
    private JPanel impegnoPanel,buttonPanel;
    private MyController controller;
    private JComboBox<TipoImpegno> compoTipi;
    private JComboBox<DayOfWeek> comboGiorni;
    private JTextField descrizione,ora,minuti;
    private JButton add,save;

    public MainFrame ( MyController controller )
    {

        this.controller = controller;
        initDATA();
        initGUI();
        this.pack();
    }



    private void initDATA()
    {
        this.compoTipi = new JComboBox<TipoImpegno>();
        this.compoTipi.addItem(TipoImpegno.APPUNTAMENTO);
        this.compoTipi.addItem(TipoImpegno.LAVORO);
        this.compoTipi.addItem(TipoImpegno.SCUOLA);

        this.comboGiorni = new JComboBox<DayOfWeek>();
        for ( DayOfWeek day : DayOfWeek.values())
            this.comboGiorni.addItem(day);

    }



    private void initGUI() 
    {

        setLayout( new BorderLayout());

        impegnoPanel = new JPanel ();
        impegnoPanel.setLayout(new BoxLayout(impegnoPanel,BoxLayout.X_AXIS));

        impegnoPanel.add(compoTipi);
        impegnoPanel.add(comboGiorni);

        this.descrizione = new JTextField();
        impegnoPanel.add(descrizione);
        this.descrizione.addActionListener(this);
        this.descrizione.setText("Descrizione");
        this.ora = new JTextField();
        impegnoPanel.add(ora);
        this.ora.addActionListener(this);
        this.ora.setText("Ora");
        this.minuti = new JTextField();
        impegnoPanel.add(minuti);
        this.minuti.addActionListener(this);
        this.minuti.setText("Minuti");

        tablePanel = new TablePanel (controller.getTableModel());

        getContentPane().add(tablePanel,BorderLayout.CENTER);

        getContentPane().add(impegnoPanel,BorderLayout.NORTH);


        buttonPanel = new JPanel();
        buttonPanel.setLayout(new BoxLayout(buttonPanel,BoxLayout.Y_AXIS));

        add = new JButton("ADD ");
        add.addActionListener(this);
        buttonPanel.add(add);

        save = new JButton("SAVE");
        save.addActionListener(this);
        buttonPanel.add(save);
        getContentPane().add(buttonPanel,BorderLayout.EAST);

    }



    @Override
    public void actionPerformed(ActionEvent arg0) 
    {
        int ora = 0,minuti = 0;
        SimpleTime st;
        TipoImpegno tipoImpegno;
        DayOfWeek day;
        String descrizione;
        Object source = arg0.getSource();

        if ( source == add )
        {
        try
        {
            ora = Integer.parseInt(this.ora.getText());
            if ( ora > 24 || ora < 0 )
                new SwingUserInteractor().showMessage("Errore formato ora");
        }
        catch ( NumberFormatException e )
        {
            new SwingUserInteractor().showMessage("Errore formato ora");
            throw new IllegalArgumentException();
        }

        try
        {
            minuti = Integer.parseInt(this.minuti.getText());
            if ( minuti > 60 || minuti < 0 )
                new SwingUserInteractor().showMessage("Errore formato minuti");

        }
        catch ( NumberFormatException e )
        {
            new SwingUserInteractor().showMessage("Errore formato minuti");
            throw new IllegalArgumentException();
        }

        st = new SimpleTime(ora , minuti);
        descrizione = this.descrizione.getText();
        tipoImpegno = (TipoImpegno) this.compoTipi.getSelectedItem();
        day = (DayOfWeek) this.comboGiorni.getSelectedItem();

        Impegno impegno = new Impegno ( tipoImpegno,descrizione,st,day);

        controller.addImpegno(impegno);
        refreshTable();
        }

        if( source == save )
        {
            try 
            {
                controller.write();
            } 
            catch (IOException | BadFileFormatException e ) 
            {   
                new SwingUserInteractor().showMessage("Errore salvataggio dati");
                e.printStackTrace();
            }
        }

    }

    private void refreshTable( )
    {
        this.tablePanel.repaint();
        this.tablePanel.validate();
    }

}

我究竟做错了什么?谢谢 :) 请原谅我的英语不好 :)

4

1 回答 1

0

好的,我很愚蠢:) 我找到了解决方案。我必须为每一列创建一个对象 ImpegniCellRenderer 和 ImpegniTableCellEditor。没错:

    for ( int j = 0; j < tableModel.getColumnCount()-1;j++)
    {
        table.getColumnModel().getColumn(j).setCellRenderer(new ImpegniCellRenderer());
        table.getColumnModel().getColumn(j).setCellEditor(new ImpegniTableCellEditor());
    }

那是错的 :

ImpegniTableCellEditor editor = new ImpegniTableCellEditor();
ImpegniCellRenderer render = new ImpegniCellRenderer();
    for ( int j = 0; j < tableModel.getColumnCount()-1;j++)
    {
        table.getColumnModel().getColumn(j).setCellRenderer(render);
        table.getColumnModel().getColumn(j).setCellEditor(editor);
    }
于 2013-10-12T11:26:10.293 回答