Netbeans Java Interface won't display when called in the main class - java

I have designed a Java Interface in Netbeans, however when I try to run it in the main class, instead of displaying, a blank new window is created in the corner of the screen.
This is my code:
private static User_Interface myUI = new User_Interface();
public static void main(String[] args) throws InterruptedException, IOException{
SwingUtilities.invokeLater(new Runnable(){
#Override
public void run(){
myUI.setVisible(true);
}
});
}
I have already tried to implement advice from this question
NetBeans Java Project Won't Display GUI Window Despite No Error
however this still doesn't help.
The interface code is all just the generated code from designing the interface, with a few setText() methods and empty actionPerformed methods. It does extend JFrame though.
Any help would be fantastic, thanks.

I guess.. There may be an issue with your layout manager. Set the layout to null or any other with which you are comfortable. In netbeans you have to select a layout according to your need. In my case the default layout was gridBagLayout that was alot pain for me while resolving the issue. Simply check your layout first. May be that resolves your issue.

Sorry for wasting your time, it appears that the source code for the interface was missing it's initComponents() method.

Related

How to execute some action after the JFrame are displayed

I have this GUI application, and I want to execute a custom action inmediatelly when the user open the application, but after the GUI are shown.
So, I put the call to the action into the public Main() of the JFrame like this:
public Main() {
initComponents();
ExecuteAfter();
}
Where ExecuteAfter() is the method that contain the acction, or actions to execute.
This works fine, but not in the way I want. This way, the action executes allways before the JFrame are displayed, that is before the aplication windows appear in the screen. What I want is that execute the action only after the JFrame are displayed, that is after the aplication windows appear in the screen.
I tried put the call into the public static void main(String args[]) because there's where the JFrame is created and displayed. But doesn't work because the method isn't static, and I can't put static that method because it use some components of the JFrame that are already initialized non-static by the IDE.
So, the question is: Where I need to put the call for the action can be executed after the JFrame are displayed on the screen? Or there's other way of doing that?
Thanks in advance!
I used a WindowListener and solved the problem.
Instead of put the call in the constructor public Main() or in the main public static void main(String args[]) which cannot be done, I configured a WindowsListener for do the call. Like that:
private void formWindowOpened(java.awt.event.WindowEvent evt) {
ExecuteAfter();
}
And works perfectly in the way I want.
Thanks #MadProgrammer for the tip.
If i understand you question, I use a similar case for my project. I was needed to start timer when JFrame show, so this is how i do that.
So i use 2 methods and 1 constructor. First method (exp. Name: prepare GUI), here you can add all thinks what you need to create JFrame, JPanel....and that method I call in constructor. In second method (exp. Name: start GUI) you will add all components to JPanel/s, JPanel/s to JFrame, and set JFrame visible = (true) and then add your method ExecuteAfter(). That second method(prepare GUI) you need to call in main method. I hope that's will help you.

NetBeans inserts main() method in every added frame

When adding new class extending JFrame (or java.awt.Frame) the class is added with main() method inside like this:
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewFrame5().setVisible(true);
}
});
}
Every JFrame class has its own main method and I guess all classes starts simultaneously.
How do I add frames without main methods?
Firstly, see The Use of Multiple JFrames, Good/Bad Practice?. Generally, recommended not to do it. See accepted answer for other possibilities (for example a JDialog)
As for your main concern, there's no way around netbeans creating the main method for top level containers like JFrame and JDialog. The logic seems right in the case of JFrame, as an application should only have one JFrame as the main top-level container for the application, but I'm not sure the logic behind the JDialog having a main method (as the dialog is usually run in the same JVM as the main JFrame). The only thing I can think is that the JDialog is created with a main for development purpopsed, if you want to test the dialog in stand-alone mode. But ultimately, you should delete the main method of the JDialog should you choose to use one.
Going back to the first point about multiple JFrames, other options I might recommend
Use a JDialog. Yes you will have to delete the main method, when going into production, as the dialog will be instantiated within the context of the main JVM and generally shouldn't run its own process.
Another option, depending on your requirements is to use a Cardlayout which will let you switch between views/panels (You can create JPanel forms in netbeans). See How to Use CardLayout in Netbeans GUI Builder for a working guide. And the official How to use CardLayout tutorial
An aside, if you are a beginner, I strongly suggest you put aside the builder tool and learn to hand code first. There may be many tutorials teaching you how to use the builder tool, but they may miss out on important concept in the swing architecture and swing in general. IMO this will greatly affect your understanding of how and why things work with the builder, causing a lot a headache when trying to debug. Keep Creating a GUI With JFC/Swing, the official tutorial handy and go through it.

NetBeans Swing TextArea is not static, causes trouble

