我很难在jTable中显示二维的char数组。 我正在使用netbeans。
有人可以指导我如何投入: char [] [] wordSet;
进入jTable。
获取错误
这是我得到的错误
(argument mismatch; char[][] cannot be converted to TableModel) constructor JTable.JTable(int,int) is not applicable (argument mismatch; char[][] cannot be converted to int) constructor JTable. JTable(Vector,Vector) is not applicable (argument mismatch; char[][] cannot be converted to Vector) constructor JTable. JTable(Object[][],Object[]) is not applicable (argument mismatch; char[][] cannot be converted to Object[][])
使用后:
JTable table = new JTable(w.getPuzzleSet(), "Puzzle");
答案 0 :(得分:2)
使用Object而不是char。例如。而不是
char[][] charTable = {{'a', 'b', 'c'},{'d', 'e','f'}};
使用
Object[][] charTable = {{'a', 'b', 'c'},{'d', 'e','f'}};
正如@nachokk所说,你也可以使用Character
Character[][] charTable = {{'a', 'b', 'c'},{'d', 'e','f'}};
char[][]
与Object[][]
不同,因为char
是基元。 JTable
使用Object[][]
和Vector
构建代码,没有char[][]
示例强>
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
public class CharTable extends JPanel{
private static final int DIM_WIDTH = 500;
private static final int DIM_HEIGHT = 500;
Object[][] charTable = {{'a', 'b', 'c'},{'d', 'e','f'}};
String[] colNames = {"col1", "col2", "col3"};
public CharTable(){
add(new JScrollPane(new JTable(charTable, colNames)));
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
}
public static void createAndShowGui(){
JFrame frame = new JFrame();
frame.add(new CharTable());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
frame.pack();
frame.setVisible(true);
}
public Dimension getPreferredSize(){
return new Dimension(DIM_WIDTH, DIM_HEIGHT);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run(){
createAndShowGui();
}
});
}
}
答案 1 :(得分:0)
您可以通过执行以下操作将char [] []转换为Object [] []:
char[][] wordSet = { {'a', 'b'},{'c', 'd'} };
String[] header = {"one", "two"};
Object[][] objectSet = new Object[wordSet.length][wordSet[0].length];
for (int i = 0; i < wordSet.length; i++) {
for (int j = 0; j < wordSet[0].length; j ++) {
objectSet[i][j] = wordSet[i][j];
}
}
JTable table = new JTable(objectSet, header);