I have a student class and I've implemented all my methods correctly. However, I do not know how to get the information out of the object to display them into the GUI, such as the image file and also the texts to be shown.
So, in the GUI Frame I have their name, title, group and demowhat as labels and then the imageFile to actually show the image from the link.
class PersonInfo
{
protected String name;
protected String title;
protected String imageFile;
public PersonInfo(String name, String title, String imageFile)
{
this.name = name;
this.title = title;
this.imageFile = imageFile;
}
public PersonInfo(PersonInfo pi)
{
this(pi.name, pi.title, pi.imageFile);
}
public String getName()
{ return name; }
public String getTitle()
{ return title; }
public String getImageFile()
{ return imageFile; }
public void SetInfo(String name, String title, String imageFile)
{
this.name = name;
this.title = title;
this.imageFile = imageFile;
}
@Override public String toString()
{
return String.format("name: %s%ntitle: %s%nimageFile:%s%n", name, title, imageFile);
}
}
class Student extends PersonInfo
{
private String group;
private String demoWhat;
public Student(String name, String title, String imageFile, String group, String demoWhat)
{
super(name, title, imageFile);
this.group = group;
this.demoWhat = demoWhat;
}
public Student(Student s)
{
super(s);
}
public String getGroup()
{
return group;
}
public String getDemoWhat()
{
return demoWhat;
}
public void SetInfo(String name, String title, String imageFile, String group, String demoWhat)
{
super.SetInfo(name, title, imageFile);
this.group = group;
this.demoWhat = demoWhat;
}
@Override
public String toString()
{
return String.format ("%s" "group: %s%n" "demoWhat: %s%n", super.toString (), group, demoWhat);
}
}
class GUI2 extends JFrame
{
private JLabel label1;
private final JLabel image2;
public GUI2()
{
super("Welcome to 121 Demo System");
setLayout( new FlowLayout());
JButton plainJButton = new JButton("Refresh button to get the next student");
add(plainJButton);
ImageIcon image = new ImageIcon(getClass().getResource("images/xx.png"));
Image imageSIM = image.getImage();
Image imageSIMResized = imageSIM.getScaledInstance(260, 180, DO_NOTHING_ON_CLOSE);
image = new ImageIcon(imageSIMResized);
image2 = new JLabel(image);
add(image2);
ButtonHandler handler1 = new ButtonHandler();
plainJButton.addActionListener(handler1);
}
class ButtonHandler implements ActionListener //call student here
{
@Override
public void actionPerformed(ActionEvent event)
{
GUI3 gui3 = new GUI3();
setLayout( new FlowLayout());
gui3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui3.setSize(450,400);
gui3.setVisible(true);
**//I WANT THE GUI TO POP UP WITH THE NAME, TITLE, IMAGEFILE, GROUP AND DEMOWHAT**
Student student1 = new Student("name", "full time student","images/xxx.JPG", "I am from group 12", "I wish to demo A1");
add(label1);
}
}
}
class GUI3 extends JFrame //firststudent
{
private final JLabel image3;
public GUI3()
{
super("Let us welcome xxx");
JButton plainJButton = new JButton("OK");
add(plainJButton);
//ImageIcon image = new ImageIcon(getClass().getResource("images/xxx.JPG"));
//Image imagexxx= image.getImage();
//Image xxxResized = imagexxx.getScaledInstance(210, 280, DO_NOTHING_ON_CLOSE);
//image = new ImageIcon(xxxResized);
//image3 = new JLabel(image);
//add(image3);
ButtonHandler handler2 = new ButtonHandler();
plainJButton.addActionListener(handler2);
}
}
I tried making the student object in the ButtonHandler class, but then I don't know what to do from here.
CodePudding user response:
Create a JPanel that renders your student. Here is a small example, based on your student class:
class StudentPanel extends JPanel {
JTextField tfGroup;
public StudentPanel() {
add(new JLabel("Group"));
tfGroup = new JTextField();
add(tfGroup);
}
public void setStudent(Student student) {
tfGroup.setText(student.getGroup());
}
}
You will want to add more fields and look at the sizing and arrangement of fields and labels. Maybe your application needs more than one way to render a student (short, full, list).
Once you have such a view, use it like so:
public static void main(String[] args) {
Student student = ... // populate the data you want to show somehow
JFrame f = new JFrame();
StudentPanel panel = new StudentPanel();
panel.setStudent(student);
f.add(panel);
f.pack();
f.setVisible(true);
}