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

  • SEARCH
  • Home
  • 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 8431629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:54:24+00:00 2026-06-10T05:54:24+00:00

I have a method where i am trying to receive an arraylist from another

  • 0

I have a method where i am trying to receive an arraylist from another class and store it in a new arraylist. The method is supposed to run when i click on the button, but i get nothing. Instead, my GUI froze, the excecution hangs.

public class GUI implements ActionListener {

private JFrame frame = new JFrame("Dryck & Ingrediens"); // GUI
private JTextField jtf = new JTextField();// GUI
private JTextArea jl1 = new JTextArea();// GUI
private JList jl = new JList();// GUI
private JButton b = new JButton("Sök");
private JScrollPane js = new JScrollPane(jl);// GUI
private JLabel lab = new JLabel("Ange dryck");//GUI
private JLabel lab1 = new JLabel("Walid Shams");
private JLabel lab2 = new JLabel("Kushtrim Brahimi");
private HashMap<String, ArrayList<String>> drinkar = null;//Controller
private ServerHandler serverH = new ServerHandler();

public GUI() {


    frame.getContentPane().setLayout(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setBounds(50, 300, 420, 400);
    frame.setResizable(false);
    frame.setVisible(true);
    js.add(jl);
    js.add(jl1);
    jl1.setEditable(false);
    lab.setBounds(90, 20, 130, 20);
    lab1.setBounds(300, 310, 130, 20);
    lab2.setBounds(300, 330, 130,20);
    jtf.setBounds(50, 40, 150, 40);
    b.setBounds(230, 40, 100, 40);
    jl.setBounds(50, 90, 150, 200);
    jl1.setBounds(210, 90, 150, 200);
    Container con = frame.getContentPane();
    con.setBackground(Color.cyan);



    jtf.addKeyListener(new KeyListener() {

        public void keyTyped(KeyEvent e) {
        }

        public void keyPressed(KeyEvent e) {
        }

        public void keyReleased(KeyEvent e) {

                if (jtf.getText().length() > 0) {
                    serverH.writeMsg(jtf.getText());
                }
                else {
                    String[] empty = new String[]{""};
                    jl.setListData(empty);
                }
        }
    });


    jl.addListSelectionListener(new ListSelectionListener() {

        public void valueChanged(ListSelectionEvent e) {
             if (jl.getSelectedIndex() != -1) {
                 String item = (String) jl.getSelectedValue();
                 jl1.setText("");
                 for (String ingrediens : drinkar.get(item)) {
                     jl1.append(ingrediens + "\n");
                 }
             }else{
                 jl1.setText("");
             }
        }
    });
    frame.add(jtf);
    frame.add(jl);
    frame.add(jl1);
    frame.add(lab);
    frame.add(lab1);
    frame.add(lab2);
    frame.add(js);
    frame.add(b);
}


public void actionPerformed(ActionEvent e) {
    if(e.getSource().equals(b)){
        getArrayList();
    }

}

// tar emot arrayen, lagrar i ny array och visar i JList
public void getArrayList() {

    DefaultListModel model = new DefaultListModel();
    ArrayList<String> drinks = new ArrayList<String>();

    drinks = serverH.receiveArrayList();
    System.out.println("the contents of the list" + drinks);

    for(int i = 0; i < drinks.size(); i++){
        model.addElement(drinks.get(i));
    }
    jl.setModel(model);

}


public static void main(String[] args) {
    GUI g = new GUI();

}

}

This is my serverHandler class which is from where im trying to receive the Arraylist. But it seems that its not going through to my GUI.

public class ServerHandler  {

private Socket socket;// ServerHandler
private DataInputStream dis;// ServerHandler
private ObjectInputStream ois = null;// ServerHandler
private DataOutputStream dos;// ServerHandler
private HashMap<String, ArrayList<String>> drinkar;

public ServerHandler(){

    socket = new Socket();
    InetSocketAddress ipPort = new InetSocketAddress("127.0.0.1", 4444);
    try {
        socket.connect(ipPort);
        dis = new DataInputStream(socket.getInputStream());
        dos = new DataOutputStream(socket.getOutputStream());
    } catch (Exception e) {
    }
//        new Thread(this).start();
}


public ArrayList<String> receiveArrayList() {
    String fromServer;
    try {
        while ((fromServer = dis.readUTF()) != null) {
            if (fromServer.equals("read")) {
                getArrayList();
            }

        }
    } catch (Exception e) {
    }
    return getArrayList();

}

public void writeMsg(String jtf){

    if (dos != null) {
        if (jtf.length() > 0) {
            try {
                dos = new DataOutputStream(socket.getOutputStream());
                dos.writeUTF(jtf);
            } catch (IOException ex) {
                System.out.print(ex);
            }
        }
    }
}

public ArrayList<String> getArrayList(){

ArrayList<String> arr = new ArrayList<String>();

    try {
        ois = new ObjectInputStream(socket.getInputStream());
        drinkar = (HashMap<String, ArrayList<String>>) (ois.readObject());

        Iterator it = drinkar.keySet().iterator();
        while(it.hasNext()){
            String temp = (String) it.next();
            arr.add(temp);
        }
        System.out.println(arr);


    } catch (ClassNotFoundException ex) {
        System.out.println(ex);
    } catch (IOException ex) {
        System.out.println(ex);
    }

    return arr;

}

public ArrayList<String> drinks(){

    ArrayList<String>arr = new ArrayList<String>();
    return null;

}

public static void main(String[] args) {
    ServerHandler sv = new ServerHandler();
}

}

  • 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-10T05:54:25+00:00Added an answer on June 10, 2026 at 5:54 am

    You never add the action listener to b, your JButton. As a result, if(e.getSource().equals(b)) is always false because the b button never initiates the action. Since this if never evaluates true, the method never gets called.

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

Sidebar

Related Questions

I have a method from decompiled code that i am trying to run and
I have been trying to pass a string value from one method into another.
I am trying to call the browse() method of the FileReference class from JavaScript
I am trying to have my WCF client receive info from a callback. I
I am trying to have a method that takes in a username and will
I have a method which I am trying to call dynamically. That method has
I have a huge problem with this method im trying to make. I have
I'm trying to not repeat code, so I have a method that does a
Trying to use GridBagLayout. I have the method called buildLabel. This creates three label.
We have the following method where we are trying to access an array object

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.