0

The Matisse code from Netbeans is blocked. The problem I have is that I have to setBackground to a JLabel from another Class in different package but I cannot do this because i have no access to the JLabel due to its private and blocked code.

Is thare any solution to this?

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Alpha2k
  • 2,212
  • 7
  • 38
  • 65

2 Answers2

2

"The Matisse code from Netbeans is blocked"

You can edit it as seen here

"because i have no access to the JLabel due to its private and blocked code"

Just write a getter method for the label in the other class

public class OtherClass .. {
    private JLabel jLabel1;

    public JLabel getLabel() {
        return jLabel1;
    }
}

import otherpackage.OtherClass;

public class MainFrame extends JFrame {
    private OtherClass otherClass;
    ...
    private void jButtonActionPerformed(ActionEvent e) {
         JLabel label = otherClass.getLabel();
         label.setBackground(...)
    }
}

"Access jframe component from another class"

Sounds like you're using multiple frames. See The Use of Multiple JFrames, Good/Bad Practice?


UPDATE

" I have a MAIN frame made in matisse but due to some reasons i have to set the background of an textField inside matisse from another class when X validation happens in the other class"

What you can do then is pass a reference of the Main frame to the other class, and have a setter in the Main frame. Something like (I will provide an interface for access)

public interface Gettable {
    public void setLabelBackground(Color color);
}

public class Main extends JFrame implements Gettable {
    private JLabel jLabel1;
    private OtherPanel otherPanel;

    public void initComponents() {
        otherPanel = new OtherPanel(Main.this); // see link above to edit this area
    }

    @Override
    public void setLabelBackground(Color color) {
        jLabel1.setBackground(color);
    }
}

public class OtherPanel extends JPanel {
    private Gettable gettable;

    public OtherPanel(Gettable gettable) {
        this.gettable = gettable;
    }

    private void jButtonActionPerformed(ActionEvent e) {
        gettable.setLabelBackground(Color.RED);
    }
}
Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Not using multiple frames... I have a MAIN frame made in matisse but due to some reasons i have to set the background of an textField inside matisse from another class when X validation happens in the other class... This isnt the solution :P – Alpha2k Apr 11 '14 at 08:15
  • Can you provide some sample code to illustrate the problem in more detail – Paul Samsotha Apr 11 '14 at 08:17
  • This is the entire project http://www.mediafire.com/download/559r2joiqmc752n/TransferenciaBancaria.7z check out the class Fecha, there is a comment – Alpha2k Apr 11 '14 at 08:25
  • See my **UPDATE** I think you are looking for something more like that. Look at the link in the beginning of the question to see how to edit the code in the `initComponents()` – Paul Samsotha Apr 11 '14 at 08:25
  • 1
    1- I'd be carful suggested that people can edit the protected blocks of the form generating, as it may be regenerated the next time the form is opened. 2- I'd also be careful about exposing UI components via getters, this leaves the open to modifications which may not want (including the ability of code to remove the,), personally, I'd use a property setter instead – MadProgrammer Apr 11 '14 at 09:45
  • @MadProgrammer Thanks I'll make note of that – Paul Samsotha Apr 11 '14 at 09:56
0
  • Create a listener for the class with JLabel with method for changing background of label
  • Implement it in the class where JLabel is used
  • Set listener of other class ( from which you want to change the BG ) to be the listener of class with JLabel
  • Change Background after whatever function you want.
Haris Mehmood
  • 854
  • 4
  • 15
  • 26