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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:51:26+00:00 2026-06-08T00:51:26+00:00

I have an arraylist in my metod receiveArrayLists which i want to add to

  • 0

I have an arraylist in my metod receiveArrayLists which i want to add to a JList. How can i do this?

import java.awt.Dimension;
import java.awt.Scrollbar;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class GUI implements Runnable {

private Server server;
private JFrame frame = new JFrame();
private JTextField jtf = new JTextField();
private JList jl = new JList();
private JTextArea jl1 = new JTextArea();
private JScrollPane pane = new JScrollPane(jl);
private Socket socket;
private DataInputStream dis;
private ObjectInputStream ois = null;
private DataOutputStream dos;

public GUI() {

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


    frame.getContentPane().setLayout(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setBounds(50, 300, 420, 400);
    frame.setResizable(false);
    frame.setVisible(true);
    pane.add(jl);
    pane.add(jl1);
    jl1.setEditable(false);
    jtf.setBounds(50, 40, 150, 40);
    jl.setBounds(50, 90, 150, 200);
    jl1.setBounds(210, 90, 150, 200);


    jtf.addKeyListener(new KeyListener() {

        public void keyTyped(KeyEvent e) {
        }

        public void keyPressed(KeyEvent e) {
        }

        public void keyReleased(KeyEvent e) {
            if (dos != null) {
                if(jtf.getText().length() >0){
                try {
                    dos.writeUTF(jtf.getText());
                } catch (IOException ex) {
                    Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
                }
                }else{
                    jl1.setText("");
                }
            }

        }
    });
    frame.add(jtf);
    frame.add(jl);
    frame.add(jl1);

    frame.add(pane);
}

public void run() {
    String fromServer;
    try {
        while ((fromServer = dis.readUTF()) != null) {
            if (fromServer.equals("read")) {
                receiveArrayList();
            }
        }
    } catch (Exception e) {

    }
}

Here is my metod, as you can see, i try to use append which obviously wont work to add an arraylist to a JList

public void receiveArrayList() {

    try {
        jl1.setText("");
        ois = new ObjectInputStream(socket.getInputStream());
        @SuppressWarnings("unchecked")
        ArrayList<String> a = (ArrayList<String>) (ois.readObject());
        for (int i = 0; i < a.size(); i++) {
            jl.append(a.get(i) + " \n");
        }
        dis = new DataInputStream(socket.getInputStream());
    } catch (ClassNotFoundException ex) {
        System.out.println(ex);
    } catch (IOException ex) {
        System.out.println(ex);
    }
}

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

}
}
  • 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-08T00:51:27+00:00Added an answer on June 8, 2026 at 12:51 am

    The simplest is to create a DefaultListModel object, iterate through the ArrayList in a for or foreach loop and add the items to the model via its addElement(...) method. Then set the JList’s model to your model.

    More involved but satisfying is to create your own ListModel by extending AbstractListModel using your ArrayList as the model’s nucleus.

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

Sidebar

Related Questions

So I have this method which will take an ArrayList of Integers, I compare
I have arraylist myorderdata . I want to retrieve this arraylist to one String
i have an arraylist and i want to add and remove many items at
I have an ArrayList in Java, which contains Objects, these Objects are made of
I have an ArrayList of objects. ie ArrayList<ObjectName> . I want to pass this
I have an ArrayList , which I want to divide into smaller List objects
I currently have an arraylist in which I add blossom objects ( which fall
Can't figure this one out I have an ArrayList of classes: // Holds an
I have an ArrayList in Java which is full of int's. I am accessing
I have this ArrayList in Java - List<Map<String, Object>> list = new ArrayList<Map<String, 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.