mclaren12c
mclaren12c

Reputation: 25

How to call JFrame from another Java class [Netbeans]

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);
            }
        });
    }
}

Upvotes: 0

Views: 13937

Answers (2)

Alexander Guyer
Alexander Guyer

Reputation: 2193

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.

Upvotes: 1

Jonah
Jonah

Reputation: 1003

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);
    }
}

Upvotes: 0

Related Questions