-1

https://i.stack.imgur.com/8rQW1.png

enter image description here

JScrollPane scrollPane_4 = new JScrollPane();
    scrollPane_4.setViewportView(list_4);

final JList list_4 = new JList(modelItems);
    list_4.setBounds(428, 39, 294, 187);

    JButton btnNewButton_4 = new JButton("Refresh");
    btnNewButton_4.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            modelItems.clear();
            arrayOfItems.clear();
            for(RSItem d : Inventory.getAll())
            {
                arrayOfItems.add(d);
                modelItems.addElement("Item: "+d.getID()+" at spot: "+(d.getIndex()+1));
            }
        }
    });

I am not sure what is causing this to happen. I have many other JLists with almost the exact same code and this does not occur.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Assume
  • 18
  • 4
  • 1
    `list_4.setBounds(428, 39, 294, 187);` doesn't look correct. Remove this line entirely - the layout should take responsibility for correct placement. – Howard Jun 04 '13 at 06:24
  • 1
    I'd start with saying this `list_4.setBounds(428, 39, 294, 187);` is a bad idea...Secondly, it either appears to be an issue with mixing light and heavy weight components or bad custom painting or other issue not highlighted by the snippet... – MadProgrammer Jun 04 '13 at 06:24
  • I am using absolute bounds. That is why I am setting the bounds. The layout manager is set to null. @mKorbel I would not be able to post an SSCCE as I am using a third party API. The code would not execute as it would require you to run the script inside of a separate (paid) application. – Assume Jun 04 '13 at 07:02
  • The anomaly reminds me of [this](http://stackoverflow.com/q/7213178/230513). – trashgod Jun 04 '13 at 08:24
  • @user2450444 _The layout manager is set to null._ No you are not. I don't see that anywhere in your code. Btw, you list is actually added to the viewport of the scrollpane (so that would mean that you are calling `scrollPane_4.getViewPort().setLayout(null)`... I doubt that. Anyway, calling `setLayout(null)` is pretty much the worst idea you could have. – Guillaume Polet Jun 04 '13 at 08:56
  • 2
    @trashgod This mainly looks like he is actually adding the `JList` instead of the `JScrollPane` to the component hierarchy, but without an [SSCCE](http://sscce.org) we can only make wild guesses – Guillaume Polet Jun 04 '13 at 08:57
  • 2
    @user2450444 _I would not be able to post an SSCCE as I am using a third party API_ You obviously have not read the article as it specifically tackles third-party library issues. – Guillaume Polet Jun 04 '13 at 09:18

1 Answers1

2

I'm pretty sure that this is not your code. The following two lines

  scrollPane_4.setViewportView(list_4);
  final JList list_4 = new JList(modelItems);

are in reverse order. You first set the viewport view before you even declare your list. Make sure that you do not set the view to null in this line and reverse order of those two.

Howard
  • 38,639
  • 9
  • 64
  • 83
  • I C&P'd that after pasting the Action listener. That is why it is out of order. Why do you Assume this is not my code? I assure you it all is. – Assume Jun 04 '13 at 06:58
  • @user2450444 I still don't understand why adding the `ActionListener` would mix up those two lines. It is obvious that your code can't compile, hence the assumption that it is not the "original" code you run. Anyway, your calls to `setBounds` are most likely one of your issues (you should never call setBounds, leave that to LayoutManager's). It also looks like you are adding your list instead of your scrollpane to the component hierrchy. For better help sooner, post an [SSCCE](http://sscce.org). – Guillaume Polet Jun 04 '13 at 08:54