Java Swing Exit Icon? - java

I was wondering how to change the minimize, maximize, and exit button icons in the top left of the default JFrame swing window? I've looked everywhere and can't find a tutorial or method that does it.

Minimize, maximize/restore and close buttons are bound to the frame decoration used.
If you are using a frame decorated by system (for example some Aero frame in Windows 7) - you cannot modify those buttons since the whole frame decoration is provided by system and there is no good way to invtervene and change its behavior.
On the other hand - if you are using a custom Look and Feel (shorty - L&F) written on Java and it provides its own frame decoration - it is possible to modify it if you have access to that L&F sources or if that L&F provides some options to add/remove those buttons.
You can read more about L&F here:
http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel
You can also find a lot of links to custom L&Fs here:
Java Look and Feel (L&F)

Start with JFrame.setDefaultLookAndFeelDecorated(true).
Then set the Pluggable Look and Feel.
For best results, this is most reliably done before the first GUI element is visible on screen.
See How to Make Frames - Specifying Window Decorations for more info.

As far as I know, you cannot, but if this is something you need to have done, you can use Netbeans to use JavaFX, which allows you to fully customize the window, including the buttons like that. The window doesnt even need to have these buttons.

Related

Swing JFrame.setDefaultLookAndFeelDecorated vs UIManager.setLookAndFeel

In Swing, it appears there are two ways of setting the theme/look-and-feel of the application:
JFrame.setDefaultLookAndFeelDecorated, and
UIManager.setLookAndFeel
I'm wondering what the difference is between these is and when/why to use either of them.
Only the UIManager.setLookAndFeel(...) methods allow setting the look and feel of the application. This is the method you want to use to set a specific look and feel to your application.
Using the static JFrame.setDefaultLookAndFeelDecorated(...) will only affect how newly created JFrames will have their windows "decorated" (i.e. window title bar, close/minimize button etc). Either by the LookAndFeel or by the system (or the "window manager"). It does not affect the look and feel of the application in general.
From the docs:
If defaultLookAndFeelDecorated is true, the current LookAndFeel supports providing window decorations, and the current window manager supports undecorated windows, then newly created JFrames will have their Window decorations provided by the current LookAndFeel. Otherwise, newly created JFrames will have their Window decorations provided by the current window manager.
Personally, I rarely find good use for the latter method.

How to make the desktop pane that Netbeans has

So when I start up Netbeans, they create a little panel on the desktop for showing the progress of loading. I'm pretty sure Microsoft Office 2010 uses this too. I was curious how to make one of those in java?
I looked through the API and saw JDesktopPane. But I don't think that's what I'm looking for unless you can take that and put in on the actual desktop, but I'm unsure. Thanks in advance!
you can actually do it using JPanel.. you don't need to do anything else..no need of desktop pane
all you need to do is design a JPanel and put a progress bar inside it that will link to a process and show how much it has been completed.
JPanel doesn't have normal frame functionality like minimize, Close etc and will act exactly as you are trying to make up.
Update : Just tried doing what you wanted.
You need to start working on JFrame. and set its decoration to false.
in Netbeans, you can just go to Frame properties and set Undecorated to true
or inside code you can just write setUndecorated(true);
then you have to design your frame, put a progress bar inside it, link it to a function, set its onTop value to true (which means it will always be on top) and set its position to center of screen. done!! you are ready with your window!!

Small Window Over Components

