Java swing GUI not showing up - java

I am using Java 11 on Debian 4. I am trying to build a very basic Java GUI. To start with I have the following code:
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
public class BasicSwing extends JFrame {
JPanel p = new JPanel();
JButton b = new JButton("Hello");
public static void main (String[] args) {
new BasicSwing();
}
public BasicSwing() {
super("Basic Swing");
setSize(400,300);
setResizable(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
p.add(b);
add(p);
setVisible(true);
}
}
I have the X11 server running. The code does not fail but the GUI does not show up. I am not using Netbeans and I compile and run the code just as I would run and compile any other java code, ie with javac and java commands.The code does not stop and does not throw any error. Am I missing something very basic? I have seen a lot of discussion on the GUI not showing up but I am unable to find a solution to this problem given my specific development environment.

Instead of calling the setVisible method inside of your JFrame extended class's constructor, You should make a call on it in your main function.
Do it this way:
public static void main (String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
BasicSwing mySwingApp = new BasicSwing();
mySwingApp.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
Please read more about why I use a java.awt.EventQueue here.
Update:
It's not good practice to directly create a class that extends JFrame. Also, please read this too, for more clarification.

Related

Proper way to Create a Swing Java Application

I had some previous experience to swing applications at school, so I know how they work and will be fine designing them, but I am unsure of the best way to set my application up.
We only created smaller programs at school so we had almost all of our code in the constructor, but I want to create something larger, and am unsure of how I should set things up.
I have my base class with the constructor that will create my JFrame that I will use throughout the application, but what classes do I make to add my components, or use action listeners? I am unsure exactly of the proper way to do this, so a guiding step would be great.
Here is my class declaration/constructor:
public class SwingApp1 extends JFrame{
public SwingApp1() {
setTitle("Greens Tracker");
setSize(800,500);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
}
Now what do I put in my main, and in what manor should I be adding components?
Any help is appreciated, thanks!
Good way of setting up your JFrame is
public GUI() {
buildGUI();
}
private void buildGUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(500, 300);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new GUI().setVisible(true);
}
});
}
I hope it helped :)
The program starts at the main as:
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new SwingApp1().setVisible(true);
}
});
}
which can be abbreviated since Java 8 as:
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new SwingApp1().setVisible(true));
}
The JFrame constructor could be as follows. A setVisible I leave to the call,
and pack() will layout the contents.
public SwingApp1() {
setTitle("Greens Tracker");
setSize(800,500);
... add components, maybe in a separate method.
pack();
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
Actually look into MVC, Model View Controller.
I generally maky an XyzApplication class as controller, the JFrame being the main view and the separately kept data (in the application) the model.
It should be mentioned, that JavaFX may be slightly more advisable, say without FXML layout files. A bit more learning involved, but nicer behavior.
Other advise would be to use a build infrastructure like maven. That helps with libraries ("dependencies") and provides "best practices" like build directories (src/main/java, src/test/java, src/main/resources ...).
Of course a version control system like git or mercurial.

I'm trying to run a program in Java and I don't understand why it's not working

I am trying to run this program in Java:
import java.awt.*;
import javax.swing.*;
public class PanelTest extends JPanel
{public void paintComponent(Graphics g)
{g.setColor(Color.green);
g.drawString("Hello World",30,100);
}
}
When I run it shows a box in which it's written:
No main methods,applets or MIDlets found in file.
I don't understand what do I have to do.
Can someone explain this to me. Any help would be appreciated.Thank you!
Every Java program requires a public static void main (String[] args) as main method. This method is the program's entry point. Everything starts from there.
You can then instantiate the Panel in that main method.
Something like this:
public static void main (String[] args) {
JFrame frame = new JFrame();
frame.setContentPane(new PanelTest());
frame.setVisible();
}
As specified in the error text, the problem is that you do not have a main method for your program. All Java programs must have a main method, since that is the starting point for Java Applications.
A typical main class for your simple application would be:
public class Main {
public static void main(String args[]){
PanelTest pt = new PanelTest();
pt.paintComponent(..);
}
}
In the above, replace the '..' with the actual method arguments. I would definitely going through the basic concepts of Java. There are some great tutorials out there.
Every Java program should be started with a main method:-
public static void main(String[] args) {
// Starting code here
}

after compiling code JFrame does not appear

I'm using the Intellij idea platform.
I have the following code:
package GUI.test;
import javax.swing.*;
public class Ramka extends JFrame{
Ramka(){
setVisible(true);
setSize(100,100);
}
public void main (String[] args){
new Ramka();
}
}
I expected to see a JFrame after compiling this code, but was nothing appeared. What kind of problem can it be?
Also must admit, that I haven't possibility to run method "main". InteligIdea propose me only to compile Ramka.java. After compiling IntelijIdeay says, that compilation completed successfully, but thats all and is nothing happened. In my previous exercises I always ran method "main".
You should do something like this:
public static void main (String[] args){
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Ramka().setVisible(true);
}
});
}
Besides missing the static identifier at main, you also have to make sure that your frame runs in the right thread check "concurrency" for swing
You would see your Ramka if you actually ran main. Main should always be declared public static void main(String[] args) where you forgot the static. If you change it to:
public static void main (String[] args){
new Ramka();
}
it should run.

Trouble Adding text to JTextArea (append or settext)

So the program I am making uses 2 threads: One for the GUI and one to do the work.
I want updates from the work thread/class to print out on JTextArea in GUI class.
Everything I tried didn't seem to work. I added lines to print out text on the console right after lines to add text to the JTextArea to make sure it had got to the line but everytime console got text but no changes happened to JTextArea in the GUI.
public static void consoleText(String consoleUpdate){
GUI.console.append(consoleUpdate);
}
I tried this in the work class but nothing happened.
Anyone know how to fix my problem?
Edit:
MAIN.JAVA
public class main {
public static void main(String[] args) {
Thread t1 = new Thread(new GUI());
t1.start();
}
GUI.JAVA
public class GUI extends JFrame implements Runnable{
public static JTextArea console;
private final static String newline = "\n";
public void run(){
GUI go = new GUI();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(350, 340);
go.setVisible(true);
}
public GUI(){
setLayout(new FlowLayout());
console = new JTextArea(ConsoleContents, 15, 30);
add(console);
}
WORK.JAVA
...{
consoleText("\nI want this text on the JText Area");
}
public static void consoleText(String consoleUpdate){
GUI.console.append(consoleUpdate);
}
First, as has been said, your GUI should only run on the Event dispatch thread.
As it is written, your GUI class does two things : it's a frame, and a runnable, and both
are used completely independently. As a matter of fact, calling "run" on a your GUI object creates another, unrelated GUI object. That's probably the reason why you see nothing.
So I suggest making your main the following:
... main(...) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
GUI gui= new GUI();
gui.setVisible(true); // and other stuff
}
});
}
(I would also suggest getting rid of all "static" fields BTW. It's probably the source
of your problems, along with the weird place of the "run" method).
Now, your "consoleText" method, which I assume you call from another thread, should not
modify the text directly, but call SwingUtilities.invokeLater() to do so :
public void consoleText(final String consoleUpdate){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
console.append(consoleUpdate);
}
});
}
(the "final" declaration is important, as it allows the Runnable to use the consoleUpdate variable).

Java JFrame is very strange

When I am running this, JLabel is not visible, but when I resize window (with mouse) JLabel is showed. Why?
import javax.swing.*;
import java.awt.*;
public class FrmTaoLogin extends JFrame {
private JPanel pnlLeft = new JPanel();
public FrmTaoLogin() {
super();
pnlLeft.setBorder(BorderFactory.createEtchedBorder());
pnlLeft.add(new JLabel("test1"));
getContentPane().add(pnlLeft,BorderLayout.SOUTH);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(200, 200);
}
public static void main(String[] args) {
FrmTaoLogin FrmLogin = new FrmTaoLogin();
FrmLogin.setVisible(true);
}
}
IIRC, this happens when you don't call Frame.pack(). It should work if you call 'pack()' as the last line of the constructor.
I suspect that the problem here may have to do with trying to build and show your GUI components outside of the Swing thread.
What if you change main() to invoke your GUI code on the Swing thread, like this?
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
FrmTaoLogin FrmLogin = new FrmTaoLogin();
FrmLogin.setVisible(true);
}
});
}
This look like some of the L&F bugs in older Java VMs on newer OS. For example on Windows 7 the most problems are solved first with 1.6.0_17. You should start your program with a console. If you see some stacktraces in the event thread then it is a problem of an L&F bug.
Thanx to all, problem resolved. I change Windows theme and all working fine. I think that's Windows Aero and my NVIDIA GeForce FX5500 problem. This card official not working with windows Aero.

Categories