Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6612387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:05:34+00:00 2026-05-25T20:05:34+00:00

I’m new to java coming from C# so I’m not familiar with java best

  • 0

I’m new to java coming from C# so I’m not familiar with java best practices.

I have a main class that opens a JFrame to get several input strings from a user. When the user clicks submit the GUI should close and the main class continue processing using the input.

This is the main class:

public class Main {
    FInput fInput;

    public void main(String[] args) {
        if(args.length==0)
        {
            fInput = new FInput();
            fInput.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            fInput.pack();
            fInput.setVisible(true);
        }
    else
        startProcess(args);
    }

    public void startProcess(String[] args) {
// Do stuff
}

The main class will use this frame to get input from the user:

public class FInput extends JFrame{
    private JTextField txtSourceDirectory;
    private JTextField txtTargetDirectory;
    private JTextField txtDefectNumber;
    private JTextField txtSliceTokens;
    private JButton btnStart;

    public FInput() {
        // Initialize text fields and button
        JButton.addActionListener(something);
    }
}

In all the examples I could find, the listener would be a FMain itself. However in this case I want Main to listen and use the input in method startProcess.

Would having Main implement ActionListener, and passing it to FMain constructor is the way to go?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:05:34+00:00Added an answer on May 25, 2026 at 8:05 pm

    Yes, that is the right idea. You must do two things in order to be able to do that, though:

    1. Put this at the beginning of the FInput class:

      Main m = new Main(this);
      
    2. Then, put these lines somewhere in the Main class…

      FInput gui;
      
      public Main(FInput in) { gui = in; }
      

    Now you can refer to any component in the FInput class from the Main class by doing something like this.

    gui.someComponent ...
    

    To set up listeners just write someComponent.addItemListener(m); or something of the sort.

    Hope this helps!


    @Yoav In response to your latest comment…

    You don’t have to separate the listening class from the GUI class; you can combine the two into one class…

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class Main extends JFrame implements ActionListener {
    
        private JTextField txtSourceDirectory;
        private JTextField txtTargetDirectory;
        private JTextField txtDefectNumber;
        private JTextField txtSliceTokens;
        private JButton btnStart;
    
        public Main() {
            txtSourceDirectory = new JTextField(40); //change this to the amount of characters you need
            txtTargetDirectory = new JTextField(40);
            txtDefectNumber = new JTextField(40);
            txtSliceTokens = new JTextField(40);
            btnStart = new JButton("Start");
            add(txtSourceDirectory);
            add(txtTargetDirectory);
            add(txtDefectNumber);
            add(txtSliceTokens);
            add(btnStart);
            btnStart.addActionListener(this);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            pack();
            setVisible(true);
        }
    
        public void actionPerformed(ActionEvent event) {
            //do stuff
        }
    
        static void startProcess(String[] ARGS) {
            //do stuff
        }
    
        public static void main(String[] args) {
            if (args.length == 0) {
                Main frame = new Main();
            } else {
                startProcess(args);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a text area in my form which accepts all possible characters from
I have thousands of HTML files to process using Groovy/Java and I need to
I need a function that will clean a strings' special characters. I do NOT
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.