JTable和数组值问题
我有这段代码可以将我的文件显示到 JTable
中,但出现错误
array required, but java.lang.Object found
这是我的代码:
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.io.*;
import java.util.*;
public class TableDemo extends JPanel {
private boolean DEBUG = false;
static ArrayList rosterList = new ArrayList(); // added static infront becuase got non static referencing error
public TableDemo() {
super(new GridLayout(1,0));
JTable table = new JTable(new MyTableModel());
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
//Add the scroll pane to this panel.
add(scrollPane);
}
class MyTableModel extends AbstractTableModel {
private String[] columnNames = { "Κωδικός", "Ποσότητα", "Τιμή", "Περιγραφή", "Μέγεθος", "Ράτσα"};
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return rosterList.size();
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col)
{
return rosterList.get(row)[col]; //array required,but java.lang.Object found
}
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("TableDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
TableDemo newContentPane = new TableDemo();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
creatArr();
createAndShowGUI();
}
});
}
private static void creatArr()
{
BufferedReader br = null;
try
{
br = new BufferedReader(new FileReader("Dogss.txt"));
String line = br.readLine();
while (line != null )
{
String [] rowfields = line.split("#");
rosterList.add(rowfields);
line = br.readLine();
}
}
catch (FileNotFoundException e)
{
// can be thrown when creating the FileReader/BufferedReader
// deal with the exception
e.printStackTrace();
}
catch (IOException e)
{
// can be thrown by br.readLine()
// deal with the exception
e.printStackTrace();
}
}
}
I have this code to display my file into JTable
but I have an error
array required, but java.lang.Object found
Here is my code:
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.io.*;
import java.util.*;
public class TableDemo extends JPanel {
private boolean DEBUG = false;
static ArrayList rosterList = new ArrayList(); // added static infront becuase got non static referencing error
public TableDemo() {
super(new GridLayout(1,0));
JTable table = new JTable(new MyTableModel());
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
//Add the scroll pane to this panel.
add(scrollPane);
}
class MyTableModel extends AbstractTableModel {
private String[] columnNames = { "Κωδικός", "Ποσότητα", "Τιμή", "Περιγραφή", "Μέγεθος", "Ράτσα"};
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return rosterList.size();
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col)
{
return rosterList.get(row)[col]; //array required,but java.lang.Object found
}
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("TableDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
TableDemo newContentPane = new TableDemo();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
creatArr();
createAndShowGUI();
}
});
}
private static void creatArr()
{
BufferedReader br = null;
try
{
br = new BufferedReader(new FileReader("Dogss.txt"));
String line = br.readLine();
while (line != null )
{
String [] rowfields = line.split("#");
rosterList.add(rowfields);
line = br.readLine();
}
}
catch (FileNotFoundException e)
{
// can be thrown when creating the FileReader/BufferedReader
// deal with the exception
e.printStackTrace();
}
catch (IOException e)
{
// can be thrown by br.readLine()
// deal with the exception
e.printStackTrace();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Java 不知道您的列表包含字符串数组。
你应该像这样声明rosterList:
或者更好:
Java doesn't know that your list contains array of String.
you should declare rosterList like this:
or even better: