import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
public class Dialog2 {
public static void main(String[] args) {
String input =
JOptionPane.showInputDialog(
null,
"Spalten",
"Titel",
JOptionPane.INFORMATION_MESSAGE);
System.out.println(input);
//Anzahl der Textfelder einlesen
int textFieldCnt = Integer.parseInt(input);
//Standardmäßige Breite der Textfelder
int defaultTextfieldSize = 10;
String[] fieldNames = new String[textFieldCnt];
int[] fieldCols = new int[textFieldCnt];
Arrays.fill(fieldCols, defaultTextfieldSize);
for (int i = 1; i < textFieldCnt; i++) {
fieldNames[i] = "Spalte " + i + ": ";
}
new Dialog2().doIt(fieldNames, fieldCols);
}
/**
* @param fieldNames
* @param fieldCols
*/
private void doIt(String[] fieldNames, int[] fieldCols) {
// TODO Auto-generated method stub
new CustomDialog("Wertebereich der Spalten", null, fieldNames, fieldCols).show();
}
class CustomDialog extends JDialog {
private int textFieldCnt;
private JTextField[] textFields;
private JLabel[] labels;
private JButton btnCancel;
private JButton btnOk;
private JPanel componentPanel;
private JPanel controlsPanel;
private JScrollPane scrollPane;
public CustomDialog(
String title,
Frame owner,
String[] textFieldNames,
int[] fieldCols) {
super(owner);
setSize(320, 240);
setTitle(title);
getContentPane().setLayout(new BorderLayout());
textFields = new JTextField[textFieldNames.length];
labels = new JLabel[textFieldNames.length];
componentPanel = new JPanel();
GridLayout grid = new GridLayout(fieldCols.length, 2);
componentPanel.setLayout(grid);
controlsPanel = new JPanel();
scrollPane = new JScrollPane();
scrollPane.setSize(300, 200);
scrollPane.getViewport().add(componentPanel);
getContentPane().add(scrollPane, BorderLayout.CENTER);
generatetextFields(textFieldNames, fieldCols);
btnOk = new JButton("OK");
btnOk.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Sammle die Textfeldinhalte ein und tue etwas ...
setVisible(false);
dispose();
System.out.println("Verarbeite Daten ...");
}
});
btnCancel = new JButton("Cancel");
btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
dispose();
//Test
System.exit(0);
}
});
controlsPanel.add(btnOk);
controlsPanel.add(btnCancel);
getContentPane().add(controlsPanel, BorderLayout.SOUTH);
}
/**
* @param textFieldNames
* @param fieldCols
*/
private void generatetextFields(
String[] textFieldNames,
int[] fieldCols) {
for (int i = 1; i < fieldCols.length; i++) {
labels[i] = new JLabel(textFieldNames[i]);
textFields[i] = new JTextField(fieldCols[i]);
componentPanel.add(labels[i]);
componentPanel.add(textFields[i]);
}
}
};
}