I'm trying to call a method from a panel class, however it does not result in anything. Can panels communicate with each other? Or is there another reason why this isn't working?
Calling the method name() in the leftInput class.
ButtonPanel class.
import model.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonPanel extends JPanel implements View
{
private Prison prison;
private JButton button = new JButton("Allocate Cell");
private LeftInputPanel leftInput;
private CrimePanel crimePanel;
public ButtonPanel(Prison prison, LeftInputPanel leftInput)
{
this.prison = prison;
this.leftInput = leftInput;
setup();
build();
}
public void setup()
{
}
public void build()
{
Dimension size = new Dimension(240, 70);
button.setPreferredSize(size);
button.setMinimumSize(size);
button.setMaximumSize(size);
button.addActionListener(new AllocateListener());
add(button);
}
public void update()
{
leftInput.clear();
}
private class AllocateListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Criminal criminal = new Criminal(leftInput.name());
prison.add(criminal);
System.out.println(leftInput.name());
}
}
}
leftInput class.
import model.*;
import java.awt.*;
import javax.swing.*;
public class LeftInputPanel extends JPanel
{
private Prison prison;
public JTextField name = new JTextField();
public JTextField days = new JTextField();
public JTextField months = new JTextField();
public JTextField years = new JTextField();
public LeftInputPanel(Prison prison)
{
this.prison = prison;
setup();
build();
}
public void setup()
{
setLayout(new FlowLayout());
Dimension size = new Dimension(100, 190);
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
}
public void build()
{
JLabel label = new JLabel("Name");
Dimension size = new Dimension(90, 20);
name.setPreferredSize(size);
add(label);
add(name);
Box box = Box.createVerticalBox();
box.add(daysPanel());
box.add(monthsPanel());
box.add(yearsPanel());
add(box);
}
public JPanel daysPanel()
{
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.LEFT));
addField(panel, days, " days");
return panel;
}
public JPanel monthsPanel()
{
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.LEFT));
addField(panel, months, " months");
return panel;
}
public JPanel yearsPanel()
{
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.LEFT));
addField(panel, years, " years");
return panel;
}
public void addField(JPanel panel, JTextField field, String label)
{
Dimension size = new Dimension(30, 20);
field.setPreferredSize(size);
field.setMinimumSize(size);
field.setMaximumSize(size);
panel.add(field);
panel.add(new JLabel(label));
}
public String name()
{
return name.getText();
}
public int days()
{
return Integer.parseInt(days.getText());
}
public int months()
{
return Integer.parseInt(months.getText());
}
public int years()
{
return Integer.parseInt(years.getText());
}
public void clear()
{
name.setText("");
days.setText("");
months.setText("");
years.setText("");
}
}