Several buttons (JButton) with the same function in different JPanels

1

I'm doing a program (using the NetBeans GUI Builder and CardLayout) with several screens (various JPanels) and all of them will have a start button, which obviously returns to the home screen.

I want to leave the code cleaner, so I created a new class, botoes.java (already planning to use it for other buttons that should be repeated), and a method:

static void inicio(){
    CardLayout c1 = (CardLayout) root.getLayout();
    c1.show(root, "pInicio");
}

When I click the button to execute this method, it works normally, as I expected, but netbeans indicates an error that is bothering me: root has private access in MainPanel , where root is JPanel main (or parent) and MainPage is JFrame.

How do I resolve this?

EDIT:

Part of the Main Screen code:

root = new javax.swing.JPanel();
pInicio = new javax.swing.JPanel();
bInicio1 = new javax.swing.JButton();

root.setLayout(new java.awt.CardLayout());

root.add(pInicio, "pInicio");

private void bInicioActionPerformed(java.awt.event.ActionEvent evt) {                                        
        Botoes.inicio();
}
    
asked by anonymous 18.10.2015 / 03:49

1 answer

1

It may be kind of late, but maybe it will help someone else. When you generate the interface using the Netbeans GUI Builder, the components are generated with private access in the class, that is, you can not reference this object from another class. To solve this problem, drop the properties of the component root (its JPanel) and, on the Código tab, search for Modificadores de Variável (Variable Modifiers) and change the value of this field to public . In this way, the root component can be accessed and modified from any class.

    
11.11.2015 / 13:56