2015-03-18 10 views
-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); 

衍生出現在其整體中的第一列(並且因此在表中唯一的列) 我因此ö NLY看 「名」 出現在數據字段

的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 

幫助表示讚賞

代碼below--

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); 



} 
+0

未來,請發佈格式良好的代碼。如果我們可以閱讀你的代碼,我們可以更好地理解它並幫助你。 – 2015-03-18 21:50:59

+0

我更新了我的問題並提供了進一步的信息 – Ingram 2015-03-18 22:02:48

+0

您仍然沒有將數組或集合傳遞給您的JTable構造函數。你傳遞一個字符串!如果它是一個List,那麼將參數從'Object'改爲'List '並將其傳遞給JTable,而不是'toString()'。這絕對沒有意義。 – 2015-03-18 22:03:37

回答

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"} 
+0

我已更新我的問題。這些字母代替了這個問題。實際的列名來自一個URL,我正在按照代碼在URL中的標題行String j =(String)list.get(2); – Ingram 2015-03-18 22:06:01

+0

@MrAssistance:再次,當你在任何*上調用'toString()'時,你就會得到一個**單**字符串。您需要將您的JTable構造函數傳遞給一個數組或'List '。 – 2015-03-18 22:07:19

+0

好吧,如果我寫了String [] columnNames = {container};不是一個數組?我已經嘗試過,但這仍然無法正常工作... – Ingram 2015-03-18 22:10:47