• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Why can't I delete item form table.(SWT)

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for my poor English first. And my question is , if I use the code which change the height of the line ,I could not click the delete button to delete one line form the table. I don't known why, please help me !!

The following is the complete demo code :
........................................................
public class ww {

private Table table;
protected Shell shell;
private Composite composite;
/**
* Launch the application
* @param args
*/
public static void main(String[] args) {
try {
ww window = new ww();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* Open the window
*/
public void open() {
final Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}

/**
* Create contents of the window
*/
protected void createContents() {
shell = new Shell();
shell.setSize(471, 500);
shell.setText("SWT Application");
composite = new Composite(shell,SWT.NONE);
composite.setSize(471, 500);

final Group group = new Group(composite, SWT.NONE);
group.setText("Edit");
group.setBounds(48, 297, 383, 145);

table = new Table(group,SWT.BORDER | SWT.V_SCROLL | SWT.FULL_SELECTION);
table.setLinesVisible(true);
table.setHeaderVisible(true);
table.setBounds(10, 24, 373, 121);

String[] tableHeader = { "Num", " type ", " X ", " Y " };
for (int i = 0; i < tableHeader.length; i++) {
TableColumn tc = new TableColumn(table, SWT.NONE);
tc.setText(tableHeader);
tc.setMoveable(true);
}
for (int i = 0, n = table.getColumnCount(); i < n; i++) {
table.getColumn(i).pack();
}

//These code used to change the height of the line, error occours here。。。
// table.addListener(SWT.MeasureItem, new Listener() {
// public void handleEvent(Event event) {
// event.height = (int)(event.gc.getFontMetrics().getHeight() * 1.5);
// }
// });


final Button button = new Button(composite, SWT.NONE);
button.setText("+");
button.setBounds(263, 252, 48, 22);
button.addSelectionListener(new SelectionListener(){

public void widgetDefaultSelected(SelectionEvent e) {}

public void widgetSelected(SelectionEvent e) {
TableItem item = new TableItem(table,SWT.NONE);
item.setText(new String[] { 1 +"", "", "", "" });

}

});


final Button button_5 = new Button(composite, SWT.NONE);
button_5.setText("-");
button_5.setBounds(340, 249, 48, 28);
button_5.addSelectionListener(new SelectionListener(){

public void widgetDefaultSelected(SelectionEvent e) {}

public void widgetSelected(SelectionEvent e) {
if(table.getSelectionIndex() >=0)
{
table.clear(table.getSelectionIndex()); //clean the value
table.remove(table.getSelectionIndex());//delete the line。
}

}

});


}

}

This problem confused my for a long time. Thank you very much for answering..
 
Zhang Wendy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got the answer. hehe. Thank you all the same. I paste it below..

..................
final Listener changeTableItemHeight = new Listener() {
public void handleEvent(Event event) {
event.height = (int)(event.gc.getFontMetrics().getHeight() * 1.5);
}
};

table.addListener(SWT.MeasureItem, changeTableItemHeight);

final Button button_5 = new Button(composite, SWT.NONE);
button_5.setText("-");
button_5.setBounds(340, 249, 48, 28);
button_5.addSelectionListener(new SelectionListener(){

public void widgetDefaultSelected(SelectionEvent e) {}

public void widgetSelected(SelectionEvent e) {
int intSelection = table.getSelectionIndex();
if(intSelection >-1)
{
table.removeListener(SWT.MeasureItem, changeTableItemHeight);
table.clear(intSelection);
table.remove(intSelection);
table.addListener(SWT.MeasureItem, changeTableItemHeight);
}
}
});
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!