2010-05-05 4 views
1

JTable이 각 열에 대해 특정 항목을 표시하도록하려면 어떻게해야합니까?JTable에서 특정 열 설정을 지정하는 방법은 무엇입니까?

둘 이상의 열에 대해 Object를 사용할 수 있습니까? 날짜 (dd.mm.yyyy)와 시간 (hh : mm)을 표시하는 열이 필요하지만 지금은 날짜 개체의 문자열 표현 만 보여줍니다.

public class AppointmentTableModel extends AbstractTableModel { 
    ... 
    @Override 
    public Object getValueAt(int rowIndex, int columnIndex) { 
     return appointments.get(rowIndex).getByColumn(columnIndex); 
    } 
} 

public class Appointment { 
    ... 
    public Object getByColumn(int columnIndex) { 
     switch (columnIndex) { 
      case 0: return date; 
      case 1: return date; 
      case 2: return sample; 
      case 3: return sample; 
      case 4: return history; 
      case 5: return comment; 
     } 
     return null; 
    } 
} 

답변

3

을, 하나는 Date 객체를 가지고 날짜 문자열로 포맷합니다, 다른 하나는 걸릴 것입니다 Date 개체를 만들고 시간 문자열로 서식을 지정합니다.

날짜 렌더링에 대한 예제는 this tutorial을 참조하십시오.

+0

+1 : 그건 내 솔루션보다 더 우아합니다 :) –

1

변경해야 할 것은 getByColumn입니다.

예를 들어 case 0case 1에 두 가지 형식 만 적용하면됩니다.


당신은 이것에 대한 SimpleDateFormat 사용할 수 있습니다 : 당신은 두 개의 열이 특정 렌더러를 설정할 수 있습니다

SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy"); 
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm"); 
// ... 
case 0: return dateFormat.format(date); 
case 1: return timeFormat.format(date); 
관련 문제