Execute statement/code when program runs - java

The title is a bit confusing so I'll try to explain more in here.
I'm quite new to computer programming and have been using Netbeans for my Java programming (stop laughing) and have found out that in order to execute a statement, an event has to be fired off (such as clicking a button) but how would I make it so that when the program starts, it creates an event that I could set to fire off statements/code.
Any help is much appreciated!
Edit: I don't have any code to show, but I can put it in pseudocode.
when <Program starts>
do <lblMessage.setText("Hello World");
Instead of
when <button clicked>
do <lblMessage.setText("Hello World");

Without any context, your described code works here
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JLabel lblMessage = new JLabel();
lblMessage.setText("Hello, World");
JFrame frame = new JFrame();
JPanel panel = new JPanel(new BorderLayout());
panel.add(lblMessage, BorderLayout.CENTER);
frame.add(panel);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}

Related

Text Box does not immediately pop up in JFrame [duplicate]

I have a simple Swing GUI. (and not only this, all swing GUI I have written). When run it, it doesn't show anything except blank screen, until I resize the main frame, so every components have painted again, and I can show them.
Here is my simple code :
public static void main(String[] args) {
JFrame frame = new JFrame("JScroll Pane Test");
frame.setVisible(true);
frame.setSize(new Dimension(800, 600));
JTextArea txtNotes = new JTextArea();
txtNotes.setText("Hello World");
JScrollPane scrollPane = new JScrollPane(txtNotes);
frame.add(scrollPane);
}
So, my question is : how can when I start this class, the frame will appear all components I have added, not until I resize frame.
Thanks :)
Do not add components to JFrame after the JFrame is visible (setVisible(true))
Not really good practice to call setSize() on frame rather call pack() (Causes JFrame to be sized to fit the preferred size and layouts of its subcomponents) and let LayoutManager handle the size.
Use EDT (Event-Dispatch-Thread)
call JFrame#setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) as said by #Gilbert Le Blanc (+1 to him) or else your EDT/Initial thread will remain active even after JFrame has been closed
Like so:
public static void main(String[] args) {
//Create GUI on EDT Thread
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame("JScroll Pane Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextArea txtNotes = new JTextArea();
txtNotes.setText("Hello World");
JScrollPane scrollPane = new JScrollPane(txtNotes);
frame.add(scrollPane);//add components
frame.pack();
frame.setVisible(true);//show (after adding components)
}
});
}
Your simple code is missing a few things.
You have to invoke SwingUtilities to put the Swing components on the event dispatch thread.
You should call the setDefaultCloseOperation on the JFrame.
You have to call the JFrame methods in the correct order. The setSize or pack method is called, then the setVisible method is called last.
public class SimpleFrame implements Runnable {
#Override
public void run() {
JFrame frame = new JFrame("JScroll Pane Test");
JTextArea txtNotes = new JTextArea();
txtNotes.setText("Hello World");
JScrollPane scrollPane = new JScrollPane(txtNotes);
frame.add(scrollPane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(800, 600));
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new SimpleFrame());
}
}

Not able to run full screen you tube video in DJ Native Browser

I want to run full screen you tube video in Swing Native browse, But i
am not able to run. i am not getting any exception. in my native
browser screen only black color cross button is display on the top
left corner of my jFarme. in my 32 bit windows machine its working
file. but when i try it on 64 bit windows machine its not working.
Please help me if you know something about my problem
Jar's i used :
DJNativeSwing.jar, DJNativeSwing-SWT.jar, org.eclipse.swt.win32.win32.x86_64-4.3.jar
Tryed URL's
https://www.youtube.com/v/hk5IMmEDWH4
https://www.youtube.com/v/b-cr0ewwatk?fs=1
Code
public static void main(String[] args) {
NativeInterface.open();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("YouTube Viewer");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(getBrowserPanel(), BorderLayout.CENTER);
frame.setSize(1024, 800);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
});
NativeInterface.runEventPump();
// don't forget to properly close native components
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
#Override
public void run() {
NativeInterface.close();
}
}));
}
public static JPanel getBrowserPanel() {
JPanel webBrowserPanel = new JPanel(new BorderLayout());
JWebBrowser webBrowser = new JWebBrowser();
webBrowserPanel.add(webBrowser, BorderLayout.CENTER);
webBrowser.setBarsVisible(false);
webBrowser.navigate(URL);
return webBrowserPanel;
}

Set all JLabels to opaque using look and feel

