views:

99

answers:

5

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("");
}
}
+2  A: 

Do you ever actually construct the LeftInputPanel anywhere? (One would think you'd be getting a null pointer exception in the code at the top).

M1EK
I have an InputPanel class. In that class, I build the LeftInputPanel and the RightInputPanel. I then build a ButtonPanel (the one with the listener) inside the RightInputPanel.
Karen
A: 

You shouldn't name methods like attributes. For instance, name() should be getName() or something like that. Might solve your problem.

Marc Müller
A: 

There are a couple things that I see as missing here:

  • within your actionPerformed method, you declare a field and without trying to initialize it, you try to access it. This will be caught by the compiler.
  • I dont see anywhere that you create an AllocateListener or attach it to anything in your panel that would trigger the actionPerformed method.
akf
+1  A: 

What is not working and what are you expecting to happen?

If you expect to be calling a method on an existing object from another object, that is perfectly doable, provided the method is public. The fact that these objects are JPanels are irrelevant.

What you should do is learn how to use the debugger to figure out if your method is being called and the println is occuring but the name is empty, or your method is not called, or any other problem.

If you're using Eclipse, there are some great debugging video tutorials here. But even if you're not using Eclipse you can check them out, and can apply them to whatever IDE you're using. It'll be far more efficient than sprinkling System.out.printlns here and there.

JRL
So the way I have done it, am I calling the method from the panel I have already built? If that's not the reason, what is wrong with it?
Karen
+2  A: 

JPanels are extensions of Component. That means you can always call Component.getParent() to get the immediate container of your component and using that reference, gain access to all sibling components in the container by using Container.getComponents().

More specifically, if you add your panels to the top level container using indexes, then you can specifically request the reference to a sibling component using its index.

This is one way to avoid passing around references to various panels, by using the parent container as a containment context (which is precisely what it is).

And once you have the reference, a class is a class and you obviously can call all visible methods.