Internalframe size is not always maximized ( alternating) - java

I am facing a weird issue that I can't see to understand why. I have a frame with an Internal JFrame that opens upon a menu click. This internal frame is supposed to be always maximized. When I run the program and click on the menu then it does what is expected. If I close the internal jframe and click the button again, the internal jframe is minimized. If I close it and click the button then it is maximimzed!! Why is it alternating like that. Here is the code that open the internal jframe. This method is inside the JFrame class
private void onButtonClick(){
InternalFrameProp intFrame = new InternalFrameProp ();
intFrame.setVisible(true);
jDesktopPane1.add(intFrame );
try {
intFrame .setMaximum(true);
} catch (PropertyVetoException ex) {
Logger.getLogger(MainHomePage.class.getName()).log(Level.SEVERE, null, ex);
}
}

As I was trying to put a runnable example, I noticed that this behaviour only happen in the Windows look and feel. To get around I did
jDesktopPane1.setDesktopManager(new DefaultDesktopManager());
That seemed to fix the problem! I have no idea why but it worked!

Related

JFrame keeps application running even after closing

I have a class with only static methods and one of them opens a JOptionPane error message dialogue using a JFrame object as component.
This is the class + method:
public class miscMethods
{
static JFrame errorWindow = null;
public static void ErrorPopup(String message)
{
errorWindow = new JFrame();
errorWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
errorWindow.setAlwaysOnTop(true);
JOptionPane.showMessageDialog(errorWindow, message, "Error", JOptionPane.ERROR_MESSAGE);
errorWindow = null;
}
}
The ErrorPopup method is used inside a JavaFX controller and other places, called like this:
import static code.miscMethods.ErrorPopup;
...
ErrorPopup("This is the error message");
Problem is that the application's process won't close when I close the the program from the window's ✕ after the popup appears, because the JFrame was created and shown.
I know the JFrame is the culprit, so I added the errorWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
but it doesn't seem to do anything, since the program isn't closing.
In this question: JFrame and why stay running
The accepted answer talks about non-daemon threads, but the only thread I open is a daemon one, so unless JavaFX open one then it can't be that I believe.
So, why does the process keep running and how can I solve it?
I'm still new to Java so if I made a mistake and/or my code shows bad practices please do point them out!
Edit: I'm using a JFrame because I need the setAlwaysOnTop, since using
JOptionPane.showMessageDialog(null, message, "Error", JOptionPane.ERROR_MESSAGE); opens it not on top of the JavaFX window. If there's a better way let me know.
This:
errorWindow = null;
does nothing of use since the object is still displayed. You want this instead:
errorWindow.dispose();
Actually, even better, simply get rid of errorWindow altogether and pass null as the first parameter to the JOptionPane.

Setting a JFrame without overlapping with taskbar

I need to have a undecorated JFrame(setUndecorated(true)) which need to be shown fullscreen, without overlapping with the taskbar.
I have tried the below solutions.
Calling setExtendedState(MAXIMIZED_BOTH).
Advantage:
This works fine as expected, i.e., the window is getting adjusted itself dynamically, except it has the below issues.
Issues
Initially the window occupies the fullscreen
Though the frame get adjusted itself dynamically, it overlaps with the taskbar.
Tried the below solution as stated in Does JFrame.setExtendedState(MAXIMIZED_BOTH) work with undecorated frames?
GraphicsConfiguration config = aFrame.getGraphicsConfiguration();
Rectangle usableBounds = SunGraphicsEnvironment.getUsableBounds(config.getDevice());
aFrame.setBounds(0, 0, usableBounds.width, usableBounds.height);
Advantage:
I am not getting overlaps and window looks fine.
Issue:
Window is not adjusting itself dynamically when the taskbar position/size is changed.
Any help would be greatly appreciated.
I thought of a design. But not sure about its feasibility. I can use the setBounds(). But then I need my frame to be notified when the task bar is adjusted or repositioned. Is there a way?
Able to able to fix the above issue with the below code,
Rectangle usableBounds = SunGraphicsEnvironment.getUsableBounds(config.getDevice());
setMaximizedBounds(usableBounds);
setExtendedState(MAXIMIZED_BOTH);
So by getUsableBounds I am able to get the bounds leaving the taskbar. And hence I am using setExtendedState(MAXIMIZED_BOTH) the window is getting updated automatically when I re-size/re-position the taskbar. :-)
final Point x = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
Have a separate thread to check whether taskbar get changed. If so update size
new Thread(new Runnable() {
#Override
public void run() {
if (x.equals(GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint())) {
Rectangle r = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
setSize(r.getSize());
}
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
}).start();

Display only one Shell if the shell if previously open not display another shell

I am developing a rcp application .I am using a Novocode swt balloon window . I need to display one BaloonWindow on button click.but whenever I click on button each time create a new balloon window
My code is below
public Object execute(ExecutionEvent event) throws ExecutionException {
try {
BalloonWindow baloonWindow=new BalloonWindow(HandlerUtil.getActiveWorkbenchWindow(event).getShell(),SWT.ON_TOP|SWT.TOOL|SWT.CLOSE);
baloonWindow.setText("XYZ");
baloonWindow.setAnchor(SWT.RIGHT|SWT.TOP);
baloonWindow.setLocation(1290, 90);
Composite c = baloonWindow.getContents();
String array[]=new String[2];
array[0]="A";
array[1]="B";
c.setLayout(new FillLayout());
TableViewer t=new TableViewer(c,SWT.NONE);
t.setContentProvider(new ArrayContentProvider());
t.setInput(array);
c.pack(true);
baloonWindow.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
anybody can help me.how to show only one balloon window at time.if a balloon window is open then another balloon window should not be allowed to open or there should remain only one balloon window open at any given point of time.
I'm not quite sure I understood you ultimate goal, so here are two possibilities:
First (at most one BalloonWindow at a time)
Create a static boolean field isOpen in your class containing the execute() method. Set this variable to true once you created the BalloonWindow and check this variable each time you enter execute(). If it is false, create a new BalloonWindow, if it is true, return.
Second (close the BalloonWindow)
The BalloonWindow has a method open(). Use this method to open it instead of setVisible(true). If you want to close the BalloonWindow, just call close(). setVisible(false) would have the same visual effect (the window is gone), but it would still be there (only invisible). close really closes the window.

How to close JFrame?

I have the main application frame. If user clicks the button he gets a new frame which has some chart in it. Now if I want to close that chart both chart and main application closes. How can I distinguish those two closings. Certainly I don't want my application to be closed after closing the frame in which I put chart.
Here's the code of the chart frame.
chartBttn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
final ShowChart sc = new ShowChart("Reserve Selection", getUtilExperiments() );
sc.pack();
RefineryUtilities.centerFrameOnScreen(sc);
sc.setVisible(true);
sc.setDefaultCloseOperation(ShowChart.DISPOSE_ON_CLOSE);
}
});
You can use the dispose() method. Or you can call setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); on the JFrame.
If I understood you correctly on your new JFrame build a method for your close button or X window button with:
setVisible(false);
dispose();
Otherwise please post your code on creating the new JFrame etc.
Certainly I don't want my application to be closed after closing the frame in which I put chart.
1) don't create lots of JFrames on the fly, create JFrame only once and re-use that for next usage(s), then
call only for visibility setVisible(false/true) with setDefaultCloseOperation(JFrame.NOTHING_ON_CLOSE)
or very simple workaround
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE)
2) or JDialog with setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE)
3) NOTICE: but in this case isn't possible close the current JVM instance, you have to add JButton or JMenu/JMenuItem which accelerate for System.exit(1)
simply use super.dispose(); for closing previous jframe

