3

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
  • 193
  • 2
  • 4
  • 13

2 Answers2

7

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);, then button in the toolbar (with X) to hide JDialog too

  • then you can to call from actionPerformed only myDialog.setVisible(true) and ModalityType too if required, setVisible shoud be wrapped in invokeLater(), instead of creating a new instance (new Dialogz(this, true); )

  • JButton placed in JDialog will be called only myDialog.setVisible(false)

  • example corresponding with your logics

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • thanks 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) – mopr mopr Nov 08 '12 at 13:03
  • again 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 – mKorbel Nov 08 '12 at 13:26
  • thankz. 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 – mopr mopr Nov 08 '12 at 13:58
  • `but i can't. truthfully my main frame have many button` :-) hehehe no issue, put all these `JPanels` to `CardLayout` and to switch betweens views (call `JDialog.pack()` if you want to change/fits `JDialog` to the `JPanels Dimension`), [important info, you have to calculating with OutOfMemory](http://stackoverflow.com/questions/6309407/remove-top-level-container-on-runtime), don't forget to `close()` all `Resultset` and `Statement` in `finally block`, otherwise these `Object` stays in JVM memory too and never will be `GC'ed` – mKorbel Nov 08 '12 at 14:08
  • i 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.. ~_~ – mopr mopr Nov 08 '12 at 14:16
  • hehe.. its okay.. at least i know how to do it.. thanks a lot.. ^^ thanks – mopr mopr Nov 08 '12 at 14:35
1

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();
        }
    }
mopr mopr
  • 193
  • 2
  • 4
  • 13