0

I've created a custom JList renderer that lists the titles of books in a sort of virtual library. The renderer works fine inside its own window, but I need it to be a part of the current JFrame I have, and I'm unsure how to implement it.

Here is the code for my renderer:

public class JListCustomRendererBook extends Frame1{

public JListCustomRendererBook(int i)
{

    library[i] = new LibraryHandler(i);
    library[i].booksToText();
    library[1].loadBooks();

    DefaultListModel<Book> listModel = new DefaultListModel<>();

    for (int j = 0; j < library[i].books.length; j++)
    {
        if (library[i].books[j] != null)
        {
            listModel.addElement(library[i].books[j]);
        }
    }

    JList<Book> bookList = new JList<>(listModel);
    add(new JScrollPane(bookList));

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //this.setTitle("Test panel");
    this.setSize(200, 200);
    //this.setLocationRelativeTo(null);
    this.setVisible(true);
}

}

And here is the initialization code for the window (Frame1):

private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 700, 350);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JListCustomRendererBook jasonList = new JListCustomRendererBook(1);
    jasonList.setBounds(450,30,197,205);

    JList list = new JList();
    list.setBounds(450, 30, 197, 205);
    list.setToolTipText("THIS IS SO COL");
    list.setLayoutOrientation(JList.VERTICAL_WRAP);
    list.setModel(new AbstractListModel() {
        String[] values = new String[] {"Hey", "This is cool", "Testing"};
        public int getSize() {
            return values.length;
        }
        public Object getElementAt(int index) {
            return values[index];
        }
    });

If I run the code as it currently is, it returns "Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError" at the line where jsonList is created. I'm fairly certain I don't have a method loop happening, but I may have overlooked it.

To summarize: I would basically like to be able to draw my custom renderer where the current JList is at. There are several other classes that these two are dependent on (ex. LibraryHandler,

Any help or advice is much appreciated! Thank you for reading all of this.

  • 2
    1) `frame.getContentPane().setLayout(null);` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Nov 08 '16 at 03:35

1 Answers1

0

I'm fairly certain I don't have a method loop happening,

You create an instance of JListCustomRendererBook which extends Frame1 which will invoke the initialize() routine which will then create another JListCutomRendererBook etc, etc.

camickr
  • 321,443
  • 19
  • 166
  • 288