1

Im designing a parent Jpanel contains JToolBar && inner JPanel.By the JToolBar's action i need to replace the inner JPanel with new JPanel which where i designed already. switching between different JPanels when pressing jtoolbar buttons. How can I do that in NetBeans IDE?

final CardLayout c = new CardLayout();
    jPanel2 = new JPanel(c);
    jPanel2.add(new BarChartPanel(), "CHART");
    jPanel2.add(new ReportViewPanel(), "REPORT");
    ClassLoader cldr = this.getClass().getClassLoader();
    java.net.URL imageURL = cldr.getResource("Images/barimages.jpg");
    ImageIcon aceOfDiamonds = new ImageIcon(imageURL);
    JButton btnChart = new JButton(aceOfDiamonds);
    btnChart.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            c.show(jPanel2, "CHART");

        }
    });
    jToolBar1.add(btnChart);
    jToolBar1.addSeparator();
    jToolBar1.setFloatable(false);

Here 'jPanel2' is my inner panel but while clicking the toolbars's button it's not diplaying!

Mukthi
  • 561
  • 4
  • 13
  • 35

2 Answers2

4

I want to change these panels in the main panel's inner panel.

Use a CardLayout for the layout of the "main panel's inner panel", as shown here.

Game view High Scores view

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • +1 for `CardLayout`; see also this [example](http://stackoverflow.com/questions/4038605/swing-link-toggle-buttons-together-with-a-button-group-along-with-correspondin/4039359#4039359) using `Action` and `JToolbar`. – trashgod Oct 28 '11 at 05:17
  • Thanks for your reply.But im having problem in displaying a new panel in an inner panel area. – Mukthi Oct 28 '11 at 05:45
2

For multiple panel, you might want something like this : (Not a perfect code,but this is the way,i'm just pointing you the way) :

 public class MultiPanels {

    private JScrollPane getContent() {
        Dimension d = new Dimension(300,200);
        JPanel panel = new JPanel(new GridBagLayout());
        panel.add(getPanel(d,  6, Color.red));

    }

    private JScrollPane getPanel(Dimension d, int rows, Color color) 
        {
        JPanel panel = new JPanel(new GridBagLayout());
        panel.setBackground(color);
    }     
 }

and now, in the question, change the look of the main panel if you mean to change the look and feel, its not possible, and if you mean to just change the appearance,say background, you can do that by overriding the paintComponent() method.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
COD3BOY
  • 11,964
  • 1
  • 38
  • 56
  • Thanks for ur reply.Actually i want to change the inner panel of the main panel while JToolbar button clicks. – Mukthi Oct 27 '11 at 11:30
  • 3
    "Swing programs should override paintComponent() instead of overriding paint()."—[The Paint Methods](http://java.sun.com/products/jfc/tsc/articles/painting/index.html#callbacks). – trashgod Oct 28 '11 at 05:19