2013-10-11 1 views
0

JTable에 주간 약정을 표시하는 프로그램을 만들려고하는데 문제가 생겼습니다. 추가 할 첫 번째 개체 인 "impegno"(영어로 된 단위)가 있습니다. JTable 셀은 스크롤 가능합니다 ... 다음은 아닙니다. 여기에서 볼 수있는 방법 : http://tinypic.com/r/2uqyeyh/5 셀 5,2는 스크롤 할 수 없습니다. 클릭하려고하면 아무 일도 발생하지 않습니다. JTable 셀 : JScrollPane이 스크롤되지 않습니다.

이게하는 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; 
    } 

} 

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


} 

이것을하는 DefaultTableCellRenderer

확장 클래스 extendes 클래스가

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

    } 


} 

JTable로를 JPanel의 확장 및 포함 된 클래스 메인 프레임

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

} 

내가 뭘 잘못하고 있니? 고마워요 :) 제발 내 영어를 용서해주십시오 :)

답변

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