Reputation: 13
I am working on a program that is supposed to track the inventory of Blu-ray movies. I have stored the movies in an object array (the array stores the name, ID, number of discs, and price of each movie). I know ArrayLists are a more efficient way of storing the objects in an array, but for the purpose of this assignment I am required to use an array. I have previously had this program print out to the console with not problem, but I'm trying to add this program to a GUI. I have already written a class for the GUI, but I cannot figure out how to add the arrays to the JPanel and JFrame.
Here is my GUI class:
class TextInFrame extends JFrame {
private JLabel greeting;
private JLabel inventoryUnsorted;
private JPanel panel;
public TextInFrame(){
super("Blu-ray Movie Inventory");
setLayout(new FlowLayout());
JFrame.setDefaultLookAndFeelDecorated(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
pack();
setVisible(true);
panel = new JPanel();
greeting = new JLabel(BluRay.programGreeting());
add(greeting);
greeting.setVerticalTextPosition(JLabel.BOTTOM);
greeting.setHorizontalTextPosition(JLabel.CENTER);
panel.add(greeting);
this.add(panel);
}
}
Here is a portion of my main method that has the
public class Inventory {
public static void main(String[] args) {
TextInFrame window = new TextInFrame();
window.setSize(600, 600);
BluRay[] movies = new BluRay[5];
movies[0] = new BluRay("Man of Steel", "48461", 24, 17.99);
movies[1] = new BluRay("Fast Five", "84624", 10, 12.99);
movies[2] = new BluRay("Batman Begins", "15483", 19, 13.98);
movies[3] = new BluRay("X-Men", "48973", 6, 15.99);
movies[4] = new BluRay("The Outsiders", "01893", 16, 9.98);
String[] stringArray = Arrays.copyOf(movies, movies.length, String[].class);
// loop to print through the BluRay movies array
for (String string : stringArray) {
System.out.println(string);
}
Every time I try to add the array into a JLabel or JPanel, I get the error "BluRay[] cannot be converted to String". I am completely stumped on how to get this information in the GUI. I am also having trouble with the greeting in the JPanel. It is not wrapping when the JFrame is resized. STUMPED.
Upvotes: 0
Views: 12998
Reputation: 347184
Take a look at How to Use Lists, they will allow you to display arrbitraty objects in a list on the GUI
Check out Creating a Model and Writing a Custom Cell Renderer in particular
JLabel
doesn't wrap it's content by default. You could use a JTextArea
set to non-editable and modify the background color and border or simply wrap the text of the JLabel
in <HTML>
tags...add us a different layout manager ;)
For example
Upvotes: 4