File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Why can't I delete item form table.(SWT) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Why can Watch "Why can New topic
Author

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

Zhang Wendy
Greenhorn

Joined: Jun 23, 2006
Posts: 2
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

Joined: Jun 23, 2006
Posts: 2
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);
}
}
});
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why can't I delete item form table.(SWT)