Reputation: 3
I'm somewhat new to Swing, and I've been having a problem with trying to display an array of JPanels inside a JPanel.
The program adds Student objects to an arraylist in the Driver class based off information inputted by the user, and then is supposed to display that information on separate JPanels in the studentPane JPanel, but they never show up after the information is put in.
The first thing I tried was having the Student class return a JPanel by making a method called getInfoPane which returns a JPanel created in the student class with all its necessary info, and then calling:
studentPane.add(students.get(x).getInfoPane());
But that didn't work. Ive also tried making the Student class extend JPanel, adding the student's info to that panel, and then trying to add it to the studentPane JPanel in the Driver class, which doesn't display either.
Currently, I'm trying having a separate arraylist of JPanels called StudentInfo, and adding the students with this code:
for(int x=0; x<Integer.parseInt(numberField.getText()); x++) {
studentInfo.add(new JPanel ()); //add new JPanel to array
studentInfo.get(x).add(new JLabel (students.get(x).toString())); //add info from Student
studentPane.add(studentInfo.get(x)); //add JPanel with student info to main Student JPanel
}
with the toString method being overridden to return a string with the info inputted into the the Student class. This method hasn't worked either. I have checked to make sure the Student class is receiving the information put in, and it prints out fine. I have also tried to directly add components, like a JLabel, to the studentPane in the Driver class but, but it wouldn't work. After a little testing I found that strings were the only thing that would show up on the studentPane. This has lead me to think that the problem deals with something other than passing a JPanel from another class. I have viewed many other questions with similar problems, but haven't been able to make the solutions work for my program.
So far here is the relevant code from my program:
public class Driver extends JFrame {
private Container contentPane;
ArrayList<Student> students = new ArrayList<Student>();
private JPanel studentPane = new JPanel();
ArrayList<JPanel> studentInfo = new ArrayList<JPanel>();
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Driver frame = new Driver();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Driver() {
contentPane = this.getContentPane();
profiles.add(addStudentProfiles); // this chunk has the user input student info and tries to add it to studentPane
addStudentProfiles.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, number, "Number to create", JOptionPane.INFORMATION_MESSAGE );
if(numberField.getValue() != null) {
for(int x=0; x<Integer.parseInt(numberField.getText()); x++) {
while(nameField.getText().equals("") || workEthicField.getText().equals("")) {
JOptionPane.showMessageDialog(null, studentInputs, "New Student", JOptionPane.PLAIN_MESSAGE);
if(nameField.getText().equals("") || workEthicField.getText().equals(""))
JOptionPane.showMessageDialog(null, "Error. Please fill out all of the fields");
}
students.add(new Student(nameField.getText(), Integer.parseInt(workEthicField.getText()), (String)(major1Field.getSelectedItem())));
nameField.setText(""); workEthicField.setValue(null);
}
for(int x=0; x<Integer.parseInt(numberField.getText()); x++) {
studentInfo.add(new JPanel ());
studentInfo.get(x).add(new JLabel (students.get(x).toString()));
studentPane.add(studentInfo.get(x));
}
numberField.setValue(null);
}
}
});
setContentPane(contentPane);
contentPane.setLayout(null);
studentPane.setBorder(BorderFactory.createTitledBorder(BorderFactory.createMatteBorder(3, 3, 3, 3, Color.black), "Students", TitledBorder.CENTER, TitledBorder.CENTER));
studentPane.setLocation(350, 0);
studentPane.setSize(300, (int)getBounds().getHeight()-62);
studentPane.setBackground(Color.lightGray);
studentPane.setVisible(true);
contentPane.add(studentPane);
}
}
And here is the Student class:
public class Student {
private String name, major;
private int workEthic;
private String info = "";
public Student(String name, int workEthic, String major) {
this.name = name;
this.workEthic = workEthic;
this.major = major;
info = name+ "\n" +major+ "\n" +workEthic;
}
public String toString() {
return info;
}
}
So basically, I am just wondering why the program won't display JPanels (or components), in the studentPane JPanel (which is inside the the overall contentPane container), and what would be the correct method for doing so. Any help would be greatly appreciated, since I have been stuck on this problem for awhile now.
Upvotes: 0
Views: 1249
Reputation: 324098
Don't use a null layout on your content pane. Let the content pane take all the space that is available to the frame.
I found that strings were the only thing that would show up on the studentPane.
That statement makes absolutely no sense. A String is not a Component and can't be added to a JPanel. Your code currently adds a JLabel containing the toString() representation of your panel.
The general code for dynamically adding components to a visible GUI is:
panel.add(...);
panel.revalidate(); // invoke layout manager
panel.repaint(); paint all components
By default component have a size of (0, 0), so until you invoke the layout manager, there is nothing to paint.
Upvotes: 1