I am used to being able to create an instance of another class by running
Config con = new Config();
con.setVisible(true);
However, this appears not to work with the way the WindowBuilder plugin has set up the gui in Config. When the previous command is run, it creates an empty, tiny JFrame. The main method of Config contains just the following:
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Config window = new Config();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
the constructor just calls an initialize method which contains the content creation:
public Config(){
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
//other configuration here
}
How can I call Config from another class to run and be visible?
Alright, found the answer. First, to stop the tiny useless frame from being displayed, do not set visible true from the initial class. Instead just run:
Config con = new Config();
Then, in the constructor of Config, after initializing everything, add
frame.setVisible(true);
ANSWER
After create a new object of that class.
WindowShow ws = new WindowsShow();
ws.frame.setVisible(true);
And it will go perfect
Related
I am learning Java Swing. So times I see codes like following in the main:-
public static void main(String args[]) {
// create an example of the data structure
HashMap<String, String[]> attributes
= new HashMap<String, String[]> ();
attributes.put("fruits", new String[]{"apples", "peaches"});
attributes.put("drugs", new String[]{"euthanasipame", "aspirine", "analgine"});
// create a ComboFest
ComboFest fest = new ComboFest(attributes);
fest.setPreferredSize(new Dimension(400, 260));
// create a window and display the ComboFest in it
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.add(fest);
frame.pack();
java.awt.EventQueue.invokeLater(new Runnable() {
#Override public void run() { frame.setVisible(true); }
});
}
why does it need to run inside a thread by the Runnable? What is the pros and cons? Coz many times I saw people don't use it at all. Please let me know?
I'm making a java proyect using eclipse and the windowbuilder plugin. After finishing the logic I've created an Application Window and execute the project, everything is fine. But now i want to modify the window, so i change the title for example. In the visual view, i can see these changes, but when i execute the application, there are no changes, it's always the same window. This is the code:
public class Application {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Application window = new Application();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Application() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("My Application");
}
}
I've just added one line so I don't know what could be wrong. I also want to know if this is the best way to make a gui application (using Application Window) or if there are better ways.
EDIT: I add some images to show which is the problem
Window when i execute:
Window in windowbuilder:
You can set title using two ways:
1.Set title using JFrame setTitle() method : frame.setTitle("My Application");
2.Set title using JFrame(String Title) constructor: frame = new JFrame("My Application");
According to my opinion both ways work to set title but still if you are struggling with first option, you can try second option. May be this code works for you.
private void initialize() {
frame = new JFrame("My Application");
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
After setting changes, go frame.dispose()
and after that You can
frame.repaint();
frame.revalidate();
Please try this:
try {
Application window = new Application();
window.frame.setVisible(true);
window.frame.setTitle("My Application");
} catch (Exception e) {
e.printStackTrace();
And let me know the result.
Edit:
remove setTitle() from initialize method.
Edit:
If you are beginner you can try JavaFX. It is also simple as Java Swing but has some more potential, better design options and stuff. Its something I have tryed with when I was started Java learning.
I think that you should delete your swing and install it again. Because any solution is not working and there is no other reason not to. Just try to reinstall swing.
I am building a GUI using internalFrames. I have a menu with different options and I whenever I choose a new option I want it to create that specific frame. However, if that 'option frame' has been created already when I click the option a second time, then I just want that frame to come on top of the other frames (since they are the same size and will lie on top of each other).
This is some of my methods I use:
This method is created when I click a menuOption named: "Home"
protected void createFrameHome()
{
NewFrame frame = new NewFrame();
desktop.add(frame);
try {
frame.setSelected(true);
} catch (PropertyVetoException e) {
e.printStackTrace();
}
}
This Method is created when I click a menuOption named "Travel"
protected void createFrameTravel()
{
NewFrame frame = new NewFrame();
frame.getContentPane().add(table, BorderLayout.NORTH);
desktop.add(frame);
try {
frame.setSelected(true);
} catch (PropertyVetoException e) {
e.printStackTrace();
}
}
Frame create:
class NewFrame extends JInternalFrame {
public NewFrame() {
setSize(700, 500);
setVisible(true);
}
}
Now as you can see I create a new frame every time these methods are being called. I have tried different things to get it to work but none seems to work.. I have for example used a variable so it doesn't create a new frame, but then it doesn't do the frame.setSelected(true); part either.
Does anyone know a way around this? Can I somehow select the frame from before maybe?
Solved it by using a variable which was set to true if frame was created
As part of my Diploma in Software Dev we have to create a Java GUI to manage a Soccer League. My group has produced numerous JPanels that in the click-through prototype we put into JTabbedPane. This worked fine up until now where I'm moving them into separate files and into a MVC layout.
I'm using a window class to hold the top level JFrame and menubar and adding in the tabbed panes to that. This works fine in the constructor but when I remove them from the constructor and try to add them from the Bootstrap via the Window.attachTabbedPanel(String name, JPanel panel) it doesn't display but querying tabbedPane.getTabCount() display an incrementing number of tabs.
Here's a stripped back set of code:
The Bootstrap file:
public class Bootstrap {
private Window mainWindow;
public Bootstrap() {
//Window class is our containing JFrame and JMenuBar
mainWindow = new Window();
//Load up our view classes
TestTab tab1 = new TestTab();
TestTab tab2 = new TestTab();
//Attach them
mainWindow.attachTabbedPanel("Tab1", tab1.getScreen());
mainWindow.attachTabbedPanel("Tab2", tab2.getScreen());
} // Bootstrap()
public gui.Window getWindow(){
return mainWindow;
}
} // Bootstrap
This is called by the Main file:
public class Main {
public static void main(String[] args) {
Bootstrap RunMVC = new Bootstrap();
gui.Window mainWindow = RunMVC.getWindow();
mainWindow.run();
} // main()
} // Main
The problem starts here at the Window class, I've added in a In Constructor tab to check I haven't stuffed up the tabbedPane but it works fine at that point.
public class Window {
private JFrame frame;
private JMenuBar menuBarMain;
private JMenu mnFile;
private JTabbedPane tabbedPane;
private int count;
/**
* Create the application.
*/
public Window() {
//Build the frame
frame = new JFrame();
frame.setBounds(100, 100, 1280, 800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new CardLayout(0, 0));
//End Build frame
TestTab conTab = new TestTab();
//Add the tabbed pane to hold the top level screens
tabbedPane = new JTabbedPane(JTabbedPane.TOP);
frame.getContentPane().add(tabbedPane, "name_1");
tabbedPane.addTab("In Consructor", conTab.getScreen());
count = 1;
}
public void attachTabbedPanel(String name, JPanel panel){
System.out.println("Window: adding Jpanel name: "+name);
System.out.println("panel is a: "+panel);
tabbedPane.addTab(name, panel);
tabbedPane.updateUI();
System.out.println("Number of tabs: "+tabbedPane.getTabCount());
System.out.println("Last Tab .isEnabledAt() "+tabbedPane.isEnabledAt(count++));
tabbedPane.updateUI();
}
/**
* Launch the window.
*/
public void run() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Window window = new Window();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
And lastly the Panel:
public class TestTab {
private JPanel screen;
private JLabel lblSeason;
private JButton btnEdit;
private JLabel lblRounds;
public JPanel getScreen() {
return screen;
}
/**
* Initialize the contents of the frame.
*/
public TestTab() {
screen = new JPanel();
screen.setLayout(new MigLayout("", "[8%,right][10%,left][8%,right][10%,left][grow][50%]", "[][][grow]"));
lblSeason = new JLabel("Test");
screen.add(lblSeason, "flowx,cell 0 0");
btnEdit = new JButton("Edit Test");
screen.add(btnEdit, "cell 5 0,alignx right");
lblRounds = new JLabel("More Testing");
screen.add(lblRounds, "cell 0 1,alignx left");
}
}
Your error is here:
public void run() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Window window = new Window();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
You are using new instance of window instead of using created earlier, try to use this code
public void run() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
updateUI() does not do what you think it does. It should never be called directly - it is called from the superclass constructor to allow the Swing look and feel delegate to initialize itself.
Eliminating the call to updateUI() may solve your problem; or if the tabbed pane is already on screen, you may need to force a repaint/revalidate - the incantation for that is invalidate(); revalidate(); repaint();.
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