Java: JFrame value extension and 1 listener for multiple actions

I am a bit confused. I am currently working (just started learning) and need to create a fully editable table (I will be using SQL soon). So I have 2 questions:

  • What do you mean by "Don't extend JFrame"? Let's say I have a class called "TestDialog" and also a JFrame called "TestUI". It would be ok to write

    public class TestDialog extends TestUI

    ?

    As I understand it, you shouldn't create a class (called MyExample) and inside this class just write

    public class MyExample extends JFrame

    Because you are creating the JFrame in an existing class and not creating it separately.

  • I'll keep him posted. Can I use 2 actions in 1 listener (for 1 button)? Something like:

     public void actionPerformed(ActionEvent e) 
     {
         Action_One; Action_Two;
     }
    
          

    • Or do I need to use 2 different listeners?

Ok what I think. I wish I had written everything clearly, I just signed up here and is really focused on translating things from my language into English. If anyone could tell me how to write here like in Eclipse, I would appreciate it because I couldn't figure out how to do it.

+3


source to share


2 answers


Composition over inheritance is an important programmatic approach. Therefore, I prefer creating a GUI.

public class Application {

  private JFrame mainFrame;

  private MainPanel mainPanel;

  private void installFrame() {
    // initialize main frame
    mainFrame = new JFrame("Title");
  }

  private void installComponents() {
    // install all components
    mainPanel = new MainPanel();
  }

  private void layout() {
    // provide layouting
    mainFrame.add(mainPanel.getComponent());
  }

  private void show() {
    mainFrame.setVisible(true);
  }

  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        Application app = new Application();
        app.installFrame();
        app.installComponents();
        app.layout();
        app.show();
      }
    });
  }
}

      

The main panel does not inherit from JPanel, but uses its instance.

public class MainPanel {
  private JPanel mainPanel;

  public MainPanel() {
    mainPanel = new JPanel(new GridBagLayout()); // or another layout
    initComponents();
    layout();
  }

  private void initComponents() {
    // init all components here
  }

  private void layout() {
    // layout panel here
  }

  public Component getComponent() {
    return mainPanel;
  }
}

      



The same template that I use for every complex component (like trees, tables, lists, tabs, etc.). But this approach has one drawback: there is no GUI constructor that supports it.

About action: you can provide joint action. Something like that

public class CombinedAction extends AbstractAction {
  private Action[] delegates;
  public CombinedAction(String name, Icon icon, Action... someDelegates) {
    super(name, icon);
    delegates = someDelegates;
  }

  public void actionPerformed(ActionEvent ae) {
    for (Action delegate : delegates) {
      delegate.actionPerfromed(ae);
    }
  }
}

      

+5


source


What does 2 actions mean? You can do anything in the method actionPerformed()

. If instead your question is "Can I have 2 actionPerformed methods in the class", that is, there are two actionListeners, then it is "NO". Instead, you should read Inner Classes.

And with regard to your first question, this is more of a design issue.
I would prefer



public class Example{
    JFrame frame;
    public void initialize()
    {
    frame.setSize(//params);
    frame......
    //other frame initilizing code

    }
    public static void main(string[] args)
    {
    Example example=new Example();
    example.initialize();


    }

}//class ends

      

while someone else might feel different

-1


source







All Articles