@ SuppressWarnings ( { "unchecked" , "rawtypes" } ) public static String [ ] showMultiInputDialog ( Component parentComponent , String title , String [ ] labels , String [ ] defaultValues , HashMap < String , String [ ] > fields2ValuesMap ) { Component [ ] valuesFields = new Component [ labels . length ] ; JPanel panel = new JPanel ( ) ; // panel.setPreferredSize(new Dimension(400, 300)); panel . setBorder ( BorderFactory . createEmptyBorder ( 10 , 10 , 10 , 10 ) ) ; String input [ ] = new String [ labels . length ] ; panel . setLayout ( new GridLayout ( labels . length , 2 , 5 , 5 ) ) ; for ( int i = 0 ; i < labels . length ; i ++ ) { panel . add ( new JLabel ( labels [ i ] ) ) ; boolean doneCombo = false ; if ( fields2ValuesMap != null ) { String [ ] values = fields2ValuesMap . get ( labels [ i ] ) ; if ( values != null ) { JComboBox < String > valuesCombo = new JComboBox <> ( values ) ; valuesFields [ i ] = valuesCombo ; panel . add ( valuesCombo ) ; if ( defaultValues != null ) { valuesCombo . setSelectedItem ( defaultValues [ i ] ) ; } doneCombo = true ; } } if ( ! doneCombo ) { valuesFields [ i ] = new JTextField ( ) ; panel . add ( valuesFields [ i ] ) ; if ( defaultValues != null ) { ( ( JTextField ) valuesFields [ i ] ) . setText ( defaultValues [ i ] ) ; } } } JScrollPane scrollPane = new JScrollPane ( panel ) ; scrollPane . setPreferredSize ( new Dimension ( 550 , 300 ) ) ; int result = JOptionPane . showConfirmDialog ( parentComponent , scrollPane , title , JOptionPane . OK_CANCEL_OPTION ) ; if ( result != JOptionPane . OK_OPTION ) { return null ; } for ( int i = 0 ; i < labels . length ; i ++ ) { if ( valuesFields [ i ] instanceof JTextField ) { JTextField textField = ( JTextField ) valuesFields [ i ] ; input [ i ] = textField . getText ( ) ; } if ( valuesFields [ i ] instanceof JComboBox ) { JComboBox < String > combo = ( JComboBox ) valuesFields [ i ] ; input [ i ] = combo . getSelectedItem ( ) . toString ( ) ; } } return input ; }
Create a simple multi input pane that returns what the use inserts .