Question

I'm trying to setup a GUI to display an array of classes, but can't figure out how to add the array to the GUI. I'm fairly new to Java, so I could use all the help possible.

Book[] bk = new Book{10]

JFrame frame = new JFrame("Bookstore");
frame.setSize(600, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
JPanel panel = new JPanel();
frame.getContentPane().add(panel, BorderLayout.NORTH);
frame.getContentPane().add(panel);
frame.setVisible(true);

class Book implements Comparable<Book> {
//rest of code  }

How would I display Book in the GUI?

Was it helpful?

Solution

You're question's a bit unclear, but if you want to present the names of the books in a GUI, first you need to give each Book a name, aka a string representation. Add a name attribute to Book with a getName() method.

Then create a String array containing the names of the books. You can do that by:

String names = new String[books.length];
for(int i=0; i<names.length; i++){
    names[i] = books[i].getName();
}

Then you can input all the names into a JTextArea, or possibly a JComboBox if you want users to be able to choose a book.

With a JComboBox:

JComboBox combo = new JComboBox(names);
panel.add(combo);

With a JTextArea:

JTextArea textArea = new JTextArea(10,names.length);
textArea.setEditable(false);

for(int i=0; i<names.size; i++){
    textArea.append("Book " + i + "#: " + names[i] + "\n");
}

panel.add(textArea);

Hope this helps.

OTHER TIPS

When you need to display the attributes of a collection or array of objects of a class, you have several options available to you including:

  • The simplest would be to use a JTextArea, give it a monospaced font, and display the String representation of each object, with each object on one line, and using a formatted String, such as can be obtained via String.format(...), to display the contents.
  • Better would be to put your collection of objects into a TableModel of some sort and then display the data in a JTable. Easiest would be to use a DefaultTableModel, but this will require that you convert the attributes of each Book object into an array or Vector of Object. More flexible would be to create your own class that extends AbstractTableModel, but this will require more work on your part to create the appropriate methods of the TableModel, and making sure to fire the correct AbstractTableModel notification methods when any of these methods change the data nucleus (usually a collection of Book object) in any significant way.
  • If you want to display one object's attributes at time, you could populate your GUI with several JTextFields, each one corresponding to an attribute of your Book object, and then use Next and Previous JButtons to move through your collection of Books, displaying the attributes of the selected book on button press. Say when the next button is pressed, an int index variable that is used to select a Book from the collection is incremented, and the Book corresponding to that index is selected. Then all of the attributes from that selected Book are displayed in their corresponding text field.

The details will depend on which route you go.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top