Need help on Model View Controller approach

Hi,

I've written an inventory program that's based on the MVC approach. The main class is TabbedJFrame.java. It's supposed to display 2 views, BrowseView.java and FindView.java into it, onto their respective tabs.
But instead, when I run it it only displays an empty frame with a title, but the tabs containing each of the views aren't there. I get a message saying 'Exception in thread "main" java.lang.NoClassDefFoundError:TabbedJFrame(wrong name:views/ TabbedJFrame)' What's this message about? How can I get the views to be displayed?

Here's my code:

[start of TabbedJFrame code]
//TabbedJFrame
package views;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TabbedJFrame extends JFrame {
private static final int ITEMS_IN_TABBED_VIEW=2;

JTabbedPane pane = new JTabbedPane();
BrowseView browse;
FindView find;

Container c = getContentPane();
private static TabbedJFrame singletonInstance = new TabbedJFrame();
public static TabbedJFrame getInstance() {
return singletonInstance;
}

private TabbedJFrame() {
super("Watch Inventory Program");
setBounds(100,100,320,200);
show();
setupTabbedPanes();

addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
dispose();
System.exit(0);
}
});
}

public void setBrowseView() {
setView(browse = new BrowseView());
}

public void setFindView() {
setView(find=new FindView());
}

public void setView(JFrame view){
c.removeAll();
c.add(view,BorderLayout.CENTER);
view.revalidate();
}

private void setupTabbedPanes() {
MouseListener[] listeners = new MouseListener[ITEMS_IN_TABBED_VIEW];
listeners[0]=new controllers.BrowseViewController();
listeners[1]=new controllers.FindViewController();

String labels[]={"Browse View","Find View"};

addTabsToTabbedFrame(labels,listeners);

}

public void addTabsToTabbedFrame(String[] tabnames,MouseListener listeners[]) {
for(int i=0; i<tabnames.length;i++) {
JLabel label = new JLabel(tabnames[i]);

label.addMouseListener(listeners[i]);
pane.add(label);

}
c.add(pane);
}
}
[end of TabbedJFrame code]

[start of BrowseView code]
package views;

import model.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class BrowseView extends JFrame {
// JRadioButton allBrands, allCategories, both;
JPanel panel= new JPanel();
JScrollPane scrollpane;

// panel.setLayout(new BorderLayout());

StringBuffer sb = new StringBuffer(20);

Watches[] w;

double currency;
String dollars;

JRadioButton allBrands= new JRadioButton("All Brands");
JRadioButton allCategories=new JRadioButton("All Categories");
JRadioButton both=new JRadioButton("Both");

ButtonGroup group = new ButtonGroup();//"Browse Options"

public BrowseView() {
//Container contentpane = getContentPane();
setLayout(new FlowLayout());

// allBrands= new JRadioButton("All Brands");
// allCategories=new JRadioButton("All Categories");
// both=new JRadioButton("Both");
panel.setLayout(new BorderLayout());

try {

allBrands.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
panel.setLayout(new BorderLayout());

for (int i=0;i<model.ReadWatchesInfo.getInstance().getLength();i++){
w=model.ReadWatchesInfo.getInstance().allBrand();
while (w != null){
addtoPanel(w[i]);
}
}
}
});

allCategories.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
panel.setLayout(new BorderLayout());

for (int i=0;i<model.ReadWatchesInfo.getInstance().getLength();i++){
w=model.ReadWatchesInfo.getInstance().allBrand();
while (w != null){
addtoPanel(w[i]);
}

}
}
});

both.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
panel.setLayout(new BorderLayout());

for (int i=0;i<model.ReadWatchesInfo.getInstance().getLength();i++){
w=model.ReadWatchesInfo.getInstance().allBrandsandCategories();
while (w != null){
addtoPanel(w[i]);
}
}
}
});

}catch(NullPointerException npe) {
System.out.println("null exception");
}

scrollpane = new JScrollPane(panel);


both.setSelected(true);

add(allBrands);
add(allCategories);
add(both);

group.add(allBrands);
group.add(allCategories);
group.add(both);

//contentpane.add(group);
add(scrollpane);
}


public void addtoPanel(Watches w) {
String[] tempfeatures;
JLabel feature=null;

JLabel brand = new JLabel(w.getBrand());
JLabel category = new JLabel(w.getCategory());

for (int x=0; x< w.featureLength();x++) {
tempfeatures=w.getFeatures();

feature = new JLabel(tempfeatures[x]+", ");
}
if (w.getImagePath()==null) {
panel.add(new JButton(new ImageIcon("nographic.jpeg",BorderLayout.CENTER)));
}
else {
panel.add(new JButton(new ImageIcon(w.getImagePath(),BorderLayout.CENTER)));
panel.add(brand,BorderLayout.SOUTH);
panel.add(category,BorderLayout.SOUTH);
double currency = w.getPrice();
String dollars=sb.append("$").append(currency).toString();
JLabel dollar = new JLabel(dollars);

panel.add(dollar,BorderLayout.SOUTH);
panel.add(feature,BorderLayout.SOUTH);
}
}

}