JOptionPane won't show its dialog on top of other windows

I have problem currently for my swing reminder application, which able to minimize to tray on close. My problem here is, I need JOptionPane dialog to pop up on time according to what I set, but problem here is, when I minimize it, the dialog will pop up, but not in the top of windows when other application like explorer, firefox is running, anyone know how to pop up the dialog box on top of windows no matter what application is running?
Create an empty respectively dummy JFrame, set it always on top and use it as the component for the JOptionPane instead of null. So the JOptionPane remains always on top over all other windows of an application. You can also determine where the JOptionPane appears on screen with the location of the dummy JFrame.
JFrame frmOpt; //dummy JFrame
private void question() {
if (frmOpt == null) {
frmOpt = new JFrame();
}
frmOpt.setVisible(true);
frmOpt.setLocation(100, 100);
frmOpt.setAlwaysOnTop(true);
String[] options = {"delete", "hide", "break"};
int response = JOptionPane.showOptionDialog(frmOpt, msg, title, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, "delete");
if (response == JOptionPane.YES_OPTION) {
removeRow();
}
frmOpt.dispose();
}
Old post, but I was struggling with this.
My problem was more with Javafx allowing the JOptionPane to go behind the current Java window.
Therefore I used the following which does what the original poster asked by putting the JOptionPane in front of all windows; even JAVAFX.
Firstly the old JOptionPane:
JOptionPane.showMessageDialog(null, "Here I am");
Now an JOptionPane that stays in front:
final JDialog dialog = new JDialog();
dialog.setAlwaysOnTop(true);
JOptionPane.showMessageDialog(dialog, "Here I am");
And for fun here is everything in one long line:
JOptionPane.showMessageDialog(
((Supplier<JDialog>) () -> {final JDialog dialog = new JDialog(); dialog.setAlwaysOnTop(true); return dialog;}).get()
, "Here I am");
You can make a static method some where that will return the JDialog for you and then just call it in the JOptionPane to clean up your code a bit.
Are you using one of the canned JOptionPanes? (Like JOptionPane.showCOnfirmDialog(...))
You may want to look at extending JDialog and making your own dialog panel, and then calling myDialog.setAlwaysOnTop(true);
Windows is blocking this operation since XP.
The scenario before was like:
Your a tiping in some text in an editor and not recognize that another dialog is coming to front when you are tipping the text. The coming dialog gets the focus and you are tiping in the new dialog. Maybe you click enter after you are ready and do this in the wrong dialog, which is asking whether you realy want to delet your hard disk ;)
The come to front call in java is only working for java windows.
The possibibilty to notify the user of a new window is to implement a Frame, which will highlighted/flashing in the windows task bar.
Correction the post above..
I have resolve my problem as below:
this.setVisible(true); // show main frame
MyDialog dialog = New MyDialog(this, true); // show my custom dialog
dialog.setVisible(true);
this.setVisible(false);
it works fine for me :)
You might think about using a JFrame instead. It may give you a little more flexibility.
If you are using a JFrame and you want it to popup on top of the other windows use:
myFrame.setVisible(true);
myFrame.setState(Frame.NORMAL);
The setState will show the window to the user if it was in minimized state previously.

Categories

Resources