how to close jdialog from a button?
Solution 1
can i change the modal of dialog after i create instance of it?
yes you can to change setModal
or ModalityTypes
on runtime, but for code in this form doesn't make me any sence
how to close jdialog from that button (inside jdialog)?
in this case doesn't matter if you'll to call setVisible
or dispose()
create
JDialog
only one time,create that as local variable
change
myDialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
, thenbutton
in thetoolbar
(withX
) to hideJDialog
toothen you can to call from
actionPerformed
onlymyDialog.setVisible(true)
andModalityType
too if required, setVisible shoud be wrapped ininvokeLater()
, instead of creating a new instance (new Dialogz(this, true);
)JButton
placed inJDialog
will be called onlymyDialog.setVisible(false)
Solution 2
somebody told me..
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
public class Mainz extends JFrame implements ActionListener{
JButton showDialog = new JButton("show dialog");
public Mainz() {
setLayout(new FlowLayout());
showDialog.addActionListener(this);
add(showDialog);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
new Dialogz(this, false);
setEnabled(false);
}
public static void main(String[]args){
new Mainz();
}
}
class Dialogz extends JDialog{
JButton close = new JButton("close");
public Dialogz(JFrame owner,boolean modal) {
super(owner, modal);
add(close);
setLocationRelativeTo(owner);
setVisible(true);
close.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
closez();
}
});
}
void closez(){
System.out.println("before ="+getModalityType());
setModal(true);
System.out.println("after ="+getModalityType());
getOwner().setEnabled(true);
Dialogz.this.dispose();
}
}
Related videos on Youtube
mopr mopr
Updated on June 11, 2022Comments
-
mopr mopr almost 2 years
i have a Jframe (Mainz),
it have a button (showDialog),
when user click the button,
jdialog (Dialogz) will show,
that jdialog have a button
- how to close jdialog from that button (inside jdialog)?
- can i change the modal of dialog after i create instance of it?
i need to block the owner of that jdialog
heres i try ...
import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; public class Mainz extends JFrame implements ActionListener{ JButton showDialog = new JButton("show dialog"); public Mainz() { setLayout(new FlowLayout()); showDialog.addActionListener(this); add(showDialog); setVisible(true); } @Override public void actionPerformed(ActionEvent e) { new Dialogz(this, true); } public static void main(String[]args){ new Mainz(); } } class Dialogz extends JDialog{ JButton close = new JButton("close"); public Dialogz(JFrame owner,boolean modal) { super(owner, modal); System.out.println(this.getModalityType()); add(close); setLocationRelativeTo(owner); setVisible(true); close.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae){ closez(); } }); } void closez(){ setModal(false); this.dispose(); System.out.println("Method Done"); } }
thanks a lot for any kind of help
-
mopr mopr over 11 yearsthanks before.. my code just to make it look simple.. (however it still feel hard to me) my dialog later will related with database (that can change a value from my dialog).. so i need to dispose and create new dialog again.. can you give me the code to close that dialog and change the modal in method closez(from my post code)
-
mKorbel over 11 yearsagain create this JDialog only one time, put there required JComponents for Database updates, put data to JComponents in JDialog, show dialog, for modality to check setModal(true/false) or similair methods for setModalityTypes
-
mopr mopr over 11 yearsthankz. but i can't. truthfully my main frame have many button.. one of it will show another jdialog. that determine the ui of my problem jdialog(dialog that contain close problem).. i already write my code. (that so many) if i try to create jdialog only one time i think it will create a bug in my case.. however maybe i can handle the bug.. put component that have relation with database it same with read,coding and think from start again. so i can't.. maybe it can,but it will take more long time. can i do this with create a new dialog every user click the button? thanks a lot for the response
-
mKorbel over 11 years
but i can't. truthfully my main frame have many button
:-) hehehe no issue, put all theseJPanels
toCardLayout
and to switch betweens views (callJDialog.pack()
if you want to change/fitsJDialog
to theJPanels Dimension
), important info, you have to calculating with OutOfMemory, don't forget toclose()
allResultset
andStatement
infinally block
, otherwise theseObject
stays in JVM memory too and never will beGC'ed
-
mopr mopr over 11 yearsi can't use card layout in my case T-T ... another side, later i plan to learn 3d that contain a value from database.. then put it in my problem dialog.. manage the value in dialog a, the view in dialog b.. so i need to create a new jdialog every time user click the button.. ~_~