[end of BrowseView code]


[start of BrowseView controller]
package controllers;

//import views.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

//This controller notifies the view to respond to user input. The view
//the retrieves the data it needs from the model.

public class BrowseViewController implements MouseListener {
public void mouseClicked(MouseEvent e) {
views.TabbedJFrame.getInstance().setBrowseView();
}

public void mouseEntered(MouseEvent e) {
}

public void mouseExited(MouseEvent e) {
}

public void mousePressed(MouseEvent e) {
}

public void mouseReleased(MouseEvent e) {
}


}
[end of BrowseViewController]
package controllers;

//import java.views.*;
import java.awt.*;
import java.awt.event.*;

//This controller notifies the view to respond to user input. The view
//the retrieves the data it needs from the model.

public class FindViewController implements MouseListener {

public void mouseClicked(MouseEvent e) {
views.TabbedJFrame.getInstance().setFindView();
}

public void mouseEntered(MouseEvent e) {
}

public void mouseExited(MouseEvent e) {
}

public void mousePressed(MouseEvent e) {
}

public void mouseReleased(MouseEvent e) {
}

}
[start of FindViewController]

[end of FindViewController]
































































Comments

  • : Hi,
    :
    : I've written an inventory program that's based on the MVC approach. The main class is TabbedJFrame.java. It's supposed to display 2 views, BrowseView.java and FindView.java into it, onto their respective tabs.
    : But instead, when I run it it only displays an empty frame with a title, but the tabs containing each of the views aren't there. I get a message saying 'Exception in thread "main" java.lang.NoClassDefFoundError:TabbedJFrame(wrong name:views/ TabbedJFrame)' What's this message about? How can I get the views to be displayed?
    :
    : Here's my code:
    :
    : [start of TabbedJFrame code]
    : //TabbedJFrame
    : package views;
    :
    : import javax.swing.*;
    : import java.awt.*;
    : import java.awt.event.*;
    :
    : public class TabbedJFrame extends JFrame {
    : private static final int ITEMS_IN_TABBED_VIEW=2;
    :
    : JTabbedPane pane = new JTabbedPane();
    : BrowseView browse;
    : FindView find;
    :
    : Container c = getContentPane();
    : private static TabbedJFrame singletonInstance = new TabbedJFrame();
    : public static TabbedJFrame getInstance() {
    : return singletonInstance;
    : }
    :
    : private TabbedJFrame() {
    : super("Watch Inventory Program");
    : setBounds(100,100,320,200);
    : show();
    : setupTabbedPanes();
    :
    : addWindowListener(new WindowAdapter() {
    : public void windowClosing(WindowEvent we) {
    : dispose();
    : System.exit(0);
    : }
    : });
    : }
    :
    : public void setBrowseView() {
    : setView(browse = new BrowseView());
    : }
    :
    : public void setFindView() {
    : setView(find=new FindView());
    : }
    :
    : public void setView(JFrame view){
    : c.removeAll();
    : c.add(view,BorderLayout.CENTER);
    : view.revalidate();
    : }
    :
    : private void setupTabbedPanes() {
    : MouseListener[] listeners = new MouseListener[ITEMS_IN_TABBED_VIEW];
    : listeners[0]=new controllers.BrowseViewController();
    : listeners[1]=new controllers.FindViewController();
    :
    : String labels[]={"Browse View","Find View"};
    :
    : addTabsToTabbedFrame(labels,listeners);
    :
    : }
    :
    : public void addTabsToTabbedFrame(String[] tabnames,MouseListener listeners[]) {
    : for(int i=0; i<tabnames.length;i++) {
    : JLabel label = new JLabel(tabnames[i]);
    :
    : label.addMouseListener(listeners[i]);
    : pane.add(label);
    :
    : }
    : c.add(pane);
    : }
    : }
    : [end of TabbedJFrame code]
    :
    : [start of BrowseView code]
    : package views;
    :
    : import model.*;
    : import javax.swing.*;
    : import java.awt.*;
    : import java.awt.event.*;
    :
    : public class BrowseView extends JFrame {
    : // JRadioButton allBrands, allCategories, both;
    : JPanel panel= new JPanel();
    : JScrollPane scrollpane;
    :
    : // panel.setLayout(new BorderLayout());
    :
    : StringBuffer sb = new StringBuffer(20);
    :
    : Watches[] w;
    :
    : double currency;
    : String dollars;
    :
    : JRadioButton allBrands= new JRadioButton("All Brands");
    : JRadioButton allCategories=new JRadioButton("All Categories");
    : JRadioButton both=new JRadioButton("Both");
    :
    : ButtonGroup group = new ButtonGroup();//"Browse Options"
    :
    : public BrowseView() {
    : //Container contentpane = getContentPane();
    : setLayout(new FlowLayout());
    :
    : // allBrands= new JRadioButton("All Brands");
    : // allCategories=new JRadioButton("All Categories");
    : // both=new JRadioButton("Both");
    : panel.setLayout(new BorderLayout());
    :
    : try {
    :
    : allBrands.addItemListener(new ItemListener() {
    : public void itemStateChanged(ItemEvent event) {
    : panel.setLayout(new BorderLayout());
    :
    : for (int i=0;i<model.ReadWatchesInfo.getInstance().getLength();i++){
    : w=model.ReadWatchesInfo.getInstance().allBrand();
    : while (w != null){
    : addtoPanel(w[i]);
    : }
    : }
    : }
    : });
    :
    : allCategories.addItemListener(new ItemListener() {
    : public void itemStateChanged(ItemEvent event) {
    : panel.setLayout(new BorderLayout());
    :
    : for (int i=0;i<model.ReadWatchesInfo.getInstance().getLength();i++){
    : w=model.ReadWatchesInfo.getInstance().allBrand();
    : while (w != null){
    : addtoPanel(w[i]);
    : }
    :
    : }
    : }
    : });
    :
    : both.addItemListener(new ItemListener() {
    : public void itemStateChanged(ItemEvent event) {
    : panel.setLayout(new BorderLayout());
    :
    : for (int i=0;i<model.ReadWatchesInfo.getInstance().getLength();i++){
    : w=model.ReadWatchesInfo.getInstance().allBrandsandCategories();
    : while (w != null){
    : addtoPanel(w[i]);
    : }
    : }
    : }
    : });
    :
    : }catch(NullPointerException npe) {
    : System.out.println("null exception");
    : }
    :
    : scrollpane = new JScrollPane(panel);
    :
    :
    : both.setSelected(true);
    :
    : add(allBrands);
    : add(allCategories);
    : add(both);
    :
    : group.add(allBrands);
    : group.add(allCategories);
    : group.add(both);
    :
    : //contentpane.add(group);
    : add(scrollpane);
    : }
    :
    :
    : public void addtoPanel(Watches w) {
    : String[] tempfeatures;
    : JLabel feature=null;
    :
    : JLabel brand = new JLabel(w.getBrand());
    : JLabel category = new JLabel(w.getCategory());
    :
    : for (int x=0; x< w.featureLength();x++) {
    : tempfeatures=w.getFeatures();
    :
    : feature = new JLabel(tempfeatures[x]+", ");
    : }
    : if (w.getImagePath()==null) {
    : panel.add(new JButton(new ImageIcon("nographic.jpeg",BorderLayout.CENTER)));
    : }
    : else {
    : panel.add(new JButton(new ImageIcon(w.getImagePath(),BorderLayout.CENTER)));
    : panel.add(brand,BorderLayout.SOUTH);
    : panel.add(category,BorderLayout.SOUTH);
    : double currency = w.getPrice();
    : String dollars=sb.append("$").append(currency).toString();
    : JLabel dollar = new JLabel(dollars);
    :
    : panel.add(dollar,BorderLayout.SOUTH);
    : panel.add(feature,BorderLayout.SOUTH);
    : }
    : }
    :
    : }
    :
    : [end of BrowseView code]
    :
    :
    : [start of BrowseView controller]
    : package controllers;
    :
    : //import views.*;
    : import javax.swing.*;
    : import java.awt.*;
    : import java.awt.event.*;
    :
    : //This controller notifies the view to respond to user input. The view
    : //the retrieves the data it needs from the model.
    :
    : public class BrowseViewController implements MouseListener {
    : public void mouseClicked(MouseEvent e) {
    : views.TabbedJFrame.getInstance().setBrowseView();
    : }
    :
    : public void mouseEntered(MouseEvent e) {
    : }
    :
    : public void mouseExited(MouseEvent e) {
    : }
    :
    : public void mousePressed(MouseEvent e) {
    : }
    :
    : public void mouseReleased(MouseEvent e) {
    : }
    :
    :
    : }
    : [end of BrowseViewController]
    : package controllers;
    :
    : //import java.views.*;
    : import java.awt.*;
    : import java.awt.event.*;
    :
    : //This controller notifies the view to respond to user input. The view
    : //the retrieves the data it needs from the model.
    :
    : public class FindViewController implements MouseListener {
    :
    : public void mouseClicked(MouseEvent e) {
    : views.TabbedJFrame.getInstance().setFindView();
    : }
    :
    : public void mouseEntered(MouseEvent e) {
    : }
    :
    : public void mouseExited(MouseEvent e) {
    : }
    :
    : public void mousePressed(MouseEvent e) {
    : }
    :
    : public void mouseReleased(MouseEvent e) {
    : }
    :
    : }
    : [start of FindViewController]
    :
    : [end of FindViewController]
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    Where's your main() method. Every program needs a main() method to run properly. Also the path in which you store the classes is important, because that defines the package-structure. Seen from the class containing the main() method, the TabbedJFrame class file shoul dbe in the views subdirectory.
  • Hi zibadian!
    Thanx, it's true that TabbedJFrame was missing a main method(oops!). Also, I decided to extend BrowseView to JPanel instead of JFrame. When I did this, the BrowseView panel got displayed onto the TabbedJFrame.

    :)
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories