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 8618317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:04:49+00:00 2026-06-12T06:04:49+00:00

I don’t know how to implement an action listener from separate ActionListener class. I

  • 0

I don’t know how to implement an action listener from separate ActionListener class. I have divided application in several classes. Main class calls the Frame class to create GUI with all the buttons. Now when the button is pressed i need to send some data to ActionListener method for it to do the work. If I add ActionListener in Frame class than I cant send needed data, if I add ActionListener in main() method, after calling Frame class it feels clumsy.
Here is the code:
public class Main {

public static Client klijent;

public static void main(String[] args) {

    FrameBuilder frame= new FrameBuilder();
    frame.frameBuild();
    frame.send.addActionListener(new SendBttnListener(ClientBuilder(klijent,frame.txt.getText())));

    try {
        frame.statLabel.setText(InetAddress.getLocalHost().getHostAddress());
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }

    Udp udp;
    // klijent = null;

    while(true){
        klijent=new Client();
        udp=new Udp();

        klijent=udp.packageIN();
        if(klijent!=null){
            frame.statLabel.setText("");
            String str=new String(klijent.getBajt(),0,klijent.bajt.length);
            str=str.trim();
            frame.statLabel.setText(str+"@...@"+klijent.clientAddress.toString()+":"+klijent.clientPort+"-->Duljina:"+klijent.bajt.length);
        }
    }
}

public static Client ClientBuilder(Client klijent2, String str){

    klijent.setBajt(str.getBytes());

    return klijent2;
}

As u can see Client object can change a lot (UDP server) and will be cleared when waiting for next package. Later I think of implementing some list of Clients and adding Action listener in Frame class seems stupid. Did i go wrong when I created separet class for building GUI? Any advice?

Thanks in advance….

  • 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-06-12T06:04:50+00:00Added an answer on June 12, 2026 at 6:04 am

    The method by which you send should be wrapped up in it’s own implementation. This could be a singlton implementation or passed as reference to a wrapping action listener.

    The buttons action listener should only be manged from within the context of the button, it should not be accessible from out side the buttons context (ie I don’t believe you should be able to do this frame.send.addActionListener(...)) as callers shouldn’t care how a send request is achieved, just that it is…

    I’d have a class or interface that you could pass to your frame that provided it with the means to “send” a message.

    public interface Sender {
        public void sendMessage(String message); // Or what ever parameter you want...
    }
    

    I’d pass a reference of the implementation to your frame…

    Sender sender = new ImplementationOfSender();
    frame.setSender(sender); // It could also be passed to the constructor
    

    In the frame, I would attach my action listener to the button and when triggered, I would access the sender and send the message…

    send.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            Sender sender = getSender();
            if (sender != null) {
                sender.sendMessage(...);
            }
        }
    });
    

    Now having said all that…your program is going to blow up in your face real quick…

    This is going to stop your program in it’s tracks, making the UI look like it’s hung.

    while(true){
        klijent=new Client();
        udp=new Udp();
    
        klijent=udp.packageIN();
        if(klijent!=null){
            frame.statLabel.setText("");
            String str=new String(klijent.getBajt(),0,klijent.bajt.length);
            str=str.trim();
            frame.statLabel.setText(str+"@...@"+klijent.clientAddress.toString()+":"+klijent.clientPort+"-->Duljina:"+klijent.bajt.length);
        }
    }
    

    All interactions with the UI MUST be done from within the context of the Event Dispatching Thread (AKA EDT). Any blocking actions, such as your loop, should NEVER be executed from within the context of the EDT

    I would have read of Concurrency in Swing.

    The only reason that you program probably has exploded yet is the fact that when you launch it, you’ve not synced the creation of your UI to the EDT.

    You might like to have a read of Swing Single Threading Rule

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

don't know better title for this, but here's my code. I have class user
Don't know what's wrong here, when I run the application it says Specified method
Don't know where else to ask, but from one day to the other my
Don't know why, but sometimes LocationManager is still working also after closing application. I
Don't know if this has been answered before. Have custom routes to users. If
Don't know why but I can't find a solution to this. I have 3
Don't know the term for the red validation border, does it have one? I
Don't know if it's stupid or reasonable question. I have methods which returns float/int
don't know if the title describes anything about what I'm trying to say but
Don't ask me how but I'm in a situation where I have DCPs published

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.