Swing JFrame.setDefaultLookAndFeelDecorated vs UIManager.setLookAndFeel - java

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.

Related

Java Frame setSize, setLocation not working

These methods are not popped up while creating the object (ctrl + spacebar). Do I have to add any other package for this to work??
Do I have to add any other package for this to work?
No. They are methods inherited from parent classes that should not be used by programmers. Instead, layout managers should use them.
Edit
I'd thought this was to do with component size and location within a frame. It was only after looking more closely at the (horrible(1)) pictures of the screen I realised they were methods related to a JFrame, which changes the answer slightly.
setSize(..) should be not used, but pack() should. The former is no better than a guess, while the latter (called after components are added via layouts) will produce the correct size).
setLocation(..) is also less than optimal. setLocationRelativeTo(null) will center a packed frame on-screen, while setLocationByPlatform(true) (which I prefer) will place the app. in the position (determined by the OS) for the 'next' application (usually they will be stacked and offset slightly).
Coming back to the (1) note above:
Don't take pictures of a computer screen. All OS offer the ability to create a screenshot of either the current screen or the current app. In Windows that would be Print Screen for the entire screen or Alt + Print Screen for the focused app.
Your question is about eclipse's content assist, right?
If so, then please look at Window -> Preferences -> Java -> Appearance -> Type Filters. If there java.awt.* is selected, then methods of Component like setSize and setLocation are not shown.

Is there a way to set the default mouse cursor image for a java Swing application?

I'm working with a large java swing application. The customer wants to have a larger cursor image for the mouse because of the limitations (space) of the monitors that can be used for the application.
The way to do this for a single Swing JFrame instance is something like:
Image image = toolkit.getImage("resources/NetworkGreen48.gif");
Cursor customCursor = toolkit.createCustomCursor(image , new Point(0, 8), "img");
mainJFrame.setCursor (customCursor);
this works fine for the mainJFrame JFrame and any components embedded on the mainJFrame JFrame.
However, this application has literally hundreds of independent JFrames, JDialogs, etc. For these, running in the same JVM, but not actively added to the mainJFrame JFrame, the cursor reverts to the operating system default.
Is there a good way to set the mouse cursor for ALL the places the mouse will be used in the Swing application?
Considered doing it by talking to the OS, but, this application is running on Windows currently and will run on some version of linux in the future so I'd better not depend on the OS.
I can do this on a Frame by Frame basis. Would just like to know if there is a better way.
first part : custom cursor
I was searching to define this using look and feel but i didn't find anything about loding custom cursor inside, so there is 2 solution always use a custom sub class of JFrame where the method frameInit is override to use your cursor. Or use a factory to create all the frame you need to set the property as you do before.
Swing is not completely os independant i think that you see few rendering difference between the os you use. The only think that change is the look and feel(laf or plaf), each OS use its own plaf. but there is a cross OS Laf call Metal. Here is the documentation related to how change laf :
https://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
to be sure to be cross OS its recommanded to work using Metal but it's not required.

Java Swing Exit Icon?

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.

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