【问题标题】:cant get array into table heading properly无法正确地将数组放入表格标题
【发布时间】:2023-03-07 01:09:01
【问题描述】:

我有以下代码:

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

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



}

【问题讨论】:

  • 以后,请发布格式良好的代码。如果我们能阅读您的代码,我们就能更好地理解它并为您提供帮助。
  • 我已经用更多信息更新了我的问题
  • 您仍然没有将 Array 或 Collection 传递给您的 JTable 构造函数。你正在传递一个字符串!如果它是一个列表,则将参数从Object 更改为List&lt;String&gt; 并将其传递给JTable,而不是toString()。这完全没有意义。
  • 而且您的代码格式仍然很糟糕。再次,请修复。您发布的所有左对齐代码 - 为什么?
  • 抱歉,我会解决这个问题,我对 Java 比较陌生,所以如果我遇到天真,我很抱歉。

标签: java arrays swing jtable heading


【解决方案1】:

你期望这会返回什么: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"}

【讨论】:

  • 我已经更新了我的问题。字母被替换说明了这个问题。实际的列名来自一个 url,我根据代码 String j = (String) list.get(2); 在 url 中取标题行
  • @MrAssistance:同样,当您在 anything 上调用 toString() 时,您会得到一个 single 字符串。您需要将一个数组或 List&lt;String&gt; 传递给您的 JTable 构造函数。
  • 好吧,如果我写了 String[] columnNames ={container};那不是一个数组吗?我已经尝试过了,但这仍然不起作用......
  • @MrAssistance:它是一个由一个项目,一个字符串组成的数组。您是否期待单列数据?我对此表示怀疑。所以不,这当然行不通。您需要传入一个有意义的数组**或List&lt;String&gt;,它将每个列标题作为自己的元素。
  • 你说得对,我正在接收输出,就好像它是一个项目,一个字符串。所以在第一部分我的 List container = Arrays.asList(items); ..容器作为列表传递,现在该列表的每个元素都需要分成单独的项目并传递到表中.....我需要用“,”分割每个元素并将其传递到新数组?
猜你喜欢
  • 2013-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多