-1

我有以下代码:

问题是似乎无法将标题放入表中的单独列中,而是我在本部分中定义的列标题

String[] columnNames ={container.toString()};

并且源自

URLConnection yc = oracle.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
yc.getInputStream()));
//create array--------------
ArrayList list = new ArrayList();


Scanner scanner = new Scanner(in);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
//add line to array-------------
list.add(line);
}
//get headings of data------------------

//String heading[] = list.get(2).toString();
String[] simpleArray = new String[list.size()];

list.toArray(simpleArray);

String j = (String) list.get(2);

String [] items = j.split(";");
List<String> container = Arrays.asList(items);



createUserInterface(container); 

全部出现在第一列(因此是表中的唯一列)因此我只看到“名字”出现在数据字段中

我的标题的 Println 是

[A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R,S] *I have substituted my actual headings with letters

帮助表示赞赏

下面的代码——

public void createUserInterface(List<String>container) {
//create user interface---------------------    
setLayout( new BorderLayout());
setTitle("Fund");//setTitle is also a JAVA Parameter
setSize(2000, 200);
setBackground(Color.gray);
setDefaultCloseOperation(EXIT_ON_CLOSE);
// Create a panel to hold all other components
JPanel topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add(topPanel);
//


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

List list = Arrays.asList(container);

Object[] columnNames ={list};
Object[][] data = {{"First Name", "Last Name", "Sport", "# of Years","First Name", "Last Name", "Sport", "# of Years","First Name", "Last Name", "Sport", "# of Years","First Name", "Last Name", "Sport", "# of Years","First Name", "Last Name", "Sport", "# of Years"}};



table = new JTable(data, columnNames);
// Create a new table instance
scrollPane = new JScrollPane( table );
topPanel.add( scrollPane, BorderLayout.CENTER );

setVisible(true);   



}
4

1 回答 1

2

你期望这会返回什么:container.toString()?那怎么可能代表您的表格标题?

如果您希望从 A 到 S 的字母成为您的 JTable 列标题(这就是我您正在尝试做的事情),为什么不简单地使用这个数组呢?

String[] columnNames = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", 
        "K", "L", "M", "N", "O", "P", "Q", "R", "S"}
于 2015-03-18T21:47:19.187 回答