Results 1 to 11 of 11
Thread: JDialog Modality Type help.
- 06-23-2010, 09:15 AM #1
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
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)
the code below does not freeze the JFrameJava Code:[b]from JFrame[/b] [i]this code does not freeze the JFrame[/i] new LoadDistinct(this, txtComponent, loadToCombo).setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
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.
any help would be greatly appreciated.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; }
and
Thanks in advance,
gejeLast edited by mine0926; 06-23-2010 at 09:44 AM.
-
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.
- 06-23-2010, 03:12 PM #3
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
Thanks for the reply. Um... can you give links on how to do this? Tutorial, i guest?
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....
gejeLast edited by mine0926; 06-23-2010 at 03:16 PM.
-
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)
- 06-24-2010, 02:42 AM #5
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
Unfortunately NO :(I take it you are familiar with calling a super constructor in a subclass's constructor, correct?
Is this correct? Because it is just same output.One constructor that would work would be to simply call super passing the calling JFrame as the only parameter.
Thanks for always helping me,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; }
geje
-
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!
- 06-24-2010, 04:29 AM #7
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
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,
gejeLast edited by mine0926; 06-24-2010 at 04:32 AM.
-
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.
- 06-24-2010, 06:34 AM #9
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
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)
- 06-24-2010, 06:38 AM #10
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
So look at your line 348 in the class pnlSearch. You are accessing a null value there.
- 06-24-2010, 06:45 AM #11
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
Thanks. Everything is working fine now. setModalityType should be place BEFORE pack() and setVisible().
Here is the code I change:
Thanks again,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);
geje
Similar Threads
-
JDialog
By frenk_castle in forum AWT / SwingReplies: 3Last Post: 05-11-2010, 12:28 PM -
Close JDialog
By sky in forum AWT / SwingReplies: 7Last Post: 11-16-2009, 06:01 PM -
using instanceof to get Object type and parent type?
By xcallmejudasx in forum New To JavaReplies: 2Last Post: 11-06-2008, 06:24 PM -
[SOLVED] Cast string type to int type
By GilaMonster in forum New To JavaReplies: 9Last Post: 09-17-2008, 10:43 AM -
help with jdialog
By leonard in forum AWT / SwingReplies: 1Last Post: 08-05-2007, 05:37 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks