2015-01-14 3 views
1

JTable을 프로그램에 구현하려고 할 때 Java의 "How to use Tables"을 통해 읽었습니다. 내가 원한 것은 데이터베이스에서 String 값 목록을 가져 와서 열과 행 이름별로 정렬하는 것입니다. 이제 기본 행 헤더가 없다는 것을 알고 있습니다. 열의 경우와 마찬가지로 첫 번째 열을 "행 머리글"로 만들어서이 문제를 해결하고 있습니다. 그렇다면 데이터를 올바르게 정렬하기 위해 Jtable에 대한 사용자 정의 테이블 모델을 작성하기로 결정했습니다 (데이터는 벡터의 문자열 벡터 & 열/행 이름을 문자열 벡터로 별도로 저장 됨). 에 문제가있다. 처음에는 인덱스 오류가 많은 배열을 받았습니다. 따라서 테이블 모델의 데이터가 Jtable에 저장되는 위치를 나타내는 코드를 추가했습니다. 그래서 여기 그래서 여기가 내 SemesterTableModel 내은 JTable 초기화하고 내 SemesterTableModel에 대한 매개 변수로 제 3 개 벡터를 통과 참조 내 Jtable 설명JTable 및 사용자 정의 TableModel이 예상대로 작동하지 않습니다.

//other GUI stuff above: buttons, labels, etc. 
    Vector<String> tableColNames = new Vector<String>(1); 
    Vector<String> tableRowNames = new Vector<String>(1); 
    Vector<Vector<String>> tableData = new Vector<Vector<String>>(1,1); //<row, col> 

    for(int i = 0; i <50; i++){ 
     tableData.insertElementAt(new Vector<String>(), i); 
     for(int b = 0; b<5; b++){ 
      tableData.elementAt(i).insertElementAt("TestData", b); 
     } 
     tableRowNames.insertElementAt("TestRowNames", i); 
    } 

    for(int a = 0; a<5; a++){ 
     tableColNames.insertElementAt("TestColNames", a); 
    } 

    System.out.println(tableData.toString()); 


    table = new JTable(new SemesterTableModel(tableColNames, tableRowNames, tableData)); 
    table.getColumnModel().getColumn(0).setCellRenderer(new JRowHeader());//Makeshift "rowHeader" 
    table.setRowHeight(30); 
    table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS); 
    table.setBackground(Color.LIGHT_GRAY); 



    JScrollPane scrollPane = new JScrollPane(table); 
    //scrollPane.setBackground(Color.BLUE); 
    springLayout.putConstraint(SpringLayout.WEST, scrollPane, 180, SpringLayout.WEST, this); 
    springLayout.putConstraint(SpringLayout.NORTH, lblCumGPA, 30, SpringLayout.NORTH, scrollPane); 
    springLayout.putConstraint(SpringLayout.EAST, comboBoxSemester, -15, SpringLayout.WEST, scrollPane); 
    springLayout.putConstraint(SpringLayout.EAST, lblCumGPA, -15, SpringLayout.WEST, scrollPane); 
    springLayout.putConstraint(SpringLayout.EAST, lblSemGPA, -15, SpringLayout.WEST, scrollPane); 
    springLayout.putConstraint(SpringLayout.SOUTH, btnRemoveSemester, 0, SpringLayout.SOUTH, scrollPane); 
    springLayout.putConstraint(SpringLayout.EAST, scrollPane, -15, SpringLayout.EAST, this); 
    springLayout.putConstraint(SpringLayout.NORTH, scrollPane, 15, SpringLayout.NORTH, this); 
    springLayout.putConstraint(SpringLayout.SOUTH, scrollPane, -15, SpringLayout.SOUTH, this); 

    add(scrollPane); 

    table.setFillsViewportHeight(true); 

를 초기화 내 JPanel의에서 코드입니다. 그 다음 아래에 전달 : 내 프로그램을 실행할 때

public class SemesterTableModel extends AbstractTableModel { 

private Vector<String> colNames, rowNames; 
private Vector<Vector<String>> data; 

public SemesterTableModel() { 
    colNames = new Vector<String>(1); 
    rowNames = new Vector<String>(1); 
    data = new Vector<Vector<String>>(1,1); 
} 

public SemesterTableModel(Vector<String> colNames, Vector<String> rowNames, Vector<Vector<String>> data){ 
    this.colNames = colNames; 
    this.rowNames = rowNames; 
    this.data = data; 
} 

@Override 
public int getColumnCount() { 
    return colNames.size(); 
} 

@Override 
public int getRowCount() { 
    return rowNames.size(); 
} 

@Override 
public Object getValueAt(int col, int row) { 
    if(col == 0) 
     return rowNames.elementAt(row); 
    else if(col >=5 || row >=5) //This is the code I added to figure out where my data was going, before I added this else if statement I was getting the array-index out of bounds errors 
     return "Out of Bounds"; 
    else 
     return data.elementAt(row).elementAt(col); 
} 

@Override 
public boolean isCellEditable(int row, int col){ 
    if(col == 0 || row < 5){ //5 is an arbitrary number, I really want this to be an arbitrary variable that will be dependent on another class I haven't finished yet. 
     return false; 
    } 
    return true; 
} 

}

그래서 지금 내 표는 다음과 같습니다.

enter image description here

는 당연히 내가 "TestData 사용"은 "행 머리글"및 열 머리글 inbetween 하락하고 싶다 "TestRowNames는"첫 번째 열에서 일하기 위해, 그리고 나 또한 "TestColNames '돈'을 가지고 심지어 내가 올바르게 기억한다면 열 머리글을 Jtable 자체를 통해 변경해야하며 TableModel은 변경하지 않아도됩니다.

분명히 나는 ​​여기서 뭔가를 이해하지 못하고 있으며, 나는 이것을 알아 내려고 잠시 동안 키보드에 대해 내 머리를 쾅 쾅 대고있다. 너희들이 무슨 일이 일어나고 있는지, 어떤 제안이 있더라도 나는 모두 귀이다.

답변

1

TableModel#getValueAtint row, int col하지 int col, int row ...해야

public Object getValueAt(int row, int col) { 

당신은 "범위에서"완전히 작동 ... 더 후 5 행

가 있기 때문에 확인하십시오 제거해야합니다

How to Use Scroll Panes에서 가짜를 만들지 않고 자신 만의 행 헤더를 구현하는 방법을 확인하십시오.

+0

와우 ..... 나는 이런 간단한 오류를 놓쳤다는 것을 믿을 수 없습니다. .. 그걸 붙잡아 주셔서 고마워. 이제 의도 한대로 작동합니다. 또한 행 헤더에 대한 제안에 감사 드리며 확실히 확인해 보겠습니다. – SilverEnsign99

관련 문제