Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: What is not right here? adding JPanel in JFrame

  1. #1
    Member
    Join Date
    Jun 2010
    Posts
    48
    Thanks
    12
    Thanked 2 Times in 2 Posts

    Default What is not right here? adding JPanel in JFrame

    [SOLVED]

    Hello,
    I am messing up with MVC constructions a little and have a strange problem with adding JPanel to a JFrame. Here is the "skelet" of the code:

    MAIN CLASS:
    public class MVCtest extends JFrame {
    	public MVCtest() {
    		super("MVC TEST");
    		Model model = new Model();
    		View view = new View(model);
    		Controller controller = new Controller(model);
    		view.addActionListener(controller);
    		model.addObserver(view);
    		this.getContentPane().add(view);
    	}
     
    	public static void main(String[ ] args) {
    		final MVCtest app = new MVCtest();
    		app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		javax.swing.SwingUtilities.invokeLater(new Runnable() {
    				public void run() {
    					app.pack();
    					app.setVisible(true);
    				}
    		});
    	}
    }

    MODEL CLASS "SKELET"
    class Model extends Observable {
    	// engine of the app
    }

    VIEW CLASS "SKELET"
    class View extends JPanel implements Observer {
    	private Model model;
    private ShowDeck pCards;
        ....
     
    	public View(Model m) {
    		this.model = m;
    		// initialization of variables
    		// adding swing objects to the extended JPanel:
    add(pCards);
    ....
    	}
     
    	public void addActionListener(ActionListener l) {
    		button.addActionListener(l);
    	}
     
    	public void update(Observable o, Object arg) {
    		// calls some methods from the class Model and does other actions
    //concerned with output on the screen
    	}
    }

    CONTROLLER CLASS "SKELET"
    class Controller implements ActionListener {
    	private Model model;
     
    	public Controller(Model m) {
    		this.model = m;
    	}
     
    	public void actionPerformed(ActionEvent e) {
    		// calls methods of class Model and does some changes in it
    	}
    }

    The problem I have is in class ShowDeck. It extends JPanel and as you can see, in the class View I add it to a View JPanel and in the main method View JPanel is added to the JFrame.
    ShowDeck class calls paintComponent method and draws some stuff on the JPanel.
    So can anyone find any mistakes from the skelet I gave you?

    ================================================== ======================
    ================================================== ======================

    EDIT:
    I though that info isn't enough, so here I paste all code. It is a mess here with lot of unnecessary things.
    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import java.util.*;
     
    public class MVCtest extends JFrame {
    	public MVCtest() {
    		super("MVC TEST");
    		setLayout(new BorderLayout());
    		Model model = new Model();
    		View view = new View(model);
    		BarView bView = new BarView();
    		Controller controller = new Controller(model);
    		view.addActionListener(controller);
    		model.addObserver(view);
    		model.addObserver(bView);
    		this.getContentPane().add(view, BorderLayout.CENTER);
    		this.getContentPane().add(bView, BorderLayout.SOUTH);
    	}
     
    	public static void main(String[ ] args) {
    		final MVCtest app = new MVCtest();
    		app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		javax.swing.SwingUtilities.invokeLater(new Runnable() {
    				public void run() {
    					app.pack();
    					app.setVisible(true);
    				}
    		});
    	}
    }
     
    class Model extends Observable {
    	private CardDeck deck = new CardDeck();
    	protected int i = 0;
     
    	public void increment() {
    		setChanged();
    		notifyObservers(new Integer(i));
    		i++;
    	}
     
    	public CardDeck getDeck() {
    		return deck;
    	}
    	public CardBase getCard() {
    		return deck.getDeck()[i];
    	}
    }
     
    class BarView extends JProgressBar implements Observer {
    	public BarView() {
    		super(0, 52);
    		setStringPainted(true);
    	}
     
    	public void update(Observable o, Object arg) {
    		Integer v = (Integer) arg;
    		setValue(v.intValue());
    	}
    }
     
    class View extends JPanel implements Observer {
    	private Model model;
    	private JButton button;
    	private JLabel label;
    	private ShowDeck pCards;
     
    	public View(Model m) {
    		this.model = m;
    		button = new JButton("Add card");
    		label = new JLabel();
    		pCards = new ShowDeck(new ArrayList());
    		add(button);
    		add(label);
    		add(pCards);
    	}
     
    	public void addActionListener(ActionListener l) {
    		button.addActionListener(l);
    	}
     
    	public void update(Observable o, Object arg) {
    		pCards.addCard(model.getCard());
    		label.setText(model.getCard() + " added");
    	}
    }
     
    class Controller implements ActionListener {
    	private Model model;
     
    	public Controller(Model m) {
    		this.model = m;
    	}
     
    	public void actionPerformed(ActionEvent e) {
    		model.increment();
    	}
    }

    What I actually want to get from this ShowDeck class right now is JPanel with the orange rectangle painted in it.
    import java.util.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.image.*;
     
    public class ShowDeck extends JPanel {
    	public static final int W = Suit.W, H = Suit.H;
    	public static final int XO = 10, YO = 10;
    	public static final int SHIFT = W / 3;
    	Collection deck;
     
    	public ShowDeck(Collection deck) {
    		this.deck = deck;
    	}
     
    	public ShowDeck() {
    		deck = new ArrayList();
    	}
     
    	// sitie veikia tik jeigu JFrame apatine eilute padaro true:
    	// frame.setDefaultLookAndFeelDecorated(true);
     
     
    	public void addCard(CardBase c) {
    		deck.add(c);
    		createAndShowGUI();
    	}
    	public void paint(Graphics g) {
    		g.setColor(Color.ORANGE);
    		g.fillRect(10, 10, 100, 100);
    	}
    /*	protected void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		int x = 10, y = 10;
    		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    		GraphicsDevice gd = ge.getDefaultScreenDevice();
    		GraphicsConfiguration gc = gd.getDefaultConfiguration();
    		BufferedImage bi = gc.createCompatibleImage(900, 200, Transparency.BITMASK);
    		Graphics2D g2 = bi.createGraphics();
    		int k = 0;
    		for(Iterator i = deck.iterator(); i.hasNext();) {
    			System.out.println(++k);
    			CardBase crd = (CardBase) i.next();
    			g2.setColor(Color.RED);
    			g2.fillRect(x, y, 30, 30);
    		//	g2.drawImage(crd.getBI(), x, y, null);
    			x += SHIFT;
    		}
    		g.drawImage(bi, 0, 0, null);
    	//	g.setColor(Color.BLACK);
    	//	g.fillRect(10, 10, 100, 100);
    		g2.dispose();
    	}
    */
    	public static Collection testOp(Collection a, Collection b) {
    		Collection res = new ArrayList(a);
    		res.addAll(b);
    		return res;
    	}
     
    	private static void createAndShowGUI() {
    		Collection all = new ArrayList();
    		all.add(Card.getCard(25));
    		all.add(Card.getCard(5));
    		all.add(Card.getCard(15));
    		all.add(Card.getCard(45));
    		all.add(Card.getCard(35));
    		Collection arg = new ArrayList();
    		all.add(Card.getCard(15));
    		all.add(Card.getCard(51));
    		ShowDeck s1 = new ShowDeck(all);
    		ShowDeck s2 = new ShowDeck(arg);
    		JPanel pnl = new JPanel();
    		pnl.setBorder(new javax.swing.border.TitledBorder(null, "Before operation"));
    		pnl.add(s1);
    		pnl.add(s2);
    		JPanel res = new JPanel();
    		res.setBorder(new javax.swing.border.TitledBorder(null, "After operation"));
    		ShowDeck s3 = new ShowDeck(testOp(all, arg));
    		res.add(s3);
    	}
    }
    Last edited by Asido; August 23rd, 2010 at 07:39 AM.


  2. #2
    Member
    Join Date
    Jun 2010
    Posts
    48
    Thanks
    12
    Thanked 2 Times in 2 Posts

    Default Re: What is not right here? adding JPanel in JFrame

    Solved myself by adding getPreferredSize and getMinimumSize methods in ShowDeck class

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: What is not right here? adding JPanel in JFrame

    Quote Originally Posted by Asido View Post
    Solved myself by adding getPreferredSize and getMinimumSize methods in ShowDeck class
    Well done By the way, I've moved this thread to the correct forum.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

Similar Threads

  1. JPanel in JFrame
    By maele in forum AWT / Java Swing
    Replies: 2
    Last Post: March 8th, 2010, 03:12 AM
  2. Funny business with JFrame, JPanel and JLabel
    By JeffC in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 21st, 2010, 12:26 PM
  3. Imitating a JFrame extended program with JPanel; help needed...
    By emigrant in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 15th, 2010, 01:30 PM
  4. need help with ActionListener,JPanel,JFrame
    By amahara in forum AWT / Java Swing
    Replies: 5
    Last Post: February 3rd, 2010, 12:40 PM
  5. Creating and displaying a JPanel inside another JPanel
    By JayDuck in forum AWT / Java Swing
    Replies: 1
    Last Post: April 7th, 2009, 08:02 AM