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
Related
I have the following code
package trtyhard;
import javax.swing.JFrame;
public class TrtyHard {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame("TryHard");
//frame.setSize(700, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setSize(700, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
Sometimes JFrame appears with diffirent size. Sometimes it has 10 additional mm at the bottom, sometimes 5-7 additional mm at the right side.
How can i fix it?
In addition to making sure the GUI is created on the Event Dispatch Thread EDT) try restructuring the code as follow:
JFrame frame = new JFrame("TryHard");
//frame.setSize(700, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setSize(700, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
The point is to set the frame properties before you do a setSize() or pack() on the frame.
Read the section from the Swing tutorial Initial Thread for more information about the EDT.
The JFrame class has the setBounds(x, y, width, height) method. You can try it. https://docs.oracle.com/javase/7/docs/api/java/awt/Window.html#setBounds(int,%20int,%20int,%20int)
I'm making a game and I'm trying to switch between different JPanels to display the menu, world etc.
public static void setComponent(Component component){
frame.getContentPane().removeAll();
frame.getContentPane().add(component, 0);
}
This is the method to change the component of the JFrame frame, this works for the first time with whichever JPanel component I give it, but when I try to change it from inside any class from a keyListener method say, the content pane doesn't display anything.
#Override
public void mousePressed(MouseEvent arg0) {
Frame.setComponent(new MenuPanel());
}
Method to create the JFrame and the thread that repaints the components
public static void createFrame(){
frame = new JFrame();
frame.setSize(size);
frame.setTitle(title);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setLayout(new GridLayout(1,1,0,0));
new Thread(){
public void run(){
while(true){
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(frame.getComponentCount() != 0){
//System.out.println("?");
frame.getContentPane().repaint();
System.out.println(frame.getContentPane().getComponentAt(1, 1));
}
}
}
}.start();
setComponent(new MenuPanel());
frame.setVisible(true);
}
Getting this result from the Output Stream when first loading
Menu.MenuPanel[,0,0,794x571,layout=java.awt.FlowLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=9,maximumSize=,minimumSize=,preferredSize=]
Then after using the setComponent method from another class
javax.swing.JPanel[null.contentPane,0,0,794x571,invalid,layout=java.awt.GridLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=9,maximumSize=,minimumSize=,preferredSize=]
When you call your setComponent method the first time, you do that before you call frame.setVisible(true) that forces the frame to validate all its components and show them.
When you change the components later you need to revalidate your frame manually so it gets aware of its components. Add these lines to your setComponent method and it should work.
public static void setComponent(Component component){
frame.getContentPane().removeAll();
frame.getContentPane().add(component, 0);
frame.revalidate(); // revalidate all the frame components
frame.repaint(); // and repaint the frame
}
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;
}
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);
}
});
}
I am writing an a snake game, but there's a small problem. SOMETIMES there's a little gap between the panel and the frame. I really have no idea what could be the problem as it appears so irregularly.
SSCCE:
public class Game {
static JFrame frame = new JFrame("The Snake Game");
static Game game = new Game();
JPanel cards;
static JPanel card1 = new JPanel();
private Game() {
}
public void addComponentToPane(Container pane) {
// CARD 1
card1.setLayout(null);
card1.setPreferredSize(new Dimension(600, 625));
CardLayout cl = new CardLayout();
cards = new JPanel(cl);
cards.add(card1);
pane.add(cards, BorderLayout.CENTER);
}
private static void createAndShowGUI() {
game.addComponentToPane(frame.getContentPane());
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
} // end of main
}
Try calling setResizable AFTER you call pack
For some reason, doing it the other way seems to add about 10-20 pixels to the width and height.
If that doesn't work, call setResizable AFTER the frame has being made visible
I encounter the same problem today. After a little bit of Googling, I couldn't find any good answer. Kuba gave a good hint and the problem is finally resolved. I guess this issue is caused by the delay of the setResizable(false) function and hence it happens occasionally. My solution is adding a short hold after calling setResizable.
setResizable(false);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
Okay, I know this is a disgusting way to do it. But in the question JFrame isResizable(false) sizing issue is said: "You can reset it by call JFrame#pack after the calling JFrame#setResizable". So I thought, why not reset it twice?!
Code:
private static void createAndShowGUI() {
game.addComponentToPane(frame.getContentPane());
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
if(frame.getContentPane().getWidth() > 600){
frame.pack();
}
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
So calling the method pack() for the second time when the width is greater than my preferred size seems to resolve the problem.