Java - call methods in Swing when working with inheritance and interfaces

My problem is quite difficult to formulate, but here is the basic plan:

I have an interface:

public interface TheInterface {
    /**
     * 
     * Returns a string
     */

    public String getStuff(); 



}

I have an abstract class that implements this interface:

public abstract class GenericClass implements TheInterface {

    public GenericClass() {
        // TODO Auto-generated constructor stub
    }



    @Override
    public String getStuff() {
        return "Random string";
    }


}

Then I have a class that extends GenericClass

public class GUIClass extends GenericClass {
    private myFrame  myNewFrame;
    public GUIClass() {
        super();
        myNewFrame = new myFrame();

    }

}

As you can see, GenericClass has a frame:

import javax.swing.JFrame;


public class myFrame extends JFrame {

    private myPanel topPanel;

    public myFrame() {

        topPanel= new myPanel(); 
        add(topPanel); 

         setSize(400,200); 
         //setLocation(200,200); 
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
         setTitle("Test Program"); 
         setVisible(true); 
    }

}

And inside this frame there is a panel containing a label:

import java.awt.GridLayout; import java.awt.Label;

import javax.swing.JLabel; import javax.swing.JPanel;

public class myPanel extends JPanel {

    private JLabel myLabel;

    public myPanel() {
        setLayout(new GridLayout(0,2));
        add (new Label("This label should contain the content of getStuff(): "));
        myLabel=new JLabel();
        add (myLabel);
    }

}

getStuff() GenericClass . , , . , - , .

.

+4
2

Observer:

public interface StuffObserver {
/**
 * 
 * Pass whatever you want, perhaps getStuff(),
 * but that method might be removed by the time we're done here
 * (depends on what else might need to query/track it without,
 *  an observer)
 */
private void onStuffChanged(String newStuff); 

}

Panel

public class myPanel extends JPanel implements StuffObserver

private void onStuffChanged(String newStuff)
{
   Runnable changeText = new Runnable() {
       public void run() {
           myLabel.setText(newStuff);
       }
   };
   SwingUtilities.invokeLater(changeText);
}

, myLabel , ( , ?)

, , GenericClass GUIClass StuffObservers ( )

private List<StuffObservers> stuffObservers = new ArrayList<>();
public void addStuffObserver(StuffObserver ob)...
// looks familar? Same way Swing has addActionListener() on some components
public void deleteStuffObserver(StuffObserver ob)... 

GUIClass - :

myNewFrame = new myFrame();
addStuffObserver(myNewFrame.getPanel());

GenericClass GUIClass , getStuff():

for (StuffObserver ob : stuffObservers)
{
    ob.onStuffChanged(someStringRepresentingWhatYouWouldChangeGetStuffTo);
}

getStuff() . , , getStuff() , JLabel , .

+2

GUIClass .

public class GUIClass extends GenericClass {
    private JFrame frame;
    private JPanel panel;
    private JLabel label;

    public GUIClass() {
        super();
        initialisation();
        setLabelText(getStuff());
    }

    private void initialisation() {
        // Label
        this.label = new JLabel();
        this.label.setText(getStuff());

        // Panel
        this.panel = new JPanel();
        this.panel.setLayout(new GridLayout(0, 2));
        this.panel.add(this.label);

        // Frame
        this.frame = new JFrame();
        this.frame.add(this.panel);
        this.frame.setSize(400, 200);
        this.frame.setLocation(200, 200);
        this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.frame.setTitle("Test Program");
        this.frame.setVisible(true);
    }

    private void setLabelText(String text) {
        this.label.setText(text);
    }
}

, , , !

+2

Source: https://habr.com/ru/post/1536148/


All Articles