I have an application that after successfull login (on a JFrame), starts to create the main frame (class MainUI that extends from JFrame). That MainUI class contains a JTabbedPane (which each tab is a class that extends from JPanel) and, on setVisible method, creates and shows each tab.
I want to add on the login form, after successfull login, a Spinner image to indicate that the MainUI is being created.
After display the Spinner image, I invoke the creation of the MainUI and call the setVisible method on EventQueue.invokeLater(); but the Spinner image is not updated. If I use new Thread(runner).start(); is updated, but I get a lot of Component creation must be done on Event Dispatch Thread
Some code of Login.java:
buttonLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
login();
}
});
private void login()
{
//check DB and permissions
//if all is ok
lMsj.setIcon(spinner);
new Thread(new Runnable() {
public void run() {
showMainUI(usr);
}
}).start();
}
private void showMainUI(final Usuario usr)
{
Runnable runner = new Runnable() {
public void run() {
final MainUI mui = new MainUI();
mui.setVisible(true);
dispose();
}
};
EventQueue.invokeLater(runner);
}
and some code of MainUI.java
public MainUI()
{
SwingUtilities.invokeLater(new Runnable() {
#Override public void run() {
setMinimumSize(new Dimension(1280, 960));
createComponents();
}
});
}
private void initComponents()
{
//..
// menuItem = new ...
// ...
}
#Override
public void setVisible(boolean value)
{
//..
if (Security.get().isAllowed("tab1")){
addTab1();
}
//..
}
private void addTab1(){
//..
getTabbedPane().addTab("Tab1", new Tab1());
//..
}
How I can fix this, so that the image is updated and the user interface is created in the "background"?
Related
I want to initialize a Graphical User Interface (GUI) for the user to input a form. After this is accomplished i want to open a new GUI, but as soon as the first GUI pops-up the next one is initialized to.
Is there any way to solve this without using waits and notifies?
here is an example of my code:
public static void main(String[] args) {
new GUIForm();
// wait until the user inputs the complete form
new GUIWelcome();
}
It is really simple I woild like to keep it that way.
Create an Interface OnActionListener
public interface OnActionListener {
public void onAction();
}
Add these code in GUIForm class
private OnActionListener listener;
private JButton action;
public GUIForm(OnActionListener listener) {
this.listener = listener;
action = new JButton("Action");
action.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
GUIForm.this.listener.onAction();
}
});
}
Now you can achieve that
new GUIForm(new OnActionListener() {
#Override
public void onAction() {
new GUIWelcome();
}
});
You need to use some sort pub/sub mechanism. This in a nutshell is what you need:
public class PubSub {
public static void main(String[] args) {
JFrame frame1 = new JFrame("GUIForm");
frame1.setSize(640, 480);
JButton button = new JButton("User Input");
JFrame frame2 = new JFrame("Welcome");
frame2.setSize(320, 240);
button.addMouseListener(new MouseAdapter() {
#Override
public void mouseEntered(MouseEvent e) {
button.setCursor(new Cursor(Cursor.HAND_CURSOR));
}
#Override
public void mouseExited(MouseEvent e) {
button.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
#Override
public void mouseClicked(MouseEvent e) {
frame2.setVisible(true);
}
});
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.add(button);
frame1.setVisible(true);
}
}
This version uses JFrame's listeners, but you could implement your on callback mechanism to accomplish the same
i got a problem with my SplashScreen not showing up, but running in the background..
Heres my Code, taken from this tutorial (hope it ok to post the link?!?) :
SplashScreen Class:
public final class SplashScreen extends JWindow {
BorderLayout borderLayout1 = new BorderLayout();
JLabel imageLabel = new JLabel();
JPanel southPanel = new JPanel();
FlowLayout southPanelFlowLayout = new FlowLayout();
JProgressBar progressBar = new JProgressBar();
ImageIcon imageIcon;
public SplashScreen(ImageIcon imageIcon) {
this.imageIcon = imageIcon;
try {
jbInit();
}
catch(Exception ex) {
}
}
// note - this class created with JBuilder
void jbInit() throws Exception {
imageLabel.setIcon(imageIcon);
this.getContentPane().setLayout(borderLayout1);
southPanel.setLayout(southPanelFlowLayout);
southPanel.setBackground(Color.BLACK);
this.getContentPane().add(imageLabel, BorderLayout.CENTER);
this.getContentPane().add(southPanel, BorderLayout.SOUTH);
southPanel.add(progressBar, null);
this.pack();
}
public void setProgressMax(int maxProgress)
{
progressBar.setMaximum(maxProgress);
}
public void setProgress(int progress)
{
final int theProgress = progress;
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
progressBar.setValue(theProgress);
}
});
}
public void setProgress(String message, int progress)
{
final int theProgress = progress;
final String theMessage = message;
setProgress(progress);
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
progressBar.setValue(theProgress);
setMessage(theMessage);
}
});
}
public void setScreenVisible(boolean b)
{
final boolean boo = b;
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
setVisible(boo);
}
});
}
private void setMessage(String message)
{
if (message==null)
{
message = "";
progressBar.setStringPainted(false);
}
else
{
progressBar.setStringPainted(true);
}
progressBar.setString(message);
}
}
In my Main() I start the SplashScren like this:
SplashScreen initscreen;
ImageIcon myImage = new ImageIcon(FilterConfiguratorJFrame.class.getResource("/images/logo.png"));
initscreen = new SplashScreen(myImage);
initscreen.setLocationRelativeTo(null);
initscreen.setProgressMax(100);
initscreen.setScreenVisible(true);
Then I set the progress like this:
initscreen.setProgress("...loading Projects" , 40);
Then my program is loading Projects via a VPN from a Server at work.
Till here i dont get any errors, but the SplashScreen isn't showing up...
I have a OptionPane showing up when theres no connection to the server, and when THIS OptionPane is showing up the SplashScreen does to. When i click "OK" on the OptionPane it disappears, and so does the SplashScreen...
I tried it, and the SplashScreen shows up no matter when or where i let an OptionPane show...
So my Question is, how do i force the SplashScreen into the foreground?
I want the SplashScreen to show up on startup and updating its progressbar while different data ist loaded from the Database (which can last 1-2 minutes).
EDIT: Tried to force the SplashScreen to show by showinf a JFrame, doesn' work, seems like ist only working with OptionPanes.
I have 2 classes .java
The main :
public class Controller extends javax.swing.JFrame
{
public static void updateProgressBar(int i) {
jProgressBar1.setValue(i);
jProgressBar1.repaint();
}
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
Controller app = new Controller();
app.setVisible(true);
app.setResizable(false);
}
});
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
ChildModel model = new ChildModel();
Thread t1 = new Thread(model);
t1.start();
}
private javax.swing.JProgressBar jProgressBar1; //Initialized with Netbeans builder
}
My ChildModel (ChildModel.java) computes some code (that takes around 10-20 sec) and I want to show the progress on the father class (Controller.java).
Here is my ChildModel :
public class ChildModel implements Runnable
{
public ChildModel(){ /* Something */ }
public void complexMath()
{
//Lots of logic here
Controller.updateProgression(purcent);
}
#Override
public void run() {
complexMath();
}
}
The problem is obviously my static void updateProgressBar that cannot modify a non-static variable. How can I accomplish this ?
The jProgressBar1 variable is an instance variable, so you can't access it from a static method. And the method shouldn't be static: you want to update the progress in the controller, and not in all the Controller instances.
Pass a reference to the controller to the ChildModel, and use this reference from the ChildModel in order to update the progress bar. Also remember that all Swing interactions must be done in the EDT, and not in a background thread. SO the code should look like this:
public class Controller extends javax.swing.JFrame
{
public void updateProgressBar(int i) {
jProgressBar1.setValue(i);
// no need for repaint. The progress bar knows it must be repainted
// when its value changes
}
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
Controller app = new Controller();
app.setVisible(true);
app.setResizable(false);
}
});
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
ChildModel model = new ChildModel(this);
Thread t1 = new Thread(model);
t1.start();
}
private javax.swing.JProgressBar jProgressBar1; //Initialized with Netbeans builder
}
public class ChildModel implements Runnable
{
private Controller controller;
public ChildModel(Controller controller){
this.controller = controller;
}
public void complexMath()
{
//Lots of logic here
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
controller.updateProgression(percent);
}
});
}
#Override
public void run() {
complexMath();
}
}
Swing has its own concurrency mechanisms to deal with updating components. Here you could use
a Swing Timer and update the JProgressBar. Rather than have ChildModel implement Runnable, you could use a Timer as a class member variable and pass in your instance jProgressBar1, enabling you to call setValue when required.
How can i exit only they new MainGame that i created from Main?
Where Main is having an original layer of game. And the MainGame was a dialog window (such as modal windows).
Main.java: (main code)
public class Main extends JWindow
{
private static JWindow j;
public static MainGame mp;
public static void main(String[] args)
{
new Thread(new Runnable()
{
public void run()
{
mp = new MainGame();
mp.runit();
//mp.stopit();
}
}).start();
j = new Main();
j.setVisible(true);
}
}
MainGame.java: (this was extended by Main, and i would like to quite this only).
public class MainGame extends JWindow
{
private static JWindow j;
public MainGame()
{
// some GUI ...
}
public static void runit()
{
j = new MainGame();
j.setVisible();
}
}
1) better would be implements CardLayout, as create Top-Level Container for new Window, then you'll only to switch betweens Cards
2) don't create lots of Top-Level Container on Runtime, because there are still in JVM memory untill current instance exist,
create required number of and re-use that, to avoiding possible memory lacks
then you have to call setVisible(false) and setVisible(true)
JWindow missed methods for setting setDefaultCloseOperation(Whatever);
3) if you'll create constructor public JWindow(Frame owner), then you'll call directly
SwingUtilities.getAccessibleChildrenCount() and SwingUtilities.getWindowAncestor()
import javax.swing.*;
import java.awt.*;
public class Testing {
private JFrame f = new JFrame("Main Frame");
private JWindow splashScreen = new JWindow();
public Testing() {
splashScreen = new JWindow(f);
splashScreen.getContentPane().setLayout(new GridBagLayout());
JLabel label = new JLabel("Splash Screen");
label.setFont(label.getFont().deriveFont(96f));
splashScreen.getContentPane().add(label, new GridBagConstraints());
splashScreen.pack();
splashScreen.setLocationRelativeTo(null);
splashScreen.setVisible(true);
new Thread(new Runnable() {
#Override
public void run() {
readDatabase();
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
createAndShowGUI();
}
});
}
}).start();
}
public void readDatabase() {
//simulate time to read/load data - 10 seconds?
try {
Thread.sleep(2000);
} catch (Exception e) {
e.printStackTrace();
}
}
public void createAndShowGUI() {
JLabel label = new JLabel("My Frame");
label.setFont(label.getFont().deriveFont(96f));
f.add(label);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
System.out.println("JFrame getAccessibleChildrenCount count -> "
+ SwingUtilities.getAccessibleChildrenCount(f));
System.out.println("JWindow getParent -> "
+ SwingUtilities.getWindowAncestor(splashScreen));
splashScreen.dispose();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
Testing t = new Testing();
}
});
}
}
I did not go really into your design. but there is 'j.dispose();'.
this should work. here is the java documentation.
notice:
dispose(); - deletes the window from memory.
setVisibilty(false); - just hides it from the screen.
You can override the 'dispose()' function to do some stuff while the widow is closing (updating scores if its a game) but at the end of the overriden function you have to call 'super.dispose();' so the function of the class Window is called.
And the MainGame was a dialog window
But thats not what your code uses. You use a JWindow.
You should be using a JDialog for a modal window. Then you just dispose() the window.
I am trying to catch an event of user clicking on and "X" button of a JDialog and only close if a user confirms. So here is skeleton of what I am doing:
public class MyDialog extends JDialog {
public MyDialog(){
super();
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
.........
}
.........
}
public class Waiter implements WindowStateListener{
#Override
public void windowStateChanged(WindowEvent event) {
System.out.println(event);
if (event.getNewState() == WindowEvent.WINDOW_CLOSING) {
if (shouldClose()) {
dialog.close();
}
}
}
}
MyDialog dialog = new MyDialog();
Waiter waiter = new Waiter();
dialog.addWindowStateListener(waiter);
As you can guess, when I click on "X" for the dialog, I do not get a message printed becasue the methodis never called. I am not sure where is the problem.
You want to use a WindowListener instead of a WindowStateListener.
Try this:
MyDialog dialog = new MyDialog();
dialog.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(final WindowEvent event) {
System.out.println(event);
if (shouldClose()) {
dialog.close();
}
}
});