0

I am having trouble using JLists in Java. I have watched video tutorials on how to use them, but they all use them with some sort of layout. I would like to have it so the "setPreferedLayout" is null, and I can use the setBounds method to decide where my lists and buttons go on the window. When I do that, and I do somthing like frame.add(list) or panel.(list) to add it to my panel, it doesn't show up on the window, but my button does.

I have something like this:

    //DECLARATION
    JFrame f = new JFrame("main Window");
    JPanel p = new JPanel();

    int WIDTH = 800;
    int HEIGHT = 650;

    public static JList mainList;
    String[] mainArray = {"one","two","three"};

    //INIT
    public mainClass() {
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(p);
        f.setVisible(true);
        p.setLayout(null);
        p.setPreferredSize( new Dimension(WIDTH,HEIGHT) );
        f.pack();
        p.setVisible(true);

        p.setFocusable(true);

        System.setProperty("sun.java2d.opengl","true");
        Thread thr1 = new Thread (r1);
        thr1.start();

        mainList = new JList(mainArray);
        mainList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        mainList.setSelectedIndex(0);
        mainList.setVisibleRowCount(3);
        JScrollPane listScrollPane = new JScrollPane(mainList);
    }

again, i'm trying to create a JList that I can have in whatever position I would like. thats basically what i'm trying to get. Whenever I try to di it the way it works will Jbuttons (setting its bounds and adding it to the panel) It doesn't show up when I runt he program...

Lukas Rotter
  • 4,158
  • 1
  • 15
  • 35
Andrew900460
  • 621
  • 1
  • 6
  • 16
  • This code is not compilable as is. Also, you haven't added the JScrollPane (which your JList is added to) to anything. Voting to close as it does not demostrate the issue – ControlAltDel Jul 28 '15 at 19:21

1 Answers1

1
public mainClass() {
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        mainList = new JList(mainArray);
        mainList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        mainList.setSelectedIndex(0);
        mainList.setVisibleRowCount(3);
        JScrollPane listScrollPane = new JScrollPane(mainList);
        p.add(listScrollPane);
        f.add(p);
        p.setPreferredSize( new Dimension(WIDTH,HEIGHT) );        
        f.pack();        
        f.setVisible(true);
        p.setFocusable(true);

        System.setProperty("sun.java2d.opengl","true");
        Thread thr1 = new Thread (r1);
        thr1.start();


    }

this should work you never added your list to panel, also you set visible than pack() thats not gonna work. The last thing you should do is set Visible.

Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
sjohnson
  • 69
  • 2
  • 13
  • Thank you, I have a side question as well... My java teacher taught us to setup the variables for the panel and frame, and then set variables for the buttons text fields etc. So is it just that JLists have to be initalized before the frame and panel are initalized? Or is it more correct to initalize your buttons textfields lists first, then the panel and frame? I would like to mess with him by doing that ;P – Andrew900460 Jul 28 '15 at 19:32
  • You can initialize them in any order. Generally it should be done from greatest to smallest ie Frame -> Panel -> list . however setting the variables does not necessarily mean setting them visible or packing them. – sjohnson Jul 28 '15 at 19:35
  • I got it working, but now I added `p.setLayout(null);` under the setFocusable method and I added `mainList.setBounds(10,10,300,300);` under setVisibleRowCount, but the still wants to stick to the center top of the window. – Andrew900460 Jul 28 '15 at 19:49
  • where do you want the list? – sjohnson Jul 28 '15 at 20:26
  • Wherever I set it to with the setBounds method (in the window). Currently it sticks to the top center of the window, probably because a certain layout is forcing it there. – Andrew900460 Jul 28 '15 at 20:36
  • well you haven't defined your layout actually. – sjohnson Jul 28 '15 at 20:42
  • I added in `p.setLayout(null);` – Andrew900460 Jul 28 '15 at 20:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/84511/discussion-between-andrew900460-and-sjohnson). – Andrew900460 Jul 28 '15 at 20:57