用不同的对象填充java表单

时间:2014-07-09 14:19:17

标签: java swing

我试图在运行时填充具有不同对象的表单,具体取决于类型列表中的值(下面为mTypes)。最初,我只考虑两种类型(JComboBoxJTextField),但稍后会将其扩展为大约7种表单元素。到目前为止我的代码创建并显示对话框; 我应该如何初始化和收集值?

private String[] showInputDialog(String mTitle, String[] mTitles, int[] mTypes)  {
        // mTitle         --> Dialog Title
        // mTitles        --> Fields Titles (labels)
        // mTypes         1 - TextField
        //                2 - ComboBox      
        JDialog dialog = new JDialog(this, Dialog.ModalityType.APPLICATION_MODAL);
        JLabel lblTemp;
        ArrayList<Object> mWidget = new ArrayList<Object>(); //widget to be textfield or combobox depending on int[] mTypes
        String[] RetValue = new String[mTitles.length]; //will hold and return the values in the different fields...


        JPanel pnlParent = new JPanel();
        pnlParent.setLayout(new BorderLayout(20, 20));
        lblTemp = new JLabel("  " + mTitle + "  ");
        pnlParent.add(lblTemp, BorderLayout.PAGE_START);
        lblTemp = new JLabel("   ");
        pnlParent.add(lblTemp, BorderLayout.LINE_START);
        lblTemp = new JLabel("   ");
        pnlParent.add(lblTemp, BorderLayout.LINE_END);

        JPanel mainPanel = new JPanel(new GridLayout(mTitles.length, 2, 10, 10));
        pnlParent.add(mainPanel, BorderLayout.CENTER);

        for (int i = 0; i < mTitles.length; i++) {
            lblTemp = new JLabel(mTitles[i]);
            lblTemp.setHorizontalAlignment(JLabel.LEFT);
            mainPanel.add(lblTemp);
            switch (mTypes[i]) {

                case 1: mWidget.add(new JTextField());
                        break;
                case 2: mWidget.add(new JComboBox());
                        break;
                default: //do something;
                        break;
                mainPanel.add(mWidget.get(i));
            }
        }

1 个答案:

答案 0 :(得分:2)

某些时候,某种用户操作(confirm按钮?)会导致对话框返回其内容。然后,您将执行与此类似的操作(假设您在其confirm侦听器中使用actionPerformed按钮):

public void actionPerformed(ActionEvent ae) {
    int i=0;
    for (Object o : mWidget) {
        if (o instanceof JTextField) { 
             retValue[i++] = ((JTextField)o).getText();
        } else if (o instanceof JComboBox) { 
             retValue[i++] = ((JComboBox)o).getSelectedItem(); 
        }
    }
}

您可以使用JTextFieldJComboBox的共同超类:JComponent。这样会留下ArrayList<JComponent> mWidget,看起来好一点。

另请注意,这是不是最干净的做事方式。您可以将这些内部JTextField / JComboBox包装在实现了MyWidget的子类中,并使用以下定义:

// MyWidget.java
public interface MyWidget {
     Object getValue();
}

// TextWidget.java
public class TextWidget extends JTextField implements MyWidget {
     // (add constructors here)
     public void setValue(Object value) { setText(value.toString()); }
     public Object getValue() { return getText(); }
}

// ComboWidget.java
public class ComboWidget extends JComboBox implements MyWidget {
     // (add constructors here)
     public void setValue(Object value) { setSelectedItem(value); }
     public Object getValue() { return getSelectedItem(); }
}

这样您就可以声明ArrayList<MyWidget>,当您需要收集所有结果时,不再需要使用演员表或instanceof