I am trying to make all JLabels opaque by default with a custom look and feel. I can set things like foreground (Label.foreground) but how do you set the opaque property?
EDIT#1:
I am creating a custom look & feel that extends MetalLookAndfeel. I override the initComponentDefaults(UIDefaults) method. However, I am open to other ways that involve using a look & feel.
EDIT#2:
I tried Vince Emigh's suggestion but it did not work. It appears that the opaque property on a JLabel has a bug - see make-jlabel-backround-transparent-again.
EDIT#3:Code sample
class Demo {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
UIManager.put("Label.opaque", Boolean.valueOf(true));
UIManager.put("Label.background", new ColorUIResource(Color.RED));
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(3);
JLabel yoyo = new JLabel("YOYOYOYO");
yoyo.setOpaque(true);// try once with this commented out and note difference
JPanel panel = new JPanel();
panel.setSize(200, 200);
panel.setBackground(Color.BLUE);
panel.add(yoyo);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
});
}
}
I also tried to set the opaque property of a JPanel - same results. It appears the setting the opaque property using UIManager has no effect on any object. :(
Is there another way to do this?
UIManager.put("Label.opaque", Boolean.valueOf(true));
It's the same as any other property
class Demo {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
UIManager.put("Label.opaque", Boolean.valueOf(true));
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(3);
JPanel panel = new JPanel();
panel.setSize(200, 200);
panel.setBackground(Color.BLUE);
panel.add(new JLabel("YOYOYOYO");
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
});
}
}

Java Swing : why must resize frame, so that can show components have added

I have a simple Swing GUI. (and not only this, all swing GUI I have written). When run it, it doesn't show anything except blank screen, until I resize the main frame, so every components have painted again, and I can show them.
Here is my simple code :
public static void main(String[] args) {
JFrame frame = new JFrame("JScroll Pane Test");
frame.setVisible(true);
frame.setSize(new Dimension(800, 600));
JTextArea txtNotes = new JTextArea();
txtNotes.setText("Hello World");
JScrollPane scrollPane = new JScrollPane(txtNotes);
frame.add(scrollPane);
}
So, my question is : how can when I start this class, the frame will appear all components I have added, not until I resize frame.
Thanks :)
Do not add components to JFrame after the JFrame is visible (setVisible(true))
Not really good practice to call setSize() on frame rather call pack() (Causes JFrame to be sized to fit the preferred size and layouts of its subcomponents) and let LayoutManager handle the size.
Use EDT (Event-Dispatch-Thread)
call JFrame#setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) as said by #Gilbert Le Blanc (+1 to him) or else your EDT/Initial thread will remain active even after JFrame has been closed
Like so:
public static void main(String[] args) {
//Create GUI on EDT Thread
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame("JScroll Pane Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextArea txtNotes = new JTextArea();
txtNotes.setText("Hello World");
JScrollPane scrollPane = new JScrollPane(txtNotes);
frame.add(scrollPane);//add components
frame.pack();
frame.setVisible(true);//show (after adding components)
}
});
}
Your simple code is missing a few things.
You have to invoke SwingUtilities to put the Swing components on the event dispatch thread.
You should call the setDefaultCloseOperation on the JFrame.
You have to call the JFrame methods in the correct order. The setSize or pack method is called, then the setVisible method is called last.
public class SimpleFrame implements Runnable {
#Override
public void run() {
JFrame frame = new JFrame("JScroll Pane Test");
JTextArea txtNotes = new JTextArea();
txtNotes.setText("Hello World");
JScrollPane scrollPane = new JScrollPane(txtNotes);
frame.add(scrollPane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(800, 600));
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new SimpleFrame());
}
}

Exporting JAR with multiple JFrames

I know how to export a Jar file and have many times before.
With my current project I was able to export and run it perfectly fine, but as soon as I added another JFrame into the project whenever I click the button to load it, the JFrame will not load and instead the JAR just freezes. Doesn't crash or anything, simply Freezes.
Is this a common problem? What can be done to fix it?
First JFrame
JFrame frame = new JFrame();
public Launcher(int id) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
frame.setUndecorated(true);
frame.setTitle("Launcher");
frame.setSize(new Dimension(width, height));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(this);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
window.setLayout(null);
Code to open second JFrame
if (Input.Clicked == 1) {
config.loadConfig("res/Config/config.xml");
frame.dispose();
new NewLauncher();
}
Code for NewLauncher()
public NewLauncher() {
Display app = new Display();
JFrame frame = new JFrame();
frame.add(app);
frame.setSize(Display.getGameWidth(), Display.getGameHeight());
frame.getContentPane();
frame.setTitle(Display.TITLE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.start();
stopMenuThread();
}
EDIT WITH EventQueue
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable()
{
public void run()
{
new Launcher(0);
}
});
//new Launcher(0);
}
Code runs fine in the debugger and compiler. Still won't run in Jar, am I doing this correctly?
Make sure you are running the UI in the Event Dispatching Thread
EventQueue.invokeLater(new Runnable() {
// Run your code here ...
});
Make sure you are not doing any time consuming tasks on the Event Dispatching Thread, including, sleeping or waiting on any locks

Categories

Resources