-2

I am creating an application that has 1 JFrame java file and 1 JDialog java file. In the JFrame, I have a button and when pressed I want it to display what I have designed in the JDialog.

So for example, my JFrame java file is called MMainView.java and my JDialog is called OptionView.java. So when the button in MMainView.java is pressed I want to display the JDialog I have designed in OptionView.java.

So in my MMainView.java file, I have a function that is called when that button is pressed. How do I display the dialog in OptionView.java?

SOLVED

For those wondering. This is what I did:

private JDialog optionView; ~~> JDialog Declaration 
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:          
           if (optionView == null) {
        JFrame mainFrame = myApp.getApplication().getMainFrame();
        optionView = new OptionView(mainFrame, true);
        optionView.setLocationRelativeTo(mainFrame);
    }
    myApp.getApplication().show(optionView);
}  
user2771150
  • 722
  • 4
  • 10
  • 33

3 Answers3

1

Sounds like you want to create an ActionListener for your button, and set the visibility of the JDialog to true when you press the button.

Something on these lines:

    final JButton button = new JButton();

    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionevent)
        {
            //set the visibility of the JDialog to true in here
        }
    });
Kakalokia
  • 3,191
  • 3
  • 24
  • 42
0

Let's say your button is called myBtn.

The class should look like this.

public class MMainView extends JFrame
    implements ActionListener

You should use a listener for the button.

JButton myBtn = new JButton();
myBtn.addActionListener(this);

And finally:

public void actionPerformed(ActionEvent e) {
    if (e.getSource() == myBtn) {
        new OptionView();

You don't really need the if, it's just in case you want to add more buttons for the actionPerformed.

0

First register the button in "MainView.java", like below.

b1.addActionListener(this);
b1.setName("OpenJDialog"); 
//this is to read in actionperformed method incase you have more than one button

// in action performed method call the dialogue class
public void actionPerformed(ActionEvent ae){

  JButton jb = (JButton)ae.getSource();
  String str = jb.getName();
  if(str.equals("OpenJDialog"){
     new OptionView(); 
      //I  am assuming u are configuring jdialog content in OptionView constructor
   }
}
Ashu Phaugat
  • 632
  • 9
  • 23
  • When I try to create an object of OptionView it asks for two parameters, Frame Parent and Boolean Modal. What are those parameters? – user2771150 Oct 22 '13 at 11:10
  • Ok, i think you need to create default constructor and call that that method/constructor inside that constructor. Or i need to see some more code. – Ashu Phaugat Oct 22 '13 at 12:37