Should I override frameInit for initialization code in a Swing GUI? - java

I noticed that JFrame has a frameInit() method. I'm building a very simple GUI that extends JFrame. The description for the method is:
Called by the constructors to init the JFrame properly.
If I have initialization code (such as setting the title, size, location, etc.), should I override the method, and put my initialization code in there? If not, where should I put initialization code (still assuming that I am extending JFrame, not using composition)?

I guess the best place to put this stuff is in the constructor. Or, if you do overload, don't forget to call super.frameInit().

Related

Access Java JFrame from another class

I have a class that creates a JFrame. When the JFrame is created it has a start button. When the start button is clicked, it runs two threads until the stop button is clicked. The two threads are in another class file. From the class that contains the threads, how can I access the JFrame instance in order to change value that are displayed?
In order to acheive this you have to pass the reference of JFrame using this keyword.
To have access to a private instance within another class, I think you should use agetter.
Example:
//JFrame declaration
private JFrame frame;
//Getter
public JFrame getFrame() {
return frame;
}
As noted by one answer, you can pass in a reference of the GUI or view into any class that needs it, for instance by passing the GUI class into the other class's constructor parameter, and using the parameter to set a field, but having said that, there are caveats:
Always be sure to make Swing state changes on the Swing event thread (the EDT). Since you're using background threading, this means that you will either
use a SwingWorker as your background thread, and notify the GUI of changes via the publish/process method pair, or
your SwingWorker can notify observers of change via PropertyChangeListeners and "bound" properties, or
if using a standard Thread/Runnable, you will need to be sure to queue Swing calls onto the EDT using SwingUtilities.invokeLater(someRunnable)
Even better is to use a Model-View-Control type structure, but even if you do this, the model should be changed on the EDT for the same reasons above.
As a side recommendation in general I try to avoid making classes that extend JFrame as that unnecessarily restricts my code to creating just JFrames.
Note that this help is very general, but if you need more specific help, then you will want to post more specific information regarding your problem and your pertinent code.

How the paint method run without in main method?

Here, we have two classes. One of them is application class.
And the output is a jframe which has a rectangular inside. But I don't get it, we don't write paint method in main method. How can it run?
The paint method is automatically called to refresh your window. For example, the paint method is automatically called when resizing your window.
You can also force to call the paint method by calling the repaint() method of JFrame.
Also, if you're looking for the declaration of paint method, it is in java.awt.Component. Dont hesitate to have a look to the javadoc

Calling non-static methods using NetBeans GUI builder action listener

I'm quite new to Java and programming, but I'm trying to learn by doing. To avoid too much hassle early-on I've used the NetBeans GUI builder to make a basic form with buttons and labels etc.
My problem is that when I call some methods inside the auto-generated Action Listener for a button, I get an error telling me how I cannot call a non-static method ( the dispose() method ) from a static context. While I understand the distinction between static and non-static in theory, I find myself lost when I'm sitting at the keyboard. I feel like I'm missing something important.
I need help getting un-stuck on this particular problem before I can move forward.
Thanks
well you cant just call dispose() as this is an instance method, you need to call dispose on the instance of your JFrame or JDialog or what-ever-window like this:
frame1.dispose():

Basic Java GUI design

In the GUI book we use in class there are many examples of how graphical user interfaces are made in Java. So many examples, that I'm very confused regarding which one should be used when it comes down to a big application.
So I've seen examples
in which the main class extends JFrame
where the JFrame object is created inside the main method
where the main class extends JFrame AND implements ActionEvent interface
where Listener classes are declared inside the main class
Sure, I can work with all of these, but right now, as I don't have any kind of experience, I don't see the benefit of using any of them. Is actually one of them the correct way to do it or it depends on my sittuation?
Thank you!
"Is A" or "Has A"? This is the question that should be asked when considering extending a class. If the new class "Is A" frame, extend frame, but if the class just needs a reference to a frame, don't extend.
In fact, if a custom component is required, extend a JComponent or JPanel, then add that to a frame, ..applet, window, JInternalFrame, dialog, constraint of a layout, part of a split pane..
Listeners
As to the listeners. Rather than traverse a huge if/else structure in the single actionPerformed() method to determine the required action, it is more optimal to either:
Create a listener for each control that needs it.
Create an instance of an AbstractAction that might be used for multiple controls ('copy' button, menu item etc.).
Summary
So (generally) for the:
JFrame, don't extend.
Listeners, create and add as needed.
Honestly, it depends on the situation. One basic rule when coding is to "code to abstract classes or interfaces".
So, in a nutshell, have a class extending (or implementing) a JFrame (or whatever interface or class) and/or have one doing the same thing with ActionListener.
It is all about the maintainability, flexibility and cleanness of your code.
Standard approach: use EventQueue in method main, that creates main form. In that case all your operations will be asynchronous
in which the main class extends JFrame
the main calss doesn't have to extend JFrame. if it doesn't you should create a JFrame object like you do with any other class
where the JFrame object is created inside the main method
If the MainClass extend JFrame it created inside the c'tor (in the super() ).

Swing add new Component

I need to add dynamicaly Components to JPanel, but if i make just add(Component) then component doesn't appears, if i make then JPanel.revalidate(); then it appears, but JPanel blinks, can I make it more fine, without blinking?
Hm, i have found solution,just after add(component); i have write component.repaint(); and it works, but now there is another Problem with Window resizing, if i resize window then all my added components disapeard!!!
This is basic, but you should make sure each component is
1) added from the EDT (see SwingUtilities.invokeLater())
2) added only once per instance
Might be a better idea to add the components on initialization and hide them, making them visible when needed.
Use the method Component.setVisible(boolean b) so show and hide components.
Edit:
I just tried a simple test class where I added random components to the main JFrame and it worked fine.
Try calling JFrame.pack() following JPanel.revalidate().
If this does not make a difference could you post some of your code where you add the dynamic components?
Another Edit:
Make your main component Implement the ComponentListener interface and implement the componentResized(ComponentEvent e) method to call JFrame.pack().
if you add a new component you have to call revalidate.
Example:
panel.add(new JButton(...), ...);
panel.revalidate();
Make sure you're calling this from within the EDT.
If it still flickers have a look at panel.setDoubleBuffered.
Hope that helps, even though example code from your side would be nice to actually see the effect you are describing.

Categories

Resources