Reputation: 41
!!!!!And I found the big problem, I'm overwriting the isModal method, which is already predefinied, therefore it wont hold and wait for the user input! slap myself in the face now :)!!!!!!
im doing a project in school about GUI. The main topic at the moment is 4 layer architect stuff, here we should use a JDialog to input information about a person, and thereby update a JList in the JFrame of the program.
My problem is, that when I use my add person button, the JDialog is set both visible and to modal, so it should wait with updating the JList on the JFrame, after the user pressed the add button on the JDialog.
But but but, it dosen't work, I talked to much of my teachers about it, and they cant figure out why it wont work.
The extra info is that I'm programming on a mac, but if the program runs on a windows machine it works.
I tried searching the specific problem around the web, but coulden't find anything related to JDialog java problems on mac.
Is there any of you who have experience this?
JFrame:
package gui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import model.Person;
import service.Service;
public class MainFrame extends JFrame {
private JList<Person> jlList;
private JScrollPane jsrPane;
private JButton addButton, updateButton;
private Controller controller;
private MyDialog myDialog;
public MainFrame(){
controller = new Controller();
this.setTitle("Marks lille MainFrame.");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(800, 600);
this.setLocation(300, 200);
this.setLayout(null);
jlList = new JList<>();
jlList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jsrPane = new JScrollPane(jlList);
jsrPane.setBounds(10, 10, 400, 500);
this.add(jsrPane);
addButton = new JButton("Add Person");
addButton.setBounds(420, 110, 100, 25);
addButton.addActionListener(controller);
this.add(addButton);
updateButton = new JButton("Update");
updateButton.setBounds(420, 150, 100, 25);
updateButton.addActionListener(controller);
this.add(updateButton);
}
private class Controller implements ActionListener{
public void actionPerformed(ActionEvent ae){
if(ae.getSource().equals(addButton)){
myDialog = new MyDialog("Add Person");
myDialog.setVisible(true);
System.out.println("Test");
if(myDialog.isModal()){
jlList.setListData(Service.getPersons().toArray(new Person[0]));
}
System.out.println("Test");
}else if(ae.getSource().equals(updateButton)){
Person ps = jlList.getSelectedValue();
myDialog = new MyDialog("Update Person");
myDialog.setVisible(true);
if(myDialog.isModal()){
Service.removePerson(ps);
jlList.setListData(Service.getPersons().toArray(new Person[0]));
}
}
}
}
}
JDialog:
package gui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import service.Service;
public class MyDialog extends JDialog {
private JButton okButton, cancelButton;
private JLabel lblName, lblTitle;
private JTextField txfName, txfTitle;
private JCheckBox ckbRetire;
private Controller controller;
private boolean modalResult;
public MyDialog(String title){
controller = new Controller();
this.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
this.setTitle(title);
setModalityType(JDialog.DEFAULT_MODALITY_TYPE);
this.setLayout(null);
this.setSize(300, 300);
this.setLocation(300, 200);
okButton = new JButton("Add");
okButton.setBounds(100, 250, 80, 25);
okButton.addActionListener(controller);
this.add(okButton);
cancelButton = new JButton("Cancel");
cancelButton.setBounds(180, 250, 80, 25);
cancelButton.addActionListener(controller);
this.add(cancelButton);
lblName = new JLabel("Name:");
lblName.setBounds(10, 10, 100, 25);
this.add(lblName);
txfName = new JTextField();
txfName.setBounds(5, 35, 200, 25);
this.add(txfName);
lblTitle = new JLabel("Title:");
lblTitle.setBounds(10, 70, 100, 25);
this.add(lblTitle);
txfTitle = new JTextField();
txfTitle.setBounds(5, 95, 200, 25);
this.add(txfTitle);
ckbRetire = new JCheckBox("retired");
ckbRetire.setBounds(10, 150, 100, 25);
this.add(ckbRetire);
}
private class Controller implements ActionListener{
public void actionPerformed(ActionEvent ae){
if(ae.getSource().equals(okButton)){
String name = txfName.getText().trim();
String title = txfTitle.getText().trim();
boolean retired = ckbRetire.isSelected();
Service.createPerson(name, title, retired);
modalResult = true;
MyDialog.this.setVisible(false);
}else if(ae.getSource().equals(cancelButton)){
modalResult = false;
MyDialog.this.setVisible(false);
}
}
}
public boolean isModal(){
return modalResult;
}
}
Main method:
package gui;
public class MainFrameApp {
/**
* @param args
*/
public static void main(String[] args) {
MainFrame frame = new MainFrame();
frame.setVisible(true);
}
}
If you need Service, Dao and Model classes, let me know.
Edit #1: Running on Java build 1.7.0_09-b05
Edit #2: Tried setting the super to the JDialog, and setting the JFrame as the super. Tried setModal, even though it's obsolete. Tried setting the modal when calling the JDialog, still no succes
Upvotes: 3
Views: 219
Reputation: 41
And I found the big problem, I'm overwriting the isModal method, which is already predefinied, therefore it wont hold and wait for the user input! slap myself in the face now :)
Upvotes: 1