-2

I have a problem with my exercise for university. I need to create a JList and put there a few elements (just a simple text) without using any collections nor arrays. It's quite silly because a JList is usually initialized with DefaultListModel (it's a collection too), but in this case I'm not allowed to use it. I tried to put a JLabel object in a parametrized constructor of JList but it doesn't work. Do anyone of you have an idea how to deal with it? Much thanks for help in advance.

My code so far:

JFrame jFrame = new JFrame("title");
JList<String> jList = new JList<>();
jList.add(new JLabel("label1"));
jFrame.add(jList);
jFrame.setSize(500, 500);
jFrame.setVisible(true);
zlakad
  • 1,314
  • 1
  • 9
  • 16
zerkenn
  • 37
  • 4
  • 2
    Can you show us some code? – Prashant Jun 24 '18 at 18:16
  • Yes, here it is: `JFrame jFrame = new JFrame("title"); JList jList = new JList<>(); jList.add(new JLabel("label1")); jFrame.add(jList); jFrame.setSize(500,500); jFrame.setVisible(true);` – zerkenn Jun 24 '18 at 18:26
  • Please edit your question and add a decent attempt at a solution to it as code-formatted text.We need to see what you've tried and you need to tell us how it's not working. – Hovercraft Full Of Eels Jun 24 '18 at 18:34
  • You don't add a JLabel to a JList. You add Strings to it. – Hovercraft Full Of Eels Jun 24 '18 at 18:34
  • I cannot add String, I can add only a Component, so that is why a JLabel seemed reasonable to me, but it doesn't work. – zerkenn Jun 24 '18 at 18:47
  • 1
    `I cannot add String` of course you can. Read the swing tutorial on [How to Use LIsts](https://docs.oracle.com/javase/tutorial/uiswing/components/list.html) for working examples. `I can add only a Component,` - that is not how a JList works. You add data (usually a String) to the `ListModel` (not the JList) and the `JList` will render the data appropriately. – camickr Jun 24 '18 at 19:17

1 Answers1

0

When I correct understand your academic, you simply need to create a list model, which simply returns the constant value or can calculate row value using a formula.

Here is my suggestion for you.

import javax.swing.AbstractListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

public class SimpleListExample implements Runnable {


    public static void main(String[] args) {
        SwingUtilities.invokeLater(new SimpleListExample());
    }

    @Override
    public void run() {
        JList<String> list = new JList<>(new SimpleListModel());
        JFrame frame = new JFrame("List example");
        frame.add(new JScrollPane(list));
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setSize(350, 200);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    @SuppressWarnings("serial")
    private static class SimpleListModel extends AbstractListModel<String> {

        @Override
        public int getSize() {
            return 20;
        }

        @Override
        public String getElementAt(int index) {
            // Generate constant value with the row index ;)
            return "It's row number: " + (index + 1);
        }
    }
}
Sergiy Medvynskyy
  • 11,160
  • 1
  • 32
  • 48
  • Thank you for answer. The point is that I cannot use any collections and `AbstractListModel` which you have used implements `ListModel` and as list is a kind of collection I'm not allowed to use it. I tried to to add some elements using the `add()` method invoked on my `JList` but it doesn't work - the frame is empty after running my code. Do you know why it won't work? – zerkenn Jun 24 '18 at 18:44
  • @zerkenn because the method `add` has no sence for `JList`. You only can interact with `JList` by using `ListModel`. So I think you've misunderstood your academic. – Sergiy Medvynskyy Jun 24 '18 at 19:12
  • @zerkenn You can’t interact with a JList without interacting with its model - it’s just impossible - you could consider creating a custom ListModel which has a static number of elements – MadProgrammer Jun 24 '18 at 20:24