I already look at java library and dont know what to use to do this..
I already tryed JInternalFrame but thats not what I really want.. because it needs to be added to a JDesktopPanel right??
And in my program I have a JFrame with content pane using BorderLayout..
Then on borderlayout center I have a JTextArea, on borderlayout east I have a list.. and on borderlayout south I have a JPanel..
What I want is, when I do a certain action, it will pop up a "mini window" where I need to choose something.. u see?
and if I create JDesktopLane it will overlap what I have on the container..
the window will be made by my like a color chooser pallete , like a grid with colors.. and a label on top saying some text..
I just dont know how to make a "window" over the other components, and users can still drag over the frame, and interact with all the other components.. the jtextarea and such..
I guess you understood, thanks alot in advance!!
If u dont understand something please tell me, I really want to do this :)
Just dont know what to use..
Thanks again ;)
Have you tried JDialog?
It's because Jdialog are not component to be add in a JFrame, it's an independant thing running on it's own
if you use JDialog, the construct parameter parent indicate wich frame the JDialog is related to.
The typical class for this task is JWindow, a borderless top-level window that can be freely positioned. You could use SwingUtilities.getPointFromComponent to get the screen coordinates for a realized coordinate.
Top-level windows (JFrame, JDialog, JWindow) are not added to containers. They can get other windows as parent.
I dont want to use another JFrame.. that is kinda bad for code, its a small window with a simple function..
Structure your code so you can read it, others can read it, and you can debug it easily (the latter is a result from the first). A low class count is useless and -most of the time- contraproductive.
Why should another JFrame (or other window) be bad?
If you absolutely want to avoid opening top level windows (e.g. to avoid applet warning icons or to implement a special kind of user interface) you could use a JLayeredPane to add additional JPanels above your existing GUI elements.

JDesktopPane - how to get active frame

How to get active (having focus) frame (JInternalFrame) that is inside JDesktopPane? I need it for my MDI notepad (not that anybody would use that, just a training project). Looking at api, I see only functions to get all JInternalFrames, not active one.
Use JDekstopPane.getSelectedFrame() method (From doc: currently active JInternalFrame in this JDesktopPane, or null if no JInternalFrame is currently active.) or JDesktopPane.getAllFrames() to get list of all JInternalFrames currently displayed in the desktop and check isSelected() method.
Make a List<JInternalFrame> and check isSelected() as you iterate though it.
Addendum: See also this example that uses Action to select an internal frame from a menu.
Have you looked at the Java tutorial titled How to Use Internal Frames? In your code you need an InternalFrameListener (API) (Tutorial) and listen to activate/deactivate events. Activated means the internal frame was brought to the top; deactivated means it's no longer on top. Since JDesktopPane extends JLayeredPane you can also set the z-order of components added to it.
Don't iterate over all the panes - use events.
If for some reason you prefer to poll your UI rather than use an event-driven approach you can call getSelectedFrame which returns the active JInternalFrame. I'm not sure why no one else mentioned it.

Custom Java Window Title Bar Menu

I'm trying to allow the user to change the title of a window in Java without adding components to the window itself. I'm actually trying this with a JInternalFrame, but figure the solution should be similar for a JFrame. I simply want to add an additional menu item in the context menu that pops up when right clicking on a window title bar. For example, the Set title below:
This example is on Windows XP, but perhaps there's a way to get the window context menu OS independently perhaps similar to the SystemTray.getSystemTray() (but for individual windows within an application). From this I would be able to provide my own ActionListener to popup a dialog for the user to enter a new title.
Is this a much bigger task than I'm guessing it is? Does anyone have solutions they've used before?
Short answer: I don't think this is easy. I'm not 100% sure if it is possible.
First, JFrame and JInternalFrame are actually quite different. JFrame is a top level component whose title bar and such are typically provided by the OS. JInternalFrame's entire content (including title bar) is provided by the Swing LAF.
For a JInternalFrame, the context menu is provided by the LAF, not JInternalFrame itself. You would have to do something with the UIComponent in order to change the context menu. I think you would likely have to provide a custom UI component in order to do this, which is ugly and typically breaks across different LAFs or works but looks terrible at best. See BasicInternalFrameTitlePane, the createSystemMenu method.
I don't think this is possible without digging way too deep into Swing's internal UI system and I wouldn't even consider doing this. Why don't you use the inbuilt JMenuBar of JInternalFrame?
myInternalFrame.setJMenuBar(myMenuBar);

Categories

Resources