0

I decided using BorderLayout for swing design named Loginwards. And I wrote 5 Classes for 5 side of BorderLayout as PageStart, LineStart, Center, LineEnd, PageEnd.

I have undecorated Loginwards and designed PageStart for use minimize,resize,quit.(for my own images, rules...) button quit is simple because it have worked like this

button.addActionListener(new ActionListener() {
    @Override
        public void actionPerformed(ActionEvent arg0) {
            System.exit(0);

        }
    });

but for example minimize I could not solve. I should have changed my Loginwards properties from another class object

Sum up

public class Loginwards extends Jframe
{   ...
    JFrame frame = new JFrame("BorderLayoutDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(PageStart.Panel(), BorderLayout.PAGE_START);
    ...
}


public class PageStart{
   public static JPanel Panel(){
     JPanel panel = new JPanel();
     FlowLayout pagestart = new FlowLayout(FlowLayout.RIGHT);
     panel.setLayout(pagestart);
     panel.add(MinimizeButton()); 
     panel.add(ResizeButton()); 
     panel.add(QuitButton()); 
   }
   public static JButton MinimizeButton(){
    JButton button = new JButton();
    button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
        // I Should be able to reach Loginwards here.
        }
    });
    return button;
}
Tekin Güllü
  • 363
  • 1
  • 3
  • 18
  • why? you don't even have an instance of Loginwards in that class – Stultuske Sep 06 '18 at 12:11
  • I want to minimize my Loginwards when click in PageStart class – Tekin Güllü Sep 06 '18 at 12:15
  • as far as that PageStart class is concerned, there is no Loginwards – Stultuske Sep 06 '18 at 12:21
  • 1
    Your design/code is unclear. Why `Loginwards extends Jframe`? And why at the same time it has `JFrame frame = new JFrame("BorderLayoutDemo");`? It is like saying `Loginwards` is a box which has its own box called `frame`. Which box are you using (if both then, how)? – Pshemo Sep 06 '18 at 13:29
  • 1
    Not really related to question, but you should take a look at [Extends JFrame vs. creating it inside the program](https://stackoverflow.com/q/22003802) – Pshemo Sep 06 '18 at 13:32
  • 1
    You should not be using static methods and variables. This indicates a design problem with your classes. Don't have enough information about what you are attempting to do to provide an alternative. But the basic answer is if you want to invoke a method in one class from another class, then the first class needs a reference to the second class. – camickr Sep 06 '18 at 13:53
  • @Pshemo you are right JFrame is useless in this case – Tekin Güllü Sep 06 '18 at 14:07

1 Answers1

1

MinimizeButton is a static method, so pass Loginwards instance as it's parameter, then make use of it in your listener:

public static JButton MinimizeButton(Loginwards loginwards){
    JButton button = new JButton();
    button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            // Invoke whatever you need on Loginwards...
            loginwards.doSomething();
        }
    });
    return button;
}

Btw, method names should start with lowercase... see naming conventions for Java

david a.
  • 5,283
  • 22
  • 24
  • You should NOT be using static methods and variables. This indicates a design problem with your classes. – camickr Sep 06 '18 at 13:53