Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Trying to call a form from another form

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

I am an experienced programmer who is teaching Java to some Jr High students.  I have some experience in Java but no experience using Swing forms.  I am trying to call a second form from my main form.  I have a button to call the form and I am using the addActionListener method to fire the call.  My problem is my called form does not appear...:-(  Here is a snippet of what I am using to call the dependent form



Here is the relavent code for my CheckMethod.Java file


Register
Finally, Here is the code for the Dependent RegisterFrm.Java



Hopefully this gives you enough code to go on.  I am using Java8 SE JDK.  This particular problem is not well documented, IMO.  YouTube tutorials are way too fast and usually use NetBeans or some other IDE
which confuses the heck out of me.

Thanks in advance for your help!

Tom Nesler
 
Marshal
Posts: 69874
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I shall move you to our GUIs forum. You might do well to consider learning the newer JavaFX framework instead of Swing.

Don't extend display classes. Make the frame a variable of your class rather than extending JFrame. I would not use two frames; if you need a second top‑level container, consider having a dialogue window appear instead, or even consider a desktop pane with internal frames.
Separate the display from the logic of the application. Make sure the app runs correctly and completely at the command line, and that it has a public interface with methods like openAccount(), depositMoney(), drawCheque(), getBalance(), getCheques() etc., each method having the appropriate parameters. Test that with code that calls all those methods. Make sure the methods correctly maintain all your class invariants, including throwing exceptions if you try to draw a cheque for a negative sum, and similar. That is doubtless second nature to you. Then you can put a GUI atop the app. Now you will see, it doesn't make that much difference whether the GUI is written in Swing or FX. In fact, if you start with FX, you can intersperse Swing classes with your FX classes, but that may be too complicated for your present requirements.
Don't use action commands. Use one listener per action, so each control requiring an action listener will have its own listener object. Five years ago, I would have recommended an anonymous class as below (lines 11‑19), but since then ActionListener has become a functional interface, so you can use a λ expression (lines 22‑23) or even a method reference instead.
You will now have something like this:-Note that you have to give a JFrame a size (or bounds, or pack() method), or it will appear really tiny. You must also call setVisible(true) to make it appear at all, and that setVisible() call should best be the last statement.
Note that I have ruthlessly abbreviated that code, leaving out any verification of inputs in amountField and payeeField. Or even those fields. Those text fields can be in different internal frames, or even different ordinary frames, though I don't like multiple top‑level containers myself.

The following only applies to Swing, not FX:-
You are supposed to make sure to start the event despatch thread (=EDT) to run your Swing app. Do it like this:-That version uses a λ. FX application have to be started with (I think) Application.start(), which starts the special FX thread corresponding to the EDT.

I hope that is of use to you.
 
Sheriff
Posts: 7108
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

FX application have to be started with (I think) Application.start(), which starts the special FX thread corresponding to the EDT.


It's Application.launch(), but yes, that's correct.
 
Tom Nesler
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell for moving my thread to the correct group and for the suggestions.  In my case, I don't have time to learn the FX framework and this program is for a class of 12 to 14 year olds, so I am skipping tons of code that would be used for a typical application such as error checking and exception handling.  When/if I have a second class, I will cover database management and error handling.  In my example program, we will be using a two dimensional array to hold entries...:-)

Since I am using JDK8 as my compiler, will the FX framework compile?

If it is alright by you, I will list the three files below so you can see more clearly what I am doing.  I don't want to optimize my code or rebuild my program.  I just want to understand why the dependent frame is not appearing when I call it.

Thanks for your patience!

Tom

************************************
CheckBook.java
-------------------
import java.awt.*;
import javax.swing.*;

public class Checkbook extends JFrame {
   JButton checkRegister;
   JButton writeCheck;
   JButton makeDeposit;

   CheckMethod chkMthd = new CheckMethod(this); //Create CheckMethod object

public Checkbook() {
       super("Checkbook");
       setLookAndFeel();
       setSize(348,500); // Size of frame
       setLocation(400,100); // 400 pixels horizontal from left , 100 pixels down from top
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       GridLayout grid = new GridLayout(3, 1, 0, 30);  //3 rows, 1 column, 0 horizontal gap, 10 vertical gap
       setLayout(grid);
       checkRegister = new JButton("View Checks");
       writeCheck = new JButton("Write a Check");
       makeDeposit = new JButton("Enter a Deposit");

       checkRegister.addActionListener(chkMthd);
       writeCheck.addActionListener(chkMthd);
       makeDeposit.addActionListener(chkMthd);

      add(checkRegister);
       add(writeCheck);
       add(makeDeposit);
       setVisible(true);
   }
public Insets getInsets() {
       Insets squeeze = new Insets(50, 50, 50, 50);
       return squeeze;
   }
   private void setLookAndFeel() {
       try {
           UIManager.setLookAndFeel(
               "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"
           );
       } catch (Exception exc) {
           // ignore error
       }
   }
   public static void main(String[] arguments) {
       Checkbook frame = new Checkbook();
   }
}

****************************
CheckMehod.java
------------------
import javax.swing.*;
import java.awt.event.*;
import javax.swing.JOptionPane;

public class CheckMethod implements ActionListener //for Buttons
{
Checkbook gui;  //Linking to CheckBook file

   public CheckMethod(Checkbook in)   // Turn on Gui
   {       gui = in;
   }

public void actionPerformed(ActionEvent event) //Buttons Listener
{
       String command = event.getActionCommand();
       if (command == "checkRegister") {
           RegisterFrm RF = new RegisterFrm();
       }
       if (command == "writeCheck") {
           writeChk();
       }
       if (command == "makeDeposit") {
           makeDep();
       }
   }


void writeChk()
{
JOptionPane.showMessageDialog(null,
"Write Check Goes Here", "Write Check",
JOptionPane.INFORMATION_MESSAGE);
}

void makeDep()
{
JOptionPane.showMessageDialog(null,
"Deposit Entry Goes Here", "Deposit Check",
JOptionPane.INFORMATION_MESSAGE);
}
}

******************************
RegisterFrm.java
-------------------
import java.awt.*;
import javax.swing.*;

public class RegisterFrm extends JFrame{
public RegisterFrm()
{
super("RegisterFrm");
       setLookAndFeel();
       setSize(600,500); // Size of frame
       setLocation(500,200); // 500 pixels horizontal from left , 200 pixels down from top
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       setVisible(true);
}


private void setLookAndFeel() {
       try {
           UIManager.setLookAndFeel(
               "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"
           );
       } catch (Exception exc) {
           // ignore error
       }
   }
public static void main(String[] arguments) {
       RegisterFrm frame = new RegisterFrm();
   }
}
*********************************
End of program files.  BTW I tried to use the attachment feature but it would not take txt or Java attachments.  What should I use instead?

 
Campbell Ritchie
Marshal
Posts: 69874
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tom Nesler wrote:Thanks Campbell for moving my thread to the correct group and for the suggestions. . . .

Since I am using JDK8 as my compiler, will the FX framework compile?

Yes

If it is alright by you, I will list the three files below so you can see more clearly what I am doing.  I don't want to optimize my code or rebuild my program.  I just want to understand why the dependent frame is not appearing when I call it.

Thanks for your patience!

Tom . . .
BTW I tried to use the attachment feature but it would not take txt or Java attachments.  What should I use instead?

Copy the code into code tags. It will then look like this:-
CheckBook.java
-------------------
RegisterFrm.javaIf you have a young class, stick to a single framework which you are familiar with. There is nothing wrong with saying there is a newer framework but you don't know it as well as the older framework yet.
Never, never write an empty catch. You need to know if an exception occurs.
Be consistent with your indentation. You will appear as much of a fusspot as I do when you tell the children that, but we see so many errors obscured by inconsistent indentation. Your CheckMethod class and RegisterFrm constructor use a sort of Allman indentation and the rest of the code K&R. Choose one convention and stick to it rigorously. Tell the kids that there are several indentation conventions but you think it is best they use convention XYZ. Look at this old post, where I showed how to maintain indentation by writing backwards.
I am still worried that you are trying to make display classes do business logic. You should have an application so you can write:-Then you add the GUI to that once you have that sort of app working. I personally think an array of arrays (which is probably not a 2D array) would be a poor way to maintain the records. There are easier data structures to use than arrays. Consider whether to scale down the project rather than using plain simple arrays.
The == operator is not suitable for use on reference types. I still dislike using action commands. I think that different action listener objects for the different buttons are much better.
Make sure the names of things reflect what they are. Names which look like verbs naturally go with methods and not with classes, and names which look like nouns do go well with classes. &#c201c;CheckMethod” is a poor name for a class, and writeCheck a poor name for a button.

Sorry to appear so negative.
 
Tom Nesler
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sorry to appear so negative.



No Problem...:-)  


I agree with the empty catch.  I copied the code from an Intro example and never bothered to fix that.

I am notorius for bad indentations.  I will look into your post.

Tom

 
Campbell Ritchie
Marshal
Posts: 69874
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tom Nesler wrote:. . . I copied the code from an Intro example . . .

Please send us more details of that example . . . so we know to avoid it 

I know I seem a fusspot for going on about indentation, but we see all sorts of weird and wonderful errors which would have been obvious to the poster with correct indentation. Even classes shown as nested, which they thought were top‑level classes.
 
    Bookmark Topic Watch Topic
  • New Topic