Bilal Haider
Bilal Haider

Reputation: 99

MVC View Error Java

I'm trying to create an Onscreen telepad where people can press the keypad buttons and itll come up in the text box I haven't made the ActionListner for the keypad yet but I want it to show up in the view... Here's the code for the Keypad Panel and the View there is also a duration timer which I've managed to get working but can't put them into one view

Here's the Keypad Panel

import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JPanel;

public class KeypadPanel extends JPanel {

private JButton noStar;
private JButton noHash;
private JButton[] buttons;
private JButton C;
private JButton add;
private JPanel keypadPanel;

public KeypadPanel(TelepadController controller)  {

    buttons = new JButton[10];
    for (int i = 0; i < buttons.length; i++) {
        buttons[i] = new JButton("" + i);
      //  buttons[i].addActionListener(controller.new NumberButtonListener());
    }

    //noStar.addActionListener(controller.new noStarActionListener);
    //noHash.addActionListener(controller.new noHashActionListener);
    //C.addActionListener(controller.new CActionListener);
    //add.addActionListener(controller.new addActionListener);


    noStar = new JButton("*");
    noHash = new JButton("#");
    C = new JButton("C");
    add = new JButton("+");


    JPanel keypadPanel = new JPanel();
    keypadPanel.setLayout(new GridLayout(4, 3));
    for (int i = 1; i <= 9; i++) {
         keypadPanel.add(buttons[i]);
            add(noStar);
    add(noHash);
  add(C);
 add(add);


    }
}

}

And here's the code for the main View

package londontelepad2;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.FontUIResource;
import org.apache.commons.beanutils.BeanUtils;


public class TelepadView implements Observer {

private StopwatchPanel stopwatchPanel;
private KeypadPanel keypadPanel;
private JFrame frame;

/**
 *
 * @param controller
 */
public TelepadView(TelepadController controller) {

    super();
    this.setResources();

    frame = new JFrame("London Telepad");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    stopwatchPanel = new StopwatchPanel(controller);
    //stopwatchPanel = new StopwatchPanel2(controller);
    keypadPanel = new KeypadPanel(controller);

    frame.getContentPane().add(stopwatchPanel);
    frame.getContentPane().add(keypadPanel);
    frame.pack();
}

public void show() {
    frame.setVisible(true);
}

@Override
public void update(Observable observable, Object arg) {
    if (arg.equals(Properties.TIME)) {
        try {
            stopwatchPanel.setTime(BeanUtils.getProperty(observable,
                    Properties.TIME));
        } catch (Exception e) {
            System.out.println(e);
        }

    }
}

public void setResetState() {
    stopwatchPanel.setButtons(true, false, false);
}

public void setStoppedState() {
    stopwatchPanel.setButtons(false, false, true);

}

public void setRunningState() {
    stopwatchPanel.setButtons(false, true, false);
}

private void setResources() {

    ColorUIResource defaultBackground = new ColorUIResource(Color.white);
    ColorUIResource defaultForeground = new ColorUIResource(Color.black);
    ColorUIResource disabledColor = new ColorUIResource(Color.lightGray);

    FontUIResource smallFont = new FontUIResource(
            new Font("Dialog", Font.BOLD, 12));
    FontUIResource bigFont = new FontUIResource(
            new Font("Dialog", Font.BOLD, 14));

    UIManager.put("Button.background",
            defaultBackground);
    UIManager.put("Button.foreground",
            defaultForeground);
    UIManager.put("Button.disabledText",
            disabledColor);
    UIManager.put("Button.font", smallFont);


    UIManager.put("Label.background",
            defaultBackground);
    UIManager.put("Label.foreground",
            defaultForeground);
    UIManager.put("Label.font", bigFont);

    UIManager.put("Panel.background",
            defaultBackground);
    UIManager.put("Panel.foreground",
            defaultForeground);
}
}

If I do the .add seperately I get an error to do with (actual and formal argument lists differ in length)

and if i do it together I get java.lang.IllegalArgumentException: cannot add to layout: constraint must be a string (or null)

And I can't find what it is im doing wrong!!

All the help in the world would be very appreciated seeing as I'm an Uber noob at java!

Thank you

Bilal

UPDATE

Here's a log of the error I reciveve when I put them in the same .add field

Exception in thread "main" java.lang.NullPointerException
at londontelepad2.KeypadPanel.<init>(KeypadPanel.java:48)
at londontelepad2.TelepadView.<init>(TelepadView.java:46)
at londontelepad2.TelepadController.<init>(TelepadController.java:33)
at londontelepad2.LondonTelepad2.main(LondonTelepad2.java:19)
Java Result: 1

Upvotes: 0

Views: 132

Answers (1)

desperateCoder
desperateCoder

Reputation: 700

Wait, KeypadPanel is just a plain object. Why doesn't it extend JPanel?

you are calling the add()-method of a JFrame to add your components to the frame? You need to call

frame.getContentPane().add(comp);

Upvotes: 3

Related Questions