先要设为table的layout
然后在其中建立TableColumn
最后在加入TableItem,也就是一个记录,一行
看看这里例子,是SWT/JFace in Action 书上的,推荐看下
//Set up the table layout
TableLayout layout = new TableLayout()
layout.addColumnData(new ColumnWeightData(33, 75, true))
layout.addColumnData(new ColumnWeightData(33, 75, true))
layout.addColumnData(new ColumnWeightData(33, 75, true))
Table table = new Table(parent, SWT.SINGLE)
table.setLayout(layout)
//Add columns to the table
TableColumn column1 = new TableColumn(table, SWT.CENTER)
TableColumn column2 = new TableColumn(table, SWT.CENTER)
TableColumn column3 = new TableColumn(table, SWT.CENTER)
TableItem item = new TableItem(table, SWT.NONE)
item.setText( new String[] { "column 1",
"column 2",
"column 3" } )
item = new TableItem(table, SWT.NONE)
item.setText( new String[] { "a", "b", "c" } )
不知道这个程序对你有没有帮助package swt
import org.eclipse.swt.SWT
import org.eclipse.swt.custom.ControlEditor
import org.eclipse.swt.custom.TableCursor
import org.eclipse.swt.custom.TableEditor
import org.eclipse.swt.custom.ViewForm
import org.eclipse.swt.events.FocusEvent
import org.eclipse.swt.events.FocusListener
import org.eclipse.swt.events.KeyEvent
import org.eclipse.swt.events.KeyListener
import org.eclipse.swt.events.ModifyEvent
import org.eclipse.swt.events.ModifyListener
import org.eclipse.swt.events.SelectionEvent
import org.eclipse.swt.events.SelectionListener
import org.eclipse.swt.widgets.Composite
import org.eclipse.swt.widgets.Display
import org.eclipse.swt.widgets.Shell
import org.eclipse.swt.widgets.Table
import org.eclipse.swt.widgets.TableColumn
import org.eclipse.swt.widgets.TableItem
import org.eclipse.swt.widgets.Text
public class SimpleTable2 {
/**
* Launch the application
* @param args
*/
private Shell shell
private Composite composite
private ViewForm viewForm
private Table table
private void createShell(){
shell = new Shell()
shell.setSize(500, 375)
shell.setText("SWT Application")
createViewForm()
}
private void createViewForm(){
viewForm=new ViewForm(shell,SWT.NONE)
createComposite()
viewForm.setContent(composite)
viewForm.pack()
}
private void createComposite(){
composite=new Composite(viewForm,SWT.NONE)
createTable()
composite.pack()
}
private void createTable() {
table=new Table(composite,SWT.FULL_SELECTION)
table.setHeaderVisible(true)
table.setLinesVisible(true)
String[] header=
for(int i=0i<header.lengthi++){
TableColumn column=new TableColumn(table,SWT.NONE)
column.setText(header[i])
}
TableItem tableItem=new TableItem(table,SWT.NONE)
tableItem.setText(new String[])
tableItem=new TableItem(table,SWT.NONE)
tableItem.setText(new String[])
for(int i=0i<table.getColumnCount()i++)
table.getColumn(i).pack()
table.pack()
//add ControlEditor on TableCursor
final TableCursor cursor=new TableCursor(table,SWT.NONE)
//cursor令使用左右键选择单元格成为可能
final ControlEditor editor=new ControlEditor(cursor)
editor.grabHorizontal=true
editor.grabVertical=true
cursor.addSelectionListener(new SelectionListener(){
public void widgetDefaultSelected(SelectionEvent e) {
//当触发此事件时才创建text(并为她添加事件),并用setFocus()把她固定住
final Text text=new Text(cursor,SWT.NONE)
TableItem row=cursor.getRow()
int column=cursor.getColumn()
text.setText(row.getText(column))
text.addKeyListener(new KeyListener(){
public void keyPressed(KeyEvent e) {
if(e.character==SWT.CR){
TableItem row=cursor.getRow()
int column=cursor.getColumn()
row.setText(column,text.getText())
text.dispose()
}
}
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
})
text.addFocusListener(new FocusListener(){
public void focusGained(FocusEvent e) {
// TODO Auto-generated method stub
}
public void focusLost(FocusEvent e) {
text.dispose() //目前认为可有可无
}
})
editor.setEditor(text)
text.setFocus() //将text固定于某个单元格上
}
public void widgetSelected(SelectionEvent e) {
table.setSelection(new TableItem[])
//当选中某行时,此行出现阴影
}
})
}
public static void main(String[] args) {
final Display display = Display.getDefault()
SimpleTable2 st=new SimpleTable2()
//
st.createShell()
st.shell.pack()
st.shell.open()
st.shell.layout()
while (!st.shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep()
}
}
}
如果是类似vue、react这样的框架中这种 *** 作相对来说简单很多,如果不使用框架,单纯的通过js实现就比较麻烦一点,代码我就不写了,比较麻烦,说一下思路吧。首先创建一个trNode,
let trNode = createElement("tr")
然后文本框里面的值你是能拿到的,拿到文本框的值以后将他拼一下,然后放到tr中
trNode.innerHTML = "<td>商品名</td><td>单价</td>..."
这样的形式,最后通过appendChild方法将这个trNode插入到table / tbody中
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)