使用JFace对话框按钮栏出现故障

时间:2015-02-04 16:46:46

标签: java dialog swt eclipse-rcp jface

如图所示,当对话框打开时,按钮栏有时无法正确显示。我认为由于我必须为DefaultTableModel嵌入一个awt框架,所以会创建一些布局问题。

Glitch with the Button bar

有什么方法可以解决这个问题吗?或者我应该使用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){
        }
    }



}
}

1 个答案:

答案 0 :(得分:2)

SWT / AWT桥不是您想要自愿使用的东西。除非你真的必须在SWT应用程序中使用现有的AWT UI,否则你应该远离它。这个article揭示了更多细节。

如果可以,请使用CheckboxTableViewer。要创建实例,请使用CheckboxTableViewer#newCheckList()。要在选中或取消选中某个元素时获得通知,请添加ICheckStateListenerJavaDoc列出了查询和操作其元素的已检查状态的所有方法。有关TableViewers的一般介绍,只需在网上搜索“JFace TableViewer”,您就会找到很多来源。