I have created a JFrame with a textArea called 'outputTextArea' and I want to print the results from a database query in the textArea. However, the variable outputTextarea is not static and therefore I can't call the method setText() in the main method to print the db resultset in the textArea.
I would like to know how I can make this variable (private javax.swing.JTextArea outputTextArea;) static, because NetBeans won't let me edit this variable because it was generated by NetBeans when I dragged and dropped the textArea.
I had the same problem.
In Netbeans IDE 8.0.2:
1) In the design tab
2) Click on the textarea
3) go to properties -> code
4) Variable Modifiers -> add static.
It worked for me.
Just add an accessor method to your class that adjusts the field. For instance:
public void setTextAreaText(String newText) {
outputTextArea.setText(newText);
}
Then anyone with a reference to your class can adjust the text in the text area. Just be sure to call that method from the Event Dispatch Thread. This is usually achieved with SwingUtilities.invokeLater
SwingUtilities.invokeLater(new Runnable() {
public void run() {
myClassReference.setTextAreaText("Hello, World");
}
});
See the documentation on Event Dispatch Thread if this sort of thing is new to you. It's important to get threading correct when using Swing.
If you just want to edit codes. Open code with another editor just like notepad or something. And if you remove GEN-BEGIN:initComponents just before the auto generated code you can edit code through netbeans also.

Lag before JFrame event handlers are added?

I'm working on a simple Java swing project. This is the code of the main class (name changed):
public class MainProg
{
private static MainProg program;
//mainWin is a JFrame
private MainWindow mainWin;
//Event handler class which extends MouseAdapter
private TrayManager trayMgr;
public static void main(String[] args)
{
program = new MainProg();
}
public MainProg()
{
mainWin = new MainWindow();
trayMgr = new TrayManager();
mainWin.startBtn.addMouseListener(trayMgr);
mainWin.setVisible(true);
}
}
As is clear, when the program starts, in main() it creates a new instance of the MainProg class, which then calls the constructor. In the constructor, it creates a new instance of the JFrame mainWin. It then attaches an event handler to a button on mainWin.
In the event handler class trayMgr, the only method is mouseClicked() which does nothing
except a System.out.println('Clicked');
The issue is, when I run this program in Netbeans, the JFrame is shown right away, but I seem to have to click the button 2-3 times before the message is printed in the console.
Is this just something specific to Netbeans, or do I have to change something to make the event handler be set before the window is made visible?
Your threading issue is not likely one that is causing your current problem, but there's the theoretic potential for problems, and I've seen some real problems associated with some of the more touchy look and feels. Quite simply you should queue your code that starts your GUI onto the Swing event thread. You do this by doing:
public void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(
public void run() {
program = new MainProg();
}
));
}
Someone else recommended using invokeAndWait(...) instead of invokeLater(...) but this can be risky especially if you inadvertently make this call from within the Swing event thread itself. For your situation you're better off using invokeLater(...).
But again, I think the main problem with the code you have shown was inappropriate use of MouseListener where an ActionListener should have been used. Learning to code any GUI library can be quite tricky, and for that reason, you can't assume anything. Check out the tutorials and learn from the experts. Also if you are considering coding Swing for the long haul, consider ditching the NetBean's code-generation utilities and learn first to code Swing by hand. You won't regret doing this.
Since you asked, the code I posted here is a Java SSCCE on a different topic. invokeLater is a way of running computations on the EDT. (There is also invokeAndWait, which would work fine here, but under some other conditions can cause a deadlock.)
In fact this example is perhaps a bit over-conservative. Some references say you can run Swing from the main thread the call to show() or setVisible(). However I have a program that misbehaves under Java 7 when I try that.

doLayout() and ActionListener questions

I'm new to JSwing, so pardon me what might be some really beginners' questions.
After reading the tutorial on how to use top level containers, I tried the following code inside the actionPerformed event in a button:
private void colgarActionListener(java.awt.event.ActionEvent evt) {
auxButton = new JButton();
auxButton.setSize(100,30);
auxButton.setText("Me button");
getContentPane().add(auxButton);
getContentPane().doLayout();
}
As you expected, it occurs that it does not work. The button just does not appear. If I try a ridiculous thing such as:
getContentPane().setBackground(Color.red);
instead of
getContentPane().doLayout();
it works. What am I doing wrong?
And the last one: if I write a class which works as a custom ActionListener (with its constructor with parameters), where should I put it? As a private class inside the GUI code? It just feels so dirty... Or as a public class inside another package. maybe?
Thank you very much.
Regards.
Martín.
You will want to read up on how the layout managers work and how to use them for that is one of the keys to using Swing (not JSwing by the way). The Layout Manager Tutorial is a great place to start.
For one, avoid using null layout and setBounds(...) For another, contentPane's usually use BorderLayout. Also, I've never seen doLayout() used before in this way. Instead I've usually seen validate() or revalidate() followed by repaint() called on the container after changing its components.
Yes, an ActionListener is typically implemented as not only a private class, but an anonymous class, exactly at the use site. Anonymous class is when you write
x.addActionListener(new ActionListener() { public void actionPerformed(Event e) {
... stuff to do ...
}});

Categories