Results 1 to 11 of 11
  1. #1
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default JDialog Modality Type help.

    Hi members, My problem now is setting the modality of a JDialog. I use this JDialog to load disticnt value for a field selected. This jDialog is being called in JFrame. I would like the JFrame to 'freeze' while JDialog is open and 'unfreeze' when JDialog is closed.

    I already try to:
    *Set the modalityType of the JDialog through its property.
    *Set it through code.(below are the codes i tried)
    Java Code:
    [b]from JFrame[/b]
    [i]this code does not freeze the JFrame[/i]
        new LoadDistinct(this, txtComponent, loadToCombo).setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
    the code below does not freeze the JFrame
    Java Code:
    public class LoadDistinct extends javax.swing.JDialog{
        JFrame caller;
        JTextField txtFocus;
        public LoadDistinct(JFrame caller, JTextField txtFocus, String sql)
        {   initComponents();
            MaterialsQuery.selectDistinct(sql, jList1); <--- Load disticnt record
            //.... some layout
            pack(); <-- Dont know if this might affect the JDialog's modality :)
    
            this.setVisible(true);
            this.caller = caller;
            this.txtFocus = txtFocus;
            [b]this.setModalityType(ModalityType.APPLICATION_MODAL);[/b]
        }

    the code freeze the JFrame but when I click/select from JList it does not do anything. I think it freeze JDialog too.
    Java Code:
    public class LoadDistinct extends javax.swing.JDialog{
        JFrame caller;
        JTextField txtFocus;
        public LoadDistinct(JFrame caller, JTextField txtFocus, String sql)
        {   initComponents();
            MaterialsQuery.selectDistinct(sql, jList1); <--- Load disticnt record
            //.... some layout
            pack();
    
            [b]this.setModalityType(ModalityType.APPLICATION_MODAL);[/b]
            this.setVisible(true);
            this.caller = caller;
            this.txtFocus = txtFocus;
        }
    any help would be greatly appreciated.
    and
    Thanks in advance,
    geje
    Last edited by mine0926; 06-23-2010 at 09:44 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You may wish to call the super JDialog constructor as the first line of your LoadDistinct constructor, and most importantly pass the JFrame reference to super in this constructor so that the JDialog knows which JFrame to "freeze". You also might wish to do long-acting actions, such as any database actions, in a background thread such as a SwingWorker.

  3. #3
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Quote Originally Posted by Fubarable View Post
    You may wish to call the super JDialog constructor as the first line of your LoadDistinct constructor, and most importantly pass the JFrame reference to super in this constructor so that the JDialog knows which JFrame to "freeze".
    Thanks for the reply. Um... can you give links on how to do this? Tutorial, i guest?

    Quote Originally Posted by Fubarable View Post
    You also might wish to do long-acting actions, such as any database actions, in a background thread such as a SwingWorker.
    Would love to do that. Actually you have told me that on my past post. Maybe I will do that some other time. As you can see, I am having problems coding thing that should be basic. I think that I am a student again since I did not use java for 2 years :o

    So I am learning basics here again and after I have done with here, I will also explore SwingWorker too. Anyway, Thanks for pointing me....

    geje
    Last edited by mine0926; 06-23-2010 at 03:16 PM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by mine0926 View Post
    Thanks for the reply. Um... can you give links on how to do this? Tutorial, i guest?
    I take it you are familiar with calling a super constructor in a subclass's constructor, correct? If not the Sun Java constructor tutorial will show you how to do this. As for the JDialog constructor signature, the JDialog API will show you what is acceptable. One constructor that would work would be to simply call super passing the calling JFrame as the only parameter.

    For other options, look here: JDialog (Java Platform SE 6)

  5. #5
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    I take it you are familiar with calling a super constructor in a subclass's constructor, correct?
    Unfortunately NO :(

    One constructor that would work would be to simply call super passing the calling JFrame as the only parameter.
    Is this correct? Because it is just same output.
    Java Code:
    public class pnlSearch extends javax.swing.JDialog{
        JFrame caller;
        JTextField txtFocus;
    
        public pnlSearch(JFrame caller, JTextField txtFocus, String sql)
        {   super (caller);
            initComponents();
    
            MaterialsQuery.selectDistinct(sql, jList1);
            ...
            this.setModalityType(ModalityType.APPLICATION_MODAL);
            this.setVisible(true);
            this.caller = caller;
            this.txtFocus = txtFocus;
        }
    Thanks for always helping me,
    geje

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Your best bet is to create a small compilable program that demonstrates your problem. Please look at link 3 in my signature about creating and posting an SSCCE.

    Much luck!

  7. #7
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    here is the example.
    At class LoadDistinct it calls method selectDistinct() from MaterialsQuery class.
    I change it to simply adding list in JList.

    Hopefully, I had put everything that might be connected in my problem.

    thanks again,
    geje
    Attached Files Attached Files
    Last edited by mine0926; 06-24-2010 at 04:32 AM.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Your code for others to see (and I suggest you post code here with code tags if possible rather than upload text files):

    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    
    public class FrmMaterials extends JFrame {
       JButton btn = new JButton("Load JDialog");
       JTextField txtMatCode = new JTextField();
       JTextField txtExtra = new JTextField();
       JPanel p = new JPanel();
    
       public FrmMaterials() {
          btn.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent evt) {
                btnActionPerformed(evt);
             }
          });
    
          this.setDefaultCloseOperation(EXIT_ON_CLOSE);
          this.add(p);
          p.setLayout(new BorderLayout());
          p.add(txtMatCode, BorderLayout.LINE_START);
          p.add(btn, BorderLayout.LINE_END);
          p.add(txtExtra, BorderLayout.PAGE_END);
          pack();
       }
    
       private void btnActionPerformed(ActionEvent act) {
          new LoadDistinct(this, txtMatCode);
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                new FrmMaterials().setVisible(true);
             }
          });
          System.out.println("running");
       }
    
    }
    
    class LoadDistinct extends JDialog {
       JList lists = new JList();
    
       public LoadDistinct(JFrame caller, JTextField aText) {
          super(caller);
    
          this.add(lists);
          addToLists();
          this.setVisible(true);
          pack();
          this.setModalityType(ModalityType.APPLICATION_MODAL);
       }
    
       void addToLists() {
          DefaultListModel lm = new DefaultListModel();
          for (int i = 0; i < 10; i++) {
             lm.addElement(i);
          }
          lists.setModel(lm);
       }
    }

    One problem is that your calling setVisible(true) on your JDialog before setting the modality type, and this is a problem because the dialog locks the frame on this call, again setVisible(true). But of course at the time that line is reached, the dialog doesn't know that it's supposed to do this.

    Solution: call setVisible after setting the modality type and also after adding all components and packing the dialog.

  9. #9
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Thanks, Have tried that but what happen is I cannot select from JList. It gives me error.
    Just like the result on my first post.
    ERROR MESSAGE: (first three lines)
    Java Code:
             Exception occurred during event dispatching:
             java.lang.NullPointerException
                at projpoms.pnlSearch.jList1ValueChanged(pnlSearch.java:348)

  10. #10
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    So look at your line 348 in the class pnlSearch. You are accessing a null value there.

  11. #11
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Thanks. Everything is working fine now. setModalityType should be place BEFORE pack() and setVisible().

    Here is the code I change:
    Java Code:
             class LoadDistinct extends JDialog {
               JList lists = new JList();
    
               public LoadDistinct(JFrame caller, JTextField aText) {
                  super(caller);
    
                  this.setModalityType(ModalityType.APPLICATION_MODAL);
                  this.add(lists);
                  addToLists();
                  pack();
                  this.setVisible(true);
    Thanks again,
    geje

Similar Threads

  1. JDialog
    By frenk_castle in forum AWT / Swing
    Replies: 3
    Last Post: 05-11-2010, 12:28 PM
  2. Close JDialog
    By sky in forum AWT / Swing
    Replies: 7
    Last Post: 11-16-2009, 06:01 PM
  3. using instanceof to get Object type and parent type?
    By xcallmejudasx in forum New To Java
    Replies: 2
    Last Post: 11-06-2008, 06:24 PM
  4. [SOLVED] Cast string type to int type
    By GilaMonster in forum New To Java
    Replies: 9
    Last Post: 09-17-2008, 10:43 AM
  5. help with jdialog
    By leonard in forum AWT / Swing
    Replies: 1
    Last Post: 08-05-2007, 05:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •