Keeping a Java Window always in the back - java

I wish to keep a java window (JFrame) always in the back, essentially never letting it be in focus or being on top of any other window.
I am currently using this
JFrame frame = new JFrame("test");
frame.addWindowListener(new WindowAdapter() {
#Override
public void windowActivated(WindowEvent e) {
frame.toBack();
}
});
When a window gains focus, it is sent to the back.
It works but there is a delay when a user clicks on the window where the window gets sent to the front then to the back, causing a very noticeable flicker for the user.
Is there any way I can prevent Java or Windows from sending the window to the front at all?

There is one solution I found:
JFrame frame = new JFrame("");
frame.setFocusableWindowState(false);
frame.toBack();
It only works if you use setFocusableWindowState(false) and not setFocusable(false)

My solution is, when I open a new Frame I call this method:
java.awt.Window[] windows = Window.getWindows();
for (int i = 0; i < windows.length; i++)
{
windows[i].toFront();
}

Related

Pressing twice on a Jbutton

I am a beginner in GUI programming and I am working on a project with a different kind of button.
For one of my Jbutton, when pressed it calls another frame that performs a task.
However, that frame goes on the background when I am working on the main frame.
When you press again the button for the second time a null pointer error is generated.
I want to be able to just bring back the frame that is in the background when the button is pressed for the second time.
changecontrastB.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// calls the contrast adjuster function
ContrastAdjuster mycontrast= new ContrastAdjuster();
// running that function which brings that frame forward
mycontrast.run("Brightness/Contrast...");
mycontrast.setVisible(true);
if (changecontrastB.isSelected() && mycontrast.isVisible()==false )
{
changecontrastB.setEnabled(false);
mycontrast.setVisible(true);
}
}
});
changecontrastB is my actual Jbuton.
Do the following:
In the class with the button, create a member variable of type JFrame:
JFrame frame = null;
In the action listener called from the button, include an if like this (you need to adapt it to your class names):
if (frame = null)
frame = new MyFrame(); //Other initializations might be needed too.
else
frame.toFront();

Closing a JFrame Opened in Another JFrame

I am building an application which I need to open a JFrame in another JFrame while the first JFrame is disabled.
The problem is when I want to close the second JFrame I need to enable the first JFrame.
I have tried in several ways, but it is not working properly and I was not able to reach one goal in each of them. I need to reach both goals:
Disabling the first Frame when the second Frame is opened
Enabling it when the second Frame is closed.
Here is part of my code:
private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
PaymentJFrame pjf = new PaymentJFrame();
//setEnableValue(false);
//enableFrame();
this.setEnabled(false);
pjf.setVisible(true);
if (!pjf.isActive()){
this.setEnabled(true);
}
}
This code dose not enable the First Frame at all.
I have tried to use it in another way by adding an enable when the second Frame is closed but it is not working:
//Class in first Frame
private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
PaymentJFrame pjf = new PaymentJFrame();
//setEnableValue(false);
//enableFrame();
this.setEnabled(false);
pjf.setVisible(true);
}
//Class in second Frame
private void formWindowClosed(java.awt.event.WindowEvent evt) {
// TODO add your handling code here:
FinancialDocumentsJFrame.setEnableValue(true);
}
Dose any one know how can I reach these goals?
The first Frame is the main frame and the second frame is a class that I make the frame object from it in order to show and get more information from users.
I ma using netBeans IDE designer.
First of all, a Swing application should only have one JFrame, other windows can be JDialog or somethign else. As for your question, use this code as an example. It uses a listener to detect the closing event of the second window. The following code should be in the (first) JFrame (it looks like you have a button there)
private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {
JDialog paymentDialog = new JDialog();
MyFirstFrame.this.setEnabled(false);
paymentDialog.setVisible(true);
paymentDialog.addWindowListener(new WindowAdapter() {
#Override
public void windowClosed(WindowEvent e) {
MyFirstFrame.this.setEnabled(true);
}
});
}
You can create your own dialog by extending JDialog as you did with the frames, and use the custom dialog in this code. Also instead of setting the JFrame enabled or disabled, you could consider using a modal JDialog that blocks actions to the JFrame while the dialog is active.
And further still, there is the setAlwaysOnTop(boolean) for Swing windows.
use this.dispose()method JFrame has dispose() method for JFrame Close
I decided to use jDialog as it was recommended by many people on the net.
I used a jDialog and copied all the objects I had used in the second jFrame.
It worked the way I wanted it to do.
My only problem is that why java hasn't prepared a different way to answer this need.
I'm using an action listener in the first jFrame to open the second Frame which I have used jDialog for it.
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
ActivityJDialog ajdf = new ActivityJDialog(this,true);
ajdf.setVisible(true);
}
And I have copied everything I wanted to have into this jDialog.
public class ActivityJDialog extends java.awt.Dialog {
//Document Attributes
int documentStatus = -1;
int documentType = -1;
/**
* Creates new form AcrivityJDialog
* #param parent
* #param modal
*/
public ActivityJDialog(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
/*if(false) // Full screen mode
{
// Disables decorations for this frame.
//this.setUndecorated(true);
// Puts the frame to full screen.
this.setExtendedState(this.MAXIMIZED_BOTH);
}
else // Window mode
{*/
// Size of the frame.
this.setSize(1000, 700);
// Puts frame to center of the screen.
this.setLocationRelativeTo(null);
// So that frame cannot be resizable by the user.
this.setResizable(false);
//}
}
Thank you all for helping.

JFrame.setAlwaysOnTop is not working

I have created an SSCCE to show my problem
public class TopTest {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(512, 512);
frame.setAlwaysOnTop(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
This program should create a window that is always on top, however if I run this and then click on another window behind it, the JFrame is sent behind it.
I think the problem is that you have another window that is always on top.So when you click on that this windows loose the focus and goes back.
One easy way if it is a task which needs this window to be on top and Focusable is to frequently check with a Thread maybe:
if(!frame.isFocused()) frame.setFocused(true);
Just something to help....

How do i find if a window is opened on swing

I have a problem with my application where the user will open more than one window at a time. And i have added dispose() method to call on closing the window. Now i should keep at-least one window open all the time so that the application does not hides without closed fully. If you don't understand read the following scenario:
I have window A and window B opened at the same time. Now i can close either window A or Window B but not both. In other words window B should be allowed to close only if window A is opened and vice versa. How do i do this in swing ??
A simple kind-of windowManger is not really tricky, all you need is
WindowListener which keeps tracks of the Windows it's listening to
a defined place to create the windows and register the the listener
make the windows do-nothing-on-close and make the listener responsible for the decision of whether to close or not (will do so for all except the last)
Some snippet:
// the listener (aka: WindowManager)
WindowListener l = new WindowAdapter() {
List<Window> windows = new ArrayList<Window>();
#Override
public void windowOpened(WindowEvent e) {
windows.add(e.getWindow());
}
#Override
public void windowClosing(WindowEvent e) {
if (windows.size() > 1) {
windows.remove(e.getWindow());
e.getWindow().dispose();
}
}
};
// create the first frame
JFrame frame = createFrame(l);
frame.setVisible(true);
// a method to create a new window, config and add the listener
int counter = 0;
private JFrame createFrame(final WindowListener l) {
Action action = new AbstractAction("open new frame: " + counter) {
#Override
public void actionPerformed(ActionEvent e) {
JFrame frame = createFrame(l);
frame.setVisible(true);
}
};
JFrame frame = new JFrame("someFrame " + counter++);
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.add(new JButton(action));
frame.addWindowListener(l);
frame.pack();
frame.setLocation(counter * 20, counter * 10);
return frame;
}
Just a possible approach...
Create a class, call it WindowManager, that manages creation and disposal of windows.
It could for example retain the count of the windows currently open, and allow a dispose operation only if there are more than one windows "alive", otherwise show a confirm message with JOptionPane telling the user "Really close? That would terminate the application." or something like that.
The "tricky" part is that you have to do this kind of window-related operations throughout the WindowManager, otherwise everything would screw up.
Dunno if Swing has something like this built-in, I've never seen such a scenario.
simply check if the other window is open before closing with window.isVisible();

Java: How do I close a JFrame while opening another one?

My program starts with a picture with a textfield in a JFrame. I want when the user types start it closes the picture JFrame and opens another JFrame with the main program. I've tried
processEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
on the Image frame but it closes all the windows.
The method JFrame.setVisible can be used to hide or display the JFrame based on the arguments, while JFrame.dispose will actually "destroy" the frame, by closing it and freeing up resources that it used. Here, you would call setVisible(false) on the picture frame if you intend to reopen it, or call dispose() on the picture frame if you will not be opening it again, so your program can free some memory. Then you would call setVisible(true) on the main frame to make it visible.
you also can use this code
for example
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
Maybe if you set the picture JFrame's default close operation to something besides EXIT_ON_CLOSE, perhaps DISPOSE_ON_CLOSE, you can prevent your application from closing before the second JFrame appears.
This post is a bit old but nevertheless.
If you initialize the form like that:
JFrame firstForm = new JFrame();
firstForm.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
firstForm.setSize(800, 600);
firstForm.setLocationRelativeTo(null);
firstForm.setVisible(true);
And for instance create or open another form by a button:
JFrame secondForm = new JFrame();
secondForm.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
secondForm.setSize(800, 600);
secondForm.setLocationRelativeTo(null);
secondForm.setVisible(true);
this.dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
This will dispose and destroy the first window without exiting the program.
The key is to set setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE).
It also raises the events (I've tested it with the WindowClosing event).
Here is my solution to this problem:
public void actionPerformed(ActionEvent e) {
String userName = textField.getText();
String password = textField_1.getText();
if(userName.equals("mgm") && password.equals("12345")) {
secondFrame nF = new secondFrame();
nF.setVisible(false);
dispose();
}
else
{
JOptionPane.showMessageDialog(null, " Wrong password ");
}
}
you also can use this :
opens_frame frameOld= new opens_frame();
frameOld.setVisible(true);
Closing_Frame.setVisible(false);
Closing_Frame.dispose();
private void closeTheCurrentFrameAndOpenNew(java.awt.event.ActionEvent evt){
dispose();//To close the current window
YourClassName closeCurrentWindow = new YourClassName();
closeCurrentWindow.setVisible(true);//Open the new window
}
I was searching for the same thing and found that using "this" is the best and easiest option.
you can Use the following code:
this.dispose();
For netbeans use the reference of the current Object and setVisible(false);
for example
private void submitActionPerformed(java.awt.event.ActionEvent evt)
{
// TODO add your handling code here:
this.setVisible(false);//Closing the Current frame
new login().setVisible(true);// Opening a new frame
}
First call it
new Window().nextjframe.setVisible(true);
thisjframe.setVisible(false);
if(username.equals("gaffar")&&password.equals("12345"))
{
label.setText("Be ready to continue");
//Start of 2nd jframe
NewJFrame1 n=new NewJFrame1();
n.setVisible(true);
//Stop code for ist jframe
NewJFrame m=new NewJFrame();
m.setVisible(false);
dispose();
}
This is what i came up for opening a new jframe while closing the other one:
JFrame CreateAccountGUI = new JFrame();
CreateAccountGUI.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
CreateAccountGUI.setSize(800, 600);
CreateAccountGUI.setLocationRelativeTo(null);
CreateAccountGUI.setVisible(true);
this.setVisible(false);

Categories

Resources