0

I know it has been posted before, but I was not able to achieve any results so I am asking for help.

I am trying to call my JFrame class to my main class that will be setting up the Comm port. The frame is created using the design in the Netbeans software. My question is why is my main Java file not able to open the frame that is created in another class?

Below are the shortened codes:

The main class is SerialTest

package javaapplication1;

import javaapplication1.RCDA_JFrame;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import gnu.io.CommPortIdentifier; 
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent; 
import gnu.io.SerialPortEventListener; 
import java.util.Enumeration;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;

public class SerialTest extends JFrame implements SerialPortEventListener {

    public static void main(String[] args) throws Exception {

                JFrame RCDA_JFrame = new JFrame();
                RCDA_JFrame.setVisible(true);
    }
}

And my GUI class is RCDA_JFrame

package javaapplication1;

import java.io.IOException;
//import java.io.OutputStream;
import javaapplication1.SerialTest;

public class RCDA_JFrame extends javax.swing.JFrame {
    //private OutputStream SerialOut;
    public RCDA_JFrame() {
        initComponents();
    }

    public static void main(String args[]) {

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new RCDA_JFrame().setVisible(true);
            }
        });
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
mclaren12c
  • 25
  • 1
  • 1
  • 7
  • 1
    See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Nov 16 '15 at 03:16
  • 1
    *"I know it has been posted before,.."* Link to it/them. *"..but I was not able to achieve any results so I am asking for help."* What is it that makes you think we can help you better than those questions could, especially when we don't know why they did **not** work for you? – Andrew Thompson Nov 16 '15 at 03:18
  • I'm only using one JFrame. http://stackoverflow.com/questions/17018857/how-to-call-jframe-from-another-java-class?answertab=active#tab-top – mclaren12c Nov 16 '15 at 03:20
  • 2
    *"I'm only using one JFrame."* And yet, both the classes extend `JFrame` .. Where are those links? – Andrew Thompson Nov 16 '15 at 03:21
  • Where is the `initComponents()` method? – Jonah Nov 16 '15 at 03:23
  • I imported the JFrame file and I followed the same steps for the main function by setting up the JFrame and then setting it visible. I am only getting a JFrame with nothing inside. – mclaren12c Nov 16 '15 at 03:24
  • Andrew, I tried not including the JFrame but it didn't change anything and I just pasted it without deleting that. I edited my comment. – mclaren12c Nov 16 '15 at 03:26
  • Jonah, I erased the 600 lines of code that was unnecessary to put on here that contains my UI. – mclaren12c Nov 16 '15 at 03:27

2 Answers2

1

Firstly, only one class (the GUI class) should extend JFrame. However, this is not causing the problem. Secondly, neither class is ever instantiated even though both extend JFrame (which generally implies that one should be instantiated). Thirdly, you should only have one main method throughout an entire application. Fourthly, not entirely sure what the main() method within the GUI class is doing -- why does it create an RCDA_JFrame (I though the Serial class was supposed to do that, based on the title question), and why does it do this within a separate thread? Finally, make sure that you are setting the JFrame's size (Though you may already be doing this within the initComponents method.)

Here's some basic code that will allow you to create and access a custom JFrame from another class:

Start class (This is the main class)

public class Start{
    private static CustomJFrame myFrame;
    public static void main(String[] args){
        myFrame = new CustomJFrame();
        //you can edit myFrame's properties here.
    }
}

CustomJFrame class (the JFrame class)

import javax.swing.JFrame;


public class CustomJFrame extends JFrame{
    public CustomJFrame(){
        //set its size in px.
        setSize(200,200);
        //center it on screen.
        setLocationRelativeTo(null);
        //give it a title.
        setTitle("My JFrame");
        //set the close operation.
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //make it visible.
        setVisible(true);
    }
}

Tested and works just fine.

Alexander Guyer
  • 2,063
  • 1
  • 14
  • 20
  • Nerdizzle, it was able to open when I comment out the setLocaton(null); ! – mclaren12c Nov 16 '15 at 04:39
  • it's `setLocationRelativeTo(null)`, not `setLocation(null)`. `setLocationRelativeTo(null)` merely centers the window on the screen. `setLocation(null)` would (at least I would imagine) return a `NullPointerException`. – Alexander Guyer Nov 17 '15 at 03:37
0

I recommend doing something like this. No need for two main methods.

import javax.swing.*;

public class SerialTest{

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(RCDA_JFrame::new);
    }
}

And this

public class RCDA_JFrame extends JFrame {

    public RCDA_JFrame() {
        initComponents();
    }

    public void initComponents(){
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        pack();
        setVisible(true);
    }
}
Jonah
  • 1,013
  • 15
  • 25
  • Jonah, just letting you know, I am unable to edit anything in the initComponents() because Netbeans automatically creates the code for my corresponding GUI. Thanks though! – mclaren12c Nov 16 '15 at 04:42
  • You don't have to. I just put that code there to post a working example. – Jonah Nov 16 '15 at 05:54