Hallo zusammen,
ich habe ein kleines Problem. Habe mir selber ein klaines Projekt angelegt um etwas Java zu üben. Versuche schon die ganze Zeit die Daten in Form von Strings in den JTextFields aus einem JDialog in die JTextFields eines JFrames zu überführen.
Das JFrame ist meine Main-GUI und das JDialog wird über ein JMenuItem der Main-GUI gestartet. Beim klicken des Buttons "Übernehmen" sollen die Daten vom Dialog in die Main-GUI übernommen werden.
Klappt aber so wie ich mach nicht. Die Felder bleiben leer.
Hier der Code für die Main-GUI:
Der Code für den JDialog:
AddressbookEntry ist eine einfache Container-Klasse, die die Person und deren Adresse enthält.
Wenn ich vergessen habe etwas zu erwähnen, dass zur Lösung helfen würde sagt mir einfach Bescheid.
Was muss ich noch machen damit die Daten vom JDialog in das JFrame rüberwandern?
Vorab vielen Dank für die Hilfe.
Viele Grüße
Edfred
ich habe ein kleines Problem. Habe mir selber ein klaines Projekt angelegt um etwas Java zu üben. Versuche schon die ganze Zeit die Daten in Form von Strings in den JTextFields aus einem JDialog in die JTextFields eines JFrames zu überführen.
Das JFrame ist meine Main-GUI und das JDialog wird über ein JMenuItem der Main-GUI gestartet. Beim klicken des Buttons "Übernehmen" sollen die Daten vom Dialog in die Main-GUI übernommen werden.
Klappt aber so wie ich mach nicht. Die Felder bleiben leer.
Hier der Code für die Main-GUI:
Java:
// imports und package-Angabe
// Initialisierung von Feldern, wie z.B. Textfelder, Buttons, usw...
public class AddressbookGUI extends JFrame{
private static JTextField givennameField;
private static AddressbookEntry entry;
private static ArrayList<String> listData = new ArrayList<String>();
public AddressbookGUI() {
AddressbookGUI.entry = new AddressbookEntry();
}
public void createView() {
/*
* Initialisiert Lables und Textfelder mit Defaultwerten, Größen, Farbe, usw..
*/
initLabelsAndFields();
/* Die Methode füllt ein Panel mit Labels und Textfeldern der Eingabemaske.
* Versorgt das Panel mit einem Layout und fügt es dem JFrame hinzu.
*/
initContent();
/* Macht das Frame Sichtbar. Enthält noch Einstellungen wie Größe und
* Position des JFrames.
*/
initFrame();
}
private void initLabelsAndFields() {
//Beispiel für die Initialisierung von einem Lablel und einem Textfeld.
givennameLabel = new JLabel(Constants.GIVENNAME_LABEL_DE);
givennameLabel.setSize(Constants.LABEL_SIZE);
int count = 0;
givennameField = new JTextField(
entry.getPerson().getGivenname() == null ? "" : entry.getPerson().getGivenname());
givennameField.setBackground(count%2==0 ? Constants.GRAY : Constants.WHITE);
count += 1;
// usw....
}
private void initContent() {
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
mainContentPanel = new JPanel();
mainContentPanel.setLayout(gbl);
picturePanel = new JPanel();
/*
* If no entries available, no information should be available.
* mainContentPanel can be set to not enabled.
*/
if(listData.size() != 0) {
int countY = 0;
int none = GridBagConstraints.NONE;
int horizontal = GridBagConstraints.HORIZONTAL;
constraints.fill = horizontal;
constraints.gridx = 0;
constraints.gridy = ++countY;
constraints.anchor = GridBagConstraints.ABOVE_BASELINE_LEADING;
constraints.weightx = 1.5;
constraints.gridwidth = 2;
constraints.insets = new Insets(5, 5, 5, 50);
mainContentPanel.add(givennameLabel,constraints);
constraints.fill = horizontal;
constraints.gridx = 1;
constraints.gridy = countY;
constraints.insets = new Insets(5, 50, 5, 50);
mainContentPanel.add(givennameField, constraints);
//usw....
} else mainContentPanel.setEnabled(false);
listOfEntries = new JList(listData.toArray());
listOfEntries.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
listOfEntries.setLayoutOrientation(JList.VERTICAL);
listOfEntries.setSize(600, 800);
listOfEntries.setFixedCellWidth(200);
listOfEntries.setFixedCellHeight(30);
listScrollPane = new JScrollPane(listOfEntries,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
listScrollPane, mainContentPanel);
split.setOneTouchExpandable(true);
split.setDividerLocation(175);
final DefaultListModel listModel = new DefaultListModel();
listModel.addElement(listData);
getContentPane().add(split);
}
private void initFrame() {
/*
* Layout für das Mainframe
*/
getContentPane().setLayout(new BorderLayout());
setTitle(Constants.FRAME_TITLE);
setSize(Constants.FRAME_SIZE);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setDefaultLookAndFeelDecorated(true);
setLocation( (Constants.SCREEN_SIZE.width - getSize().width)/2,
(Constants.SCREEN_SIZE.height - getSize().height)/2);
setVisible(Constants.FRAME_VISIBILITY);
myMenuBar = new JMenuBar();
myMenuBar.setBackground(Constants.GRAY);
file = new JMenu(Constants.MENU_FILE_DE);
file.setBackground(Constants.GRAY);
/* Beim Klicken auf newPerson öffnet sich das JDialog.*/
newPerson = new JMenuItem(Constants.MENU_NEW_PERSON_DE);
newPerson.addActionListener(new NewPersonActionListenerImpl(newPerson));
file.add(newPerson);
}
// Jetzt folgen nur noch Getter- und Setter-Methoden und die Main-Methode zum Testen
Der Code für den JDialog:
Java:
public class NewPersonEntryGUI implements ActionListener{
private JTextField givennameField, secondGivennameField, surenameField, birthdayField,
streetField, houseNumberField, placeField, zipField,
emailField, phoneField, mobileField;
private JLabel givennameLabel, secondGivennameLabel, surenameLabel, birthdayLabel,
streetLabel, houseNumberLabel, placeLabel, zipLabel,
emailLabel, phoneLabel, mobileLabel, pictureLabel;
private JPanel mainContentPanel;
private JPanel picturePanel;
private JPanel buttonPanel;
private JButton add, cancel, pictureButton;
private JFileChooser pictureChooser;
private ImageIcon pictureIcon;
private JLabel pictureContainer;
private static AddressbookEntry entry;
private JDialog dialog = new JDialog();
/**
*
*/
private static final long serialVersionUID = -9135804139088796764L;
public NewPersonEntryGUI() {}
public void initDialog() {
dialog.setLayout(new BorderLayout());
dialog.setTitle(Constants.NEW_PERSON_TITLE_DE);
dialog.setSize(Constants.NEW_PERSON_FRAME_SIZE);
dialog.setLocation((Constants.SCREEN_SIZE.width - dialog.getSize().width)/2,
(Constants.SCREEN_SIZE.height - dialog.getSize().height)/2);
dialog.setVisible(Constants.NEW_PERSON_VISIBILITY);
dialog.setAlwaysOnTop(false);
dialog.setModal(false);
initContent();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.validate();
dialog.repaint();
}
private void initContent() {
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
mainContentPanel = new JPanel();
mainContentPanel.setLayout(gbl);
givennameLabel = new JLabel(Constants.GIVENNAME_LABEL_DE);
givennameField = new JTextField();
buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
buttonPanel.add(add);
buttonPanel.add(cancel);
cancel.addActionListener(this);
pictureButton.addActionListener(this);
add.addActionListener(this);
int countY = 0;
int none = GridBagConstraints.NONE;
int horizontal = GridBagConstraints.HORIZONTAL;
constraints.fill = horizontal;
constraints.gridx = 0;
constraints.gridy = ++countY;
constraints.anchor = GridBagConstraints.ABOVE_BASELINE_LEADING;
constraints.weightx = 1.5;
constraints.gridwidth = 2;
constraints.insets = new Insets(5, 5, 5, 50);
mainContentPanel.add(givennameLabel,constraints);
constraints.fill = horizontal;
constraints.gridx = 1;
constraints.gridy = countY;
constraints.insets = new Insets(5, 50, 5, 50);
mainContentPanel.add(givennameField, constraints);
dialog.getContentPane().add(mainContentPanel, BorderLayout.CENTER);
dialog.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
}
public static void main(String[] args) {
new NewPersonEntryGUI().initDialog();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(cancel)) {
dialog.setVisible(false);
}
//Hier sollen eigentlich die Daten vom JDialog in das JFrame rüberwandern. Tun sie aber nicht.
else if(e.getSource().equals(add)) {
if(!givennameField.getText().equals("")
&& givennameField.getText().length() != 0) {
AddressbookGUI.getEntry().getPerson().setGivenname(givennameField.getText());
AddressbookGUI.getListData().add(givennameField.getText());
AddressbookGUI.getGivennameField()
.setText(givennameField.getText());
}
dialog.setVisible(false);
}
}
}
AddressbookEntry ist eine einfache Container-Klasse, die die Person und deren Adresse enthält.
Wenn ich vergessen habe etwas zu erwähnen, dass zur Lösung helfen würde sagt mir einfach Bescheid.
Was muss ich noch machen damit die Daten vom JDialog in das JFrame rüberwandern?
Vorab vielen Dank für die Hilfe.
Viele Grüße
Edfred