Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Change JTextArea to JList; Display ArrayList in JList

  1. #1
    Junior Member
    Join Date
    Jun 2017
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Change JTextArea to JList; Display ArrayList in JList

    I can display the ArrayLists in a JTextArea, but how can I change this to a JList. Specifically: If I change it to a JList, how can I display the List of Persons in the JList?

    This is my code:
    /I cut out the (probably) non-relevant parts/

     
    package personFiles;
     
    import java.awt.*;
    import java.util.List;
    import java.awt.event.*;
    import java.util.ArrayList;
     
    import javax.swing.*;
    import javax.swing.event.*;
     
     
    @SuppressWarnings({ "serial", "unchecked" })
    public class Osoblje extends JFrame {
        private JList outputJTA = new JList();
        private JScrollPane outputJTAScrollPane = new JScrollPane(outputJTA);
        private JLabel idJL = new JLabel();
        private JLabel nameJL = new JLabel();
        private JTextField idJTF = new JTextField();
        private JTextField nameJTF = new JTextField();
        private JLabel surnameJL = new JLabel();
        private JLabel ageJL = new JLabel();
        private JTextField surnameJTF = new JTextField();
        private JTextField ageJTF = new JTextField();
        private JLabel genderJL = new JLabel();
        private JTextField genderJTF = new JTextField();
        private JButton newPersonJB = new JButton();
        private JButton pokaziJB = new JButton();
     
        List<Person> personList = new ArrayList<>();
     
        public Osoblje(String title) { 
            // Frame-Initialisierung
            super(title);
            setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            int frameWidth = 310; 
            int frameHeight = 269;
            setSize(frameWidth, frameHeight);
            Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
            int x = (d.width - getSize().width) / 2;
            int y = (d.height - getSize().height) / 2;
            setLocation(x, y);
            setResizable(false);
            Container cp = getContentPane();
            cp.setLayout(null);
            // Anfang Komponenten
     
            outputJTAScrollPane.setBounds(8, 8, 129, 217);
            cp.add(outputJTAScrollPane);
            idJL.setBounds(152, 16, 51, 19);
            idJL.setText("ID:");
            cp.add(idJL);
            nameJL.setBounds(152, 40, 54, 20);
            nameJL.setText("Name:");
            cp.add(nameJL);
            idJTF.setBounds(208, 16, 65, 17);
            cp.add(idJTF);
            nameJTF.setBounds(208, 40, 65, 17);
            cp.add(nameJTF);
            surnameJL.setBounds(152, 64, 59, 19);
            surnameJL.setText("Surname:");
            cp.add(surnameJL);
            ageJL.setBounds(152, 88, 51, 19);
            ageJL.setText("Age:");
            cp.add(ageJL);
            surnameJTF.setBounds(208, 64, 65, 17);
            cp.add(surnameJTF);
            ageJTF.setBounds(208, 88, 65, 17);
            cp.add(ageJTF);
            genderJL.setBounds(152, 112, 59, 19);
            genderJL.setText("Gender:");
            cp.add(genderJL);
            genderJTF.setBounds(208, 112, 65, 17);
            cp.add(genderJTF);
            newPersonJB.setBounds(152, 144, 121, 25);
            newPersonJB.setText("nova osoba");
            newPersonJB.setMargin(new Insets(2, 2, 2, 2));
            newPersonJB.addActionListener(new ActionListener() { 
                public void actionPerformed(ActionEvent evt) { 
                    newPersonJB_ActionPerformed(evt);
                }
            });
            cp.add(newPersonJB);
            pokaziJB.setBounds(152, 184, 121, 25);
            pokaziJB.setText("pokazi");
            pokaziJB.setMargin(new Insets(2, 2, 2, 2));
            pokaziJB.addActionListener(new ActionListener() { 
                public void actionPerformed(ActionEvent evt) { 
                    pokaziJB_ActionPerformed(evt);
                }
            });
            cp.add(pokaziJB);
            // Ende Komponenten
     
            setVisible(true);
        } // end of public Osoblje
     
        // Anfang Methoden
     
        public static void main(String[] args) {
            new Osoblje("Osoblje");
        } // end of main
     
        public void newPersonJB_ActionPerformed(ActionEvent evt) {
        	personList.add(new Person(idJTF.getText(), nameJTF.getText(), surnameJTF.getText(), ageJTF.getText(), genderJTF.getText()));//create new person
        } // end of newPersonJB_ActionPerformed
     
        public void pokaziJB_ActionPerformed(ActionEvent evt) {
            outputJTA.setText(personList + "\n"); //display personList
        } // end of pokaziJB_ActionPerformed
     
        }

    The error message (in eclipse):
    The method setText(String) is undefined for the type JList
    Last edited by AP2000; June 5th, 2017 at 11:10 AM.

  2. #2
    Member
    Join Date
    May 2017
    Location
    Eastern Florida
    Posts
    68
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Change JTextArea to JList; Display ArrayList in JList

    Where is the JList used in the code? I can't see it.
    Did you look at the API doc for the JList class? It has sample code.
    The API doc: Java Platform SE 8

  3. #3
    Junior Member
    Join Date
    Jun 2017
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Change JTextArea to JList; Display ArrayList in JList

    I haven't implemented it yet, as it doesn't work.

  4. #4
    Member
    Join Date
    May 2017
    Location
    Eastern Florida
    Posts
    68
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Change JTextArea to JList; Display ArrayList in JList

    it doesn't work.
    Can you post the code that you are working with so we can see what the problems are?
    Also post the full text of any error messages.

  5. #5
    Junior Member
    Join Date
    Jun 2017
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Change JTextArea to JList; Display ArrayList in JList

    Changed it in the question.

  6. #6
    Member
    Join Date
    May 2017
    Location
    Eastern Florida
    Posts
    68
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Change JTextArea to JList; Display ArrayList in JList

    The method setText(String) is undefined for the type JList
    The message says what the problem is: JList does not have a setText() method.

    Read the API doc for JList to see an example and what methods it has.
    There is a link in the API doc for how to use Lists: How to Use Lists (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

Similar Threads

  1. JList help
    By Masic1990 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 6th, 2014, 12:08 AM
  2. How to display ArrayList in jTextArea?
    By Prog in forum Object Oriented Programming
    Replies: 8
    Last Post: January 12th, 2014, 02:49 AM
  3. Jlist.
    By lucky123098 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: July 21st, 2013, 06:35 AM
  4. JList display problem
    By Poseidon in forum AWT / Java Swing
    Replies: 3
    Last Post: February 19th, 2012, 08:37 AM
  5. [SOLVED] JList display Hebrew right-to-left
    By ilan in forum AWT / Java Swing
    Replies: 3
    Last Post: October 5th, 2011, 06:43 AM