Java 如何在SWT中将按钮添加到表中?
为了澄清,我只是使用SWT,而不是JFace(试图一次学习一个GUI库,尽管这可能不明智)。给出了一个树编辑器的示例,我目前没有使用它 我遇到的问题是,在我的一生中,我似乎无法在第0行第0列中显示一个按钮: 在调试过程中我注意到的一件事是,检查按钮向下移动了一个;第1行中的第一个可见按钮实际上对应于项0 下面的代码是Scala的,尽管它非常基础,对于熟悉Java和SWT的人来说,应该不难理解;我很乐意接受Java的回答Java 如何在SWT中将按钮添加到表中?,java,scala,swt,Java,Scala,Swt,为了澄清,我只是使用SWT,而不是JFace(试图一次学习一个GUI库,尽管这可能不明智)。给出了一个树编辑器的示例,我目前没有使用它 我遇到的问题是,在我的一生中,我似乎无法在第0行第0列中显示一个按钮: 在调试过程中我注意到的一件事是,检查按钮向下移动了一个;第1行中的第一个可见按钮实际上对应于项0 下面的代码是Scala的,尽管它非常基础,对于熟悉Java和SWT的人来说,应该不难理解;我很乐意接受Java的回答 package com.bar.baz import org.eclip
package com.bar.baz
import org.eclipse.swt.widgets._
import org.eclipse.swt.custom.TableEditor
import org.eclipse.swt.layout.FillLayout
import org.eclipse.swt.SWT
object TableTest {
/**
* Launch the application.
*
* @param args
*/
def main(args: Array[String]) = {
try {
val nrows = 5
val ncols = 3
val display = new Display ()
val shell = new Shell (display)
shell.setLayout(new FillLayout())
val table: Table = new Table(
shell, SWT.BORDER /*| SWT.CHECK */| SWT.FULL_SELECTION | SWT.SINGLE
)
val editor: TableEditor = new TableEditor(table)
editor.horizontalAlignment = SWT.LEFT
editor.grabHorizontal = true
table.setLinesVisible(true)
table.setHeaderVisible(true)
for (ii <- 0 until ncols) {
val column: TableColumn = new TableColumn(table, SWT.NONE)
column.setText("Col " + ii.toString)
column.setWidth(40)
}
for (row <- 0 until nrows; col <- 0 until ncols) {
if (col == 0) {
new TableItem(table, SWT.NONE)
createDeleteRowButton(row, col)
}
else {
val item = table.getItem(row)
item.setText(col, s"$row, $col")
}
}
for (col <- 0 until ncols) {
table.getColumn(col).pack()
}
shell.pack ()
shell.open ()
while (!shell.isDisposed) {
if (!display.readAndDispatch ()) display.sleep ()
}
display.dispose ()
def createDeleteRowButton(row: Int, col: Int): Button = {
val delButton = new Button(table, SWT.CHECK)
val item = table.getItem(row)
editor.setEditor(delButton, item, col)
delButton
}
}
catch {
case e: Exception =>
e.printStackTrace()
}
}
}
package com.bar.baz
导入org.eclipse.swt.widgets_
导入org.eclipse.swt.custom.TableEditor
导入org.eclipse.swt.layout.FillLayout
导入org.eclipse.swt.swt
对象表测试{
/**
*启动应用程序。
*
*@param args
*/
def main(参数:数组[字符串])={
试一试{
val nrows=5
val ncols=3
val显示=新显示()
val外壳=新外壳(显示)
setLayout(新的FillLayout())
val表格:表格=新表格(
外壳,SWT.BORDER/*| SWT.CHECK*/| SWT.FULL|U SELECTION | SWT.SINGLE
)
val编辑器:表格编辑器=新表格编辑器(表格)
editor.horizontalAlignment=SWT.LEFT
editor.grabHorizontal=true
表.setLinesVisible(真)
表.setHeadServiceable(true)
对于(ii我将此作为一个答案发布,尽管它并不理想,因为它添加了一个空行,并且需要在实际应用程序中使用索引进行大量欺骗…但是它可以工作,除了当表的其余部分垂直滚动时按钮不会移动。这是“将所有内容向下移动一个”解决方案
请注意,它需要在单独的循环中提前创建表项
import org.eclipse.swt.widgets._
import org.eclipse.swt.custom.TableEditor
import org.eclipse.swt.layout.FillLayout
import org.eclipse.swt.SWT
object TableTest {
def main(args: Array[String]) = {
try {
val nrows = 5
val ncols = 3
val display = new Display ()
val shell = new Shell (display)
shell.setLayout(new FillLayout())
val table: Table = new Table(
shell, SWT.BORDER /*| SWT.CHECK */| SWT.FULL_SELECTION | SWT.SINGLE
)
val editor: TableEditor = new TableEditor(table)
editor.horizontalAlignment = SWT.LEFT
editor.grabHorizontal = true
table.setLinesVisible(true)
table.setHeaderVisible(true)
for (ii <- 0 until ncols) {
val column: TableColumn = new TableColumn(table, SWT.NONE)
column.setText("Col " + ii.toString)
column.setWidth(40)
}
for (row <- 0 until nrows) {new TableItem(table, SWT.NONE)}
for (row <- 0 until nrows; col <- 0 until ncols) {
if (col == 0) {
createDeleteRowButton(row, col)
}
else if (row < nrows - 1) {
val item = table.getItem(row+1)
item.setText(col, s"$row, $col")
}
}
shell.pack ()
shell.open ()
while (!shell.isDisposed) {
if (!display.readAndDispatch ()) display.sleep ()
}
display.dispose ()
def createDeleteRowButton(row: Int, col: Int): Button = {
val delButton = new Button(table, SWT.CHECK)
val item = table.getItem(row)
editor.setEditor(delButton, item, col)
delButton
}
}
catch {
case e: Exception =>
e.printStackTrace()
}
}
}
import org.eclipse.swt.widgets_
导入org.eclipse.swt.custom.TableEditor
导入org.eclipse.swt.layout.FillLayout
导入org.eclipse.swt.swt
对象表测试{
def main(参数:数组[字符串])={
试一试{
val nrows=5
val ncols=3
val显示=新显示()
val外壳=新外壳(显示)
setLayout(新的FillLayout())
val表格:表格=新表格(
外壳,SWT.BORDER/*| SWT.CHECK*/| SWT.FULL|U SELECTION | SWT.SINGLE
)
val编辑器:表格编辑器=新表格编辑器(表格)
editor.horizontalAlignment=SWT.LEFT
editor.grabHorizontal=true
表.setLinesVisible(真)
表.setHeadServiceable(true)
对于(ii我将此作为一个答案发布,尽管它并不理想,因为它添加了一个空行,并且需要在实际应用程序中使用索引进行大量欺骗…但是它可以工作,除了当表的其余部分垂直滚动时按钮不会移动。这是“将所有内容向下移动一个”解决方案
请注意,它需要在单独的循环中提前创建表项
import org.eclipse.swt.widgets._
import org.eclipse.swt.custom.TableEditor
import org.eclipse.swt.layout.FillLayout
import org.eclipse.swt.SWT
object TableTest {
def main(args: Array[String]) = {
try {
val nrows = 5
val ncols = 3
val display = new Display ()
val shell = new Shell (display)
shell.setLayout(new FillLayout())
val table: Table = new Table(
shell, SWT.BORDER /*| SWT.CHECK */| SWT.FULL_SELECTION | SWT.SINGLE
)
val editor: TableEditor = new TableEditor(table)
editor.horizontalAlignment = SWT.LEFT
editor.grabHorizontal = true
table.setLinesVisible(true)
table.setHeaderVisible(true)
for (ii <- 0 until ncols) {
val column: TableColumn = new TableColumn(table, SWT.NONE)
column.setText("Col " + ii.toString)
column.setWidth(40)
}
for (row <- 0 until nrows) {new TableItem(table, SWT.NONE)}
for (row <- 0 until nrows; col <- 0 until ncols) {
if (col == 0) {
createDeleteRowButton(row, col)
}
else if (row < nrows - 1) {
val item = table.getItem(row+1)
item.setText(col, s"$row, $col")
}
}
shell.pack ()
shell.open ()
while (!shell.isDisposed) {
if (!display.readAndDispatch ()) display.sleep ()
}
display.dispose ()
def createDeleteRowButton(row: Int, col: Int): Button = {
val delButton = new Button(table, SWT.CHECK)
val item = table.getItem(row)
editor.setEditor(delButton, item, col)
delButton
}
}
catch {
case e: Exception =>
e.printStackTrace()
}
}
}
import org.eclipse.swt.widgets_
导入org.eclipse.swt.custom.TableEditor
导入org.eclipse.swt.layout.FillLayout
导入org.eclipse.swt.swt
对象表测试{
def main(参数:数组[字符串])={
试一试{
val nrows=5
val ncols=3
val显示=新显示()
val外壳=新外壳(显示)
setLayout(新的FillLayout())
val表格:表格=新表格(
外壳,SWT.BORDER/*| SWT.CHECK*/| SWT.FULL|U SELECTION | SWT.SINGLE
)
val编辑器:表格编辑器=新表格编辑器(表格)
editor.horizontalAlignment=SWT.LEFT
editor.grabHorizontal=true
表.setLinesVisible(真)
表.setHeadServiceable(true)
对于(ii)我想你是在Linux上。不是吗?那么你使用的是哪种操作系统。不要将复选框列设为第一列。将其设为第二列、第三列或最后一列,然后进行检查。将scala代码转换为Java并发布,以便我可以直接复制并尝试修复代码。谢谢-我添加了Java代码。实际上,这是Windows 10。第一列不好有什么原因吗我还没有尝试过这个建议,但我认为第一行可能有问题,通过将所有其他列向下移动一行,我已经在演示中取得了一些初步成功,但我还没有在我的应用程序中测试过这一点,它会留下一整行空白。一个相关的问题-也许我应该稍后再问另一个问题?按钮不要与其他表项一起滚动;有没有办法使它们适当滚动?@Chandrayagk-我只是尝试更改了列;同样的问题似乎也发生了。我认为您是在Linux上。不是吗?那么您使用的是哪个操作系统。不要将复选框列设为第一列。将其设为第二列、第三列或最后一列,然后选中。将scala代码转换为Java并发布它,以便我可以直接复制并尝试修复代码。谢谢-我添加了Java代码。实际上,它是Windows 10。第一列不好有什么原因吗?我还没有尝试过这个建议,但我认为第一行可能有问题,并且通过移动所有其他列,在这个演示中取得了一些初步成功umns向下滚动了一行,但我还没有在我的应用程序中测试过这一点,它会留下一整行空白。一个相关的问题-也许我以后应该问另一个问题?按钮不会与表的其余条目一起滚动;有没有办法让它们适当地滚动?@ChandrayGk-我只是尝试过更改列;同样的问题似乎发生了。