Question

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!

Était-ce utile?

La solution

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

Autres conseils

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top