2012-10-30 2 views
0

행운을 빌어 JTable 객체에 배열을로드하려고했습니다.JTable - 다차원 배열을 테이블에로드하십시오.

int[][] board = { 
    {0, 0, 0, 0, 2, 0, 0, 0, 0}, 
    {0, 0, 5, 0, 0, 0, 0, 2, 4},  
    {1, 0, 0, 4, 0, 0, 0, 3, 8}, 
    {0, 0, 0, 6, 0, 0, 0, 0, 7}, 
    {0, 0, 4, 5, 3, 8, 9, 0, 0}, 
    {8, 0, 0, 0, 0, 7, 0, 0, 0}, 
    {7, 4, 0, 0, 0, 6, 0, 0, 1}, 
    {6, 1, 0, 0, 0, 0, 3, 0, 0}, 
    {0, 0, 0, 0, 9, 0, 0, 0, 0} 

내가 http://docs.oracle.com/javase/tutorial/uiswing/components/table.html 에 가서 INT 배열을 착용하는 생성자가 없지만, 피사체가 : 그래서 여기 내 배열입니다.

누구나 알고있는 방법, 감사합니다!

+0

이 시도 시도하십시오 Integer 배열에 int 배열을 변경하려고 : http://docs.oracle.com/javase/6/docs/api/javax /swing/JTable.html#JTable(java.lang.Object [] [], java.lang.Object []) –

답변

2

이 같은 것을 할 수있는 :

Integer[][] board = new Integer[][]{ 
     {0, 0, 0, 0, 2, 0, 0, 0, 0}, 
     {0, 0, 5, 0, 0, 0, 0, 2, 4},  
     {1, 0, 0, 4, 0, 0, 0, 3, 8}, 
     {0, 0, 0, 6, 0, 0, 0, 0, 7}, 
     {0, 0, 4, 5, 3, 8, 9, 0, 0}, 
     {8, 0, 0, 0, 0, 7, 0, 0, 0}, 
     {7, 4, 0, 0, 0, 6, 0, 0, 1}, 
     {6, 1, 0, 0, 0, 0, 3, 0, 0}, 
     {0, 0, 0, 0, 9, 0, 0, 0, 0}}; 

new JTable(board, new String[]{"columnName1"...}); 
2

내가 여기 두 가지 가능성을 참조 : Zou는 대신 Object[][]에 주조 할 수 int[][]Integer[][]를 사용할 수 있으며,이 JTable로 작동합니다 또는 당신이 당신의 자신을 쓸 수를 데이터 모델.

결국 달성하고자하는 것에 따라 더 적절한 것을 선택해야합니다.

1

그냥

1

이이

import javax.swing.*; 
import java.awt.*; 
public class JTableComponent{ 
    public static void main(String[] args) 
{ 
    new JTableComponent(); 
    } 

    public JTableComponent(){ 
    JFrame frame = new JFrame("Creating JTable Component Example!"); 
    JPanel panel = new JPanel(); 
    Integer[][] board = { 
      {0, 0, 0, 0, 2, 0, 0, 0, 0}, 
      {0, 0, 5, 0, 0, 0, 0, 2, 4},  
      {1, 0, 0, 4, 0, 0, 0, 3, 8}, 
      {0, 0, 0, 6, 0, 0, 0, 0, 7}, 
      {0, 0, 4, 5, 3, 8, 9, 0, 0}, 
      {8, 0, 0, 0, 0, 7, 0, 0, 0}, 
      {7, 4, 0, 0, 0, 6, 0, 0, 1}, 
      {6, 1, 0, 0, 0, 0, 3, 0, 0}, 
      {0, 0, 0, 0, 9, 0, 0, 0, 0}}; 

    String col[] = {"1","2","3","4","5","6","7","8","9"}; 
    JTable table = new JTable(board,col); 
    panel.add(table,BorderLayout.CENTER); 

frame.add(panel); 
    frame.setSize(800,500); 
    frame.setVisible(true); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 
관련 문제