다시 한번 기본적인 질문을해서 죄송합니다. 자바를 처음 접했기 때문에 어리석은 일을하는 대신지도를받는 것이 더 낫다고 생각했습니다.
내 jtable에는 다음 데이터가 있습니다.
Id | Name | Date(month-year)
1 | XYZ | August-2014
2 | ABc | April-2014
내 Modelclass는 다음과 같습니다.
class MyTableModel extends AbstractTableModel {
private String[] columnNames = {"Id", "Country","Date"};
public final Object[] longValues = {Integer.class,"",Date.class};
이제 권장되는대로 모델 테이블에서 getColumnClass를 재정의했습니다.
@Override
public Class getColumnClass(int c)
{
if(c == 2)
{
//return Date.class;
return getValueAt(0,c).getClass();
}
else
return getValueAt(0,c).getClass();
}
기본 열 분류기를 활성화했습니다.
이제 기본적으로 모든 문자열 열은 단락에서 잘 작동하지만 Date 열의 경우에도 문자열로 간주하고 문자열로 줄입니다.
열을 날짜로 짧게 만들면 오류가 발생합니다.
내가 뭘 잘못하고 있고 올바른 방법은 무엇입니까?
감사
이제 권장
getColumnClass
되는대로 모델 테이블에서
Note that your implementation is wrong mainly because you never have to ask for model's value to get the column's class (not to mention redundant/useless if-else
block). Since you know your model then you are perfectly capable to return the right column class with no need to query the model:
@Override
public Class<?> getColumnClass(int columnIndex) {
switch (columnIndex) {
case 0: return Integer.class; // Id column
case 1: return String.class; // Name column
case 2: return Date.class; // Date column
default: return super.getColumnClass(columnIndex);
}
}
This should be enough to properly sort dates column, even if your renderer shows month-year pattern:
table.setDefaultRenderer(Date.class, new DefaultTableCellRenderer() {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if (value instanceof Date) {
SimpleDateFormat formatter = new SimpleDateFormat("MMMM-yyyy");
value = formatter.format((Date)value);
}
return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
}
});
For example if you have 9/27/2014
and 9/30/2014
then this should be the natural ascending order when you sort this column, regardless both cells display September-2014
.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다