# array of JPanel in java....



## tweety_bird_bunny (Sep 26, 2006)

hi...
i am trying to write a program in java....the class uses ActionListener interface....
i have 2 create multiple jpanels so i created an array of jpanels.....
the array works fine in constructor....but as soon as as i try to use the panels of array in public void actionPerformed(ActionEvent ae)interface, the program compiles sucesfully but as soon as i click on the button when the program is runnning a huge list of errors comes in console....
plz help me to sort out the problem...
i'm pasting the code...

//start of code

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

public class  MyFrame1 implements ActionListener
{
	JFrame frame;
	JPanel main,panel[];
	JButton next,previous,endexam;
	JLabel lab1,lab2,lab3,lab4,lab5;
	public MyFrame1()
	{
int i;		
frame = new JFrame("Welcome to My Frame");
		main = new JPanel();	
		JPanel panel[]=new JPanel[2];
  		for(i=0;i<2;i++)panel_=new JPanel();


		next = new JButton("Next");
		previous = new JButton("Previous");

		lab1 = new JLabel("Panel ONE");
		lab2 = new JLabel("Panel TWO");


		main.add(panel[0]);
            	main.add(panel[1]);
                frame.getContentPane().add(main);
		frame.setVisible(true);
		frame.setSize(600,600);
		panel[1].setVisible(false);
		panel[0].add(lab1);
		panel[0].add(next);
		panel[0].add(previous);
		next.addActionListener(this);
		previous.addActionListener(this);
	}

	public void actionPerformed(ActionEvent e)
	{
		JPanel panel[]=new JPanel[2];
		String s = e.getActionCommand();

		if ("Next".equals(s))
		{
			String str ="panel";
			panel[0].setVisible(false);
			panel[1].add(lab2);
			panel[1].add(next);
			panel[1].add(previous);
			panel[1].setVisible(true);
		}
		if ("Previous".equals(s))
		{
			panel[0].setVisible(true);
			panel[0].add(lab1);
			panel[0].add(next);
			panel[0].add(previous);
			panel[1].setVisible(false);
		}
	}
	public static void main(String[] args) 
	{
		MyFrame1 my = new MyFrame1();

	}
}

// end of code.....

also plz refer me some more interactive compiler for java(j2sdk)....i am bored with this whole console interface....
some gui cmpiler (eg devc++ for c....similarly refer me for java) which supports all the basic commands..._


----------



## JGuru (Sep 26, 2006)

@Tweety, This is not the correct way to right a Java program!!!
 If you want to align items say JButtons, or any other Component, you must use
 LayoutManagers!! 
 See this *Layout Manager Tutorial* Click here
 Download *Java Tutorial* from here

 Use a Good Java IDE like *JCreator*: *www.jcreator.com/

 For aligning buttons use *FlowLayout*

*Here is a Sample code using FlowLayout*:


```
[b]

/*
 * FlowLayoutDemo.java requires no other files.
 */

import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;

import java.awt.Dimension;
import java.awt.ComponentOrientation;

public class FlowLayoutDemo {
    public static boolean RIGHT_TO_LEFT = false;

    public static void addComponents(Container contentPane) {
        if (RIGHT_TO_LEFT) {
            contentPane.setComponentOrientation(
                ComponentOrientation.RIGHT_TO_LEFT);
        }
        contentPane.setLayout(new FlowLayout());

        contentPane.add(new JButton("Button 1"));
        contentPane.add(new JButton("Button 2"));
        contentPane.add(new JButton("Button 3"));
        contentPane.add(new JButton("Long-Named Button 4"));
        contentPane.add(new JButton("5"));
    }

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        //JFrames decorated by the Java look and feel
        //can't get smaller than their minimum size.
        //We specify a skinnier minimum size than the
        //content pane will cause the frame to request,
        //so that you can see what happens when you
        //drag the window so that it's narrower than a
        //single row.
        JFrame frame = new JFrame("FlowLayoutDemo") {
            public Dimension getMinimumSize() {
                Dimension prefSize = getPreferredSize();
                return new Dimension(100, prefSize.height);
            }
        };
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Set up the content pane.
        addComponents(frame.getContentPane());

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
```
[/b]


----------



## tweety_bird_bunny (Sep 27, 2006)

actually what i ment was that i want to create an array of jpanels and want the actionPerformed interface to change the panels whenever i click a button or something....
but when i use the notation panel_.setVisible(true); in the interface then a null pointer exception occures while runtime...

