CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2009
    Posts
    1

    Problem when displaying JFrame by button click

    Basically, this is my problem. I have two files: NoteFrame.java and Chords.java.

    I'm attempting to make an instance of the NoteFrame dialog appear when I click on a button in the Chords.java class.

    I have the following code in the event handler code for the button "tunerBtn" in the Chords.java class:

    private void tunerBtnActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:

    NoteFrame newNoteFrame = new NoteFrame();
    newNoteFrame.setVisible(true);
    newNoteFrame.pack();

    }


    This works, as in when I click the button, the NoteFrame dialog appears. However, the NoteFrame dialog is not set up as it is when I run the dialog directly from the NoteFrame class itself.

    Inside the 'main' function in the NoteFrame class I have included a function called 'run' which sets up all the JLabels, Text colours etc.. for the dialog. This doesnt seem to be called when the NoteFrame dialog is created by the button click in the Chords class..

    Is there any way to gain access to 'main' or 'run' through the Chords class in order to ensure the dialog is set up correctly..??

    Any help is appreciated,

    Thanks
    Last edited by bearchild; April 6th, 2009 at 10:18 AM.

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Problem when displaying JFrame by button click

    If the run() method needs to be called to setup the dialog then it probably should be called from the constructor.

    I haven't seen your classes code so I'm only guessing here but my guess is your NoteFrame class extends JDialog. A couple of points here:

    Firstly, you need to sort out your naming - you have a method called run() that doens't run anything, it lays out components and class called NoteFrame that isn't a frame, it's a dialog.

    Secondly, extending JDialog (and/or JFrame for that matter) is generally a bad idea. It's much better to extend JPanel/JComponent, have private constructors and provide static methods to create an instance of the class. That way you can have a range of static factory methods that can create an instance as either a JDialog, a JFrame, a JInternalFrame etc.

  3. #3
    Join Date
    Oct 2008
    Posts
    77

    Re: Problem when displaying JFrame by button click

    I too hope you have extended JDialog, otherwise your form will not appear as the dialog.

    Second solution would be to not call
    Code:
    newNoteFrame.setVisible(true);
    but instead
    Code:
    newNoteFrame.run();
    and then have inside run() method your
    Code:
    setVisible(true)
    call at the end of function

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured