I'm self-teaching Java GUIs and am trying to implement an older text-based program I made into something with an actual menu. In using WindowBuilder on Eclipse for formatting and internet resources to help, but I've hit a bit of a roadblock.
我正在自学Java GUI,我正在尝试实现一个较旧的基于文本的程序,我用实际的菜单制作了一些东西。在Eclipse上使用WindowBuilder进行格式化和互联网资源帮助,但我遇到了一些障碍。
I've got a one-dimensional vector that can hold any number of strings. I'd like to display it in a table, but I'm not quite sure if I'm doing it right. All of my back-end stuff works fine, I'm just really struggling with the GUI. Any tips/corrections/resources you're willing to offer are greatly appreciated! I've been bumbling around for hours and I fear I've hit a wall. To be honest I'm having trouble testing the GUI within WindowBuilder as well, but that's another story. The relevant code in my GUI class is as follows:
我有一个可以容纳任意数量字符串的一维向量。我想将它显示在一张桌子上,但我不确定我是否正确行事。我的所有后端工作都运行良好,我只是在努力使用GUI。我们非常感谢您愿意提供的任何提示/更正/资源!我已经笨拙地笨拙几个小时,我担心我撞墙了。说实话,我在WindowBuilder中测试GUI时遇到了麻烦,但这是另一个故事。我的GUI类中的相关代码如下:
Vector<String> demo = new Vector<String>();
//nonsense elements just for the sake of debugging
demo.addElement("Line1");
demo.addElement("Line2");
demo.addElement("Line3");
demo.addElement("Line4");
demo.addElement("Line5");
demo.addElement("Line6");
table = new Table(this, SWT.BORDER | SWT.FULL_SELECTION);
table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
table.setHeaderVisible(true);
table.setLinesVisible(true);
TableItem item;
for(int i = 0; i < demo.size(); i++)
{
// Create a new TableItem for each line in the vector (each row)
item = new TableItem(table, SWT.NONE);
for (int j = 1; j <= demo.size(); j++) {
// Populate the item
item.setText(j - 1, demo.get(j));
}
}
1 个解决方案
#1
2
The problem is this line:
问题是这一行:
item.setText(j - 1, demo.get(j));
You only have one column (since you didn't create any yourself, the table assumes there is only one column), but using TableItem#setText(int, String)
sets the text in column i
(which is only equal to 0
for one of your items).
您只有一列(因为您没有自己创建,表假定只有一列),但使用TableItem#setText(int,String)设置列i中的文本(对于一列只能等于0)你的物品)。
So, if you've just got one column, use this:
所以,如果您只有一列,请使用:
item.setText(demo.get(j));
or
要么
item.setText(0, demo.get(j));
If you've got more colums, create them before adding items (new TableColumn(table, SWT.NONE)
), then add your items using:
如果你有更多的colums,请在添加项目之前创建它们(new TableColumn(table,SWT.NONE)),然后使用以下命令添加项目:
for(int i = 0; i < items.size(); i++)
{
TableItem item = new TableItem(table, SWT.NONE);
for(int j = 0; j < table.getColumnCount(); j++)
{
item.setText(j, "something here");
}
}
Then afterwards you have to pack()
the columns:
然后你必须打包()列:
for(TableColumn col : table.getColumns())
{
col.pack();
}
#1
2
The problem is this line:
问题是这一行:
item.setText(j - 1, demo.get(j));
You only have one column (since you didn't create any yourself, the table assumes there is only one column), but using TableItem#setText(int, String)
sets the text in column i
(which is only equal to 0
for one of your items).
您只有一列(因为您没有自己创建,表假定只有一列),但使用TableItem#setText(int,String)设置列i中的文本(对于一列只能等于0)你的物品)。
So, if you've just got one column, use this:
所以,如果您只有一列,请使用:
item.setText(demo.get(j));
or
要么
item.setText(0, demo.get(j));
If you've got more colums, create them before adding items (new TableColumn(table, SWT.NONE)
), then add your items using:
如果你有更多的colums,请在添加项目之前创建它们(new TableColumn(table,SWT.NONE)),然后使用以下命令添加项目:
for(int i = 0; i < items.size(); i++)
{
TableItem item = new TableItem(table, SWT.NONE);
for(int j = 0; j < table.getColumnCount(); j++)
{
item.setText(j, "something here");
}
}
Then afterwards you have to pack()
the columns:
然后你必须打包()列:
for(TableColumn col : table.getColumns())
{
col.pack();
}