Reputation: 514
As shown in figure the button bar sometimes does not appear properly as the dialog box opens. I think some layout out issues are created since I had to embed a awt frame for the DefaultTableModel
.
Is there any way to solve this problem? Or should I use a jface checkboxtableviewer, but I couldn't find a good example for that.
public class FunctionMaskDialog extends Dialog implements TableModelListener{
private Boolean[] functionChecked;
private JTable table;
private Table table_1;
/**
* Constructor Create the dialog.
*
* @param parentShell
*/
public FunctionMaskDialog(Shell parentShell) {
super(parentShell);
}
@Override
protected void configureShell(Shell shell) {
super.configureShell(shell);
shell.setText("Function Mask");
}
/**
* Create contents of the button bar.
*
* @param parent
*/
@Override
protected void createButtonsForButtonBar(Composite parent) {
createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
getButton(IDialogConstants.OK_ID).addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
setFunctionCheckedArray();
}
});
createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
getButton(IDialogConstants.CANCEL_ID).addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
// TODO Auto-generated method stub
}
});
}
/**
* Create contents of the dialog.
*
* @param parent
*/
@Override
protected Control createDialogArea(Composite parent) {
Composite container = new Composite(parent, SWT.EMBEDDED);
container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Frame frame = SWT_AWT.new_Frame(container);
frame.setLayout(null);
frame.setBackground(new Color(240, 240, 240));
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(30, 30, 494, 400);
// Table
table = new JTable();
scrollPane.setViewportView(table);
this.createFunctionTaskTable(table);
frame.add(scrollPane);
return container;
}
/**
* Return the initial size of the dialog.
*/
@Override
protected Point getInitialSize() {
return new Point(600, 530);
}
private void createFunctionTaskTable(JTable table){
// Model for Table
@SuppressWarnings("serial")
DefaultTableModel model = new DefaultTableModel() {
public Class<?> getColumnClass(int column) {
switch (column) {
case 0:
return Boolean.class;
case 1:
return String.class;
default:
return String.class;
}
}
@Override
public boolean isCellEditable(int row, int column) {
if (column == 1)
return false;
else
return true;
}
};
table.setModel(model);
model.addColumn("Select");
model.addColumn("Function Name");
table.getColumnModel().getColumn(0).setPreferredWidth(94);
table.getColumnModel().getColumn(1).setPreferredWidth(400);
// initialize chked array according to the size of task list.
functionChecked = GanttFrame.getFunctionCheckedArray();
model.addTableModelListener(this);
// Data Rows
for (int i = 0; i < GanttFrame.getFunctionTaskList().size(); i++) {
model.addRow(new Object[0]);
model.setValueAt(functionChecked[i], i, 0);
model.setValueAt(GanttFrame.getFunctionTaskList().get(i).getTaskName(), i, 1); // initialize the function names
}
}
private void setFunctionCheckedArray(){
Boolean[] tempArray = functionChecked;
for (int i = 0; i < table.getRowCount(); i++) {
functionChecked[i] = Boolean.valueOf(table.getValueAt(i, 0).toString());
}
if (!tempArray.equals(functionChecked)){
GanttFrame.setFunctionCheckedArray(functionChecked);
}
}
@Override
public void tableChanged(TableModelEvent event) {
DefaultTableModel model = (DefaultTableModel)event.getSource();
if (event.getColumn() == 0){
if (event.getFirstRow() == 0){
}
}
}
}
Upvotes: 0
Views: 149
Reputation: 21005
The SWT/AWT bridge isn't something that you want to use voluntarily. Unless you really have to bring an existing AWT UI to work within an SWT application you should stay away from it. This article reveals more details.
Use the CheckboxTableViewer
if you can. To create an instance, use CheckboxTableViewer#newCheckList()
. To get informed when an element is checked or unchecked, add an ICheckStateListener
. The JavaDoc lists all the methods to query and manipulate the checked state of its elements. For a general introduction to TableViewers just search the web for 'JFace TableViewer' and you'll find plenty of sources.
Upvotes: 2