如图所示,当对话框打开时,按钮栏有时无法正确显示。我认为由于我必须为DefaultTableModel
嵌入一个awt框架,所以会创建一些布局问题。
有什么方法可以解决这个问题吗?或者我应该使用jface checkboxtableviewer,但我找不到一个很好的例子。
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){
}
}
}
}
答案 0 :(得分:2)