i dont knw how to utilize the array of panels in the interface method  though it works perfectly in the above class ...._


----------



## JGuru (Sep 27, 2006)

@tweety, Use *CardLayout*,     CardLayout class lets you implement an area that contains different components at different times. A CardLayout is often controlled by a combo box, with the state of the combo box determining which panel (group of components) the CardLayout displays. An alternative to using CardLayout is using a tabbed pane, which provides similar functionality but with a pre-defined GUI. For further details, see How to Use CardLayout. 


```
[b]
@tweety, Use [b]CardLayout[/b],     CardLayout class lets you implement an area that contains different components at different times. A CardLayout is often controlled by a combo box, with the state of the combo box determining which panel (group of components) the CardLayout displays. An alternative to using CardLayout is using a tabbed pane, which provides similar functionality but with a pre-defined GUI.
 See this example. Maybe this is what you want to do!!

[code]
[b]
/*
 * @(#)CardTest.java	@author JGuru
 */

import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;

class CardPanel extends Panel {
    ActionListener listener;

    Panel create(LayoutManager layout) {
	Button b = null;
	Panel p = new Panel();

	p.setLayout(layout);

	b = new Button("one");
	b.addActionListener(listener);
	p.add("North", b);

	b = new Button("two");
	b.addActionListener(listener);
	p.add("West", b);

	b = new Button("three");
	b.addActionListener(listener);
	p.add("South", b);

	b = new Button("four");
	b.addActionListener(listener);
	p.add("East", b);

	b = new Button("five");
	b.addActionListener(listener);
	p.add("Center", b);

	b = new Button("six");
	b.addActionListener(listener);
	p.add("Center", b);

	return p;
    }

    CardPanel(ActionListener actionListener) {
	listener = actionListener;
	setLayout(new CardLayout());
	add("one", create(new FlowLayout()));
	add("two", create(new BorderLayout()));
	add("three", create(new GridLayout(2, 2)));
	add("four", create(new BorderLayout(10, 10)));
	add("five", create(new FlowLayout(FlowLayout.LEFT, 10, 10)));
	add("six", create(new GridLayout(2, 2, 10, 10)));
    }

    public Dimension getPreferredSize() {
	return new Dimension(200, 100);
    }
}

public class CardTest extends Applet
		      implements ActionListener,
				 ItemListener {
    CardPanel cards;

    public CardTest() {
	setLayout(new BorderLayout());
	add("Center", cards = new CardPanel(this));
	Panel p = new Panel();
	p.setLayout(new FlowLayout());
	add("South", p);

	Button b = new Button("first");
	b.addActionListener(this);
	p.add(b);

	b = new Button("next");
	b.addActionListener(this);
	p.add(b);

	b = new Button("previous");
	b.addActionListener(this);
	p.add(b);

	b = new Button("last");
	b.addActionListener(this);
	p.add(b);

	Choice c = new Choice();
	c.addItem("one");
	c.addItem("two");
	c.addItem("three");
	c.addItem("four");
	c.addItem("five");
	c.addItem("six");
	c.addItemListener(this);
	p.add(c);
    }

    public void itemStateChanged(ItemEvent e) {
	((CardLayout)cards.getLayout()).show(cards,
	                                     (String)(e.getItem()));
    }

    public void actionPerformed(ActionEvent e) {
	String arg = e.getActionCommand();

	if ("first".equals(arg)) {
	    ((CardLayout)cards.getLayout()).first(cards);
	} else if ("next".equals(arg)) {
	    ((CardLayout)cards.getLayout()).next(cards);
	} else if ("previous".equals(arg)) {
	    ((CardLayout)cards.getLayout()).previous(cards);
	} else if ("last".equals(arg)) {
	    ((CardLayout)cards.getLayout()).last(cards);
	} else {
	    ((CardLayout)cards.getLayout()).show(cards,(String)arg);
	}
    }

    public static void main(String args[]) {
	Frame f = new Frame("CardTest");
	CardTest cardTest = new CardTest();
	cardTest.init();
	cardTest.start();

	f.add("Center", cardTest);
	f.setSize(300, 300);
	f.show();
    }
    
    public String getAppletInfo() {
        return "Demonstrates the different types of layout managers.";
    }
}
```
[/b]


----------

