Java Swing Range Slider U.I - java

I needed a slider with two knobs on it (representing a range) and I found this nifty one here. However, they created their own U.I. which extends Java's BasicSliderUI. They override the paint method to draw their own knobs. I would like to use the default knobs based on the current look and feel. I tried calling BasicSliderUI's paintThumb method, but this gives me a generic looking knob which doesn't seem to be tied to the current look and feel. As far as I can tell, JSlider gets its U.I. from MultiSliderUI. How can I create a U.I which changes like JSlider's, but draws two knobs instead of one?
Thanks in advance!

SwingX provides JXMultiThumbSlider which may be what you are looking for.

Try getting the current look and feel from the javax.swing.UIManager and calling getUI(new JSlider()) on it. This should return the current UI used for the JSlider, which should be castable to JSliderUI.
Unfortunately JSliderUI doesn't have a paintThumb(Graphics g) method, but the BasicSliderUI does, and at least on Windows XP and Windows Vista the system look and feel is a subclass of BasicSliderUI, so is the Metal look and feel.
So once you have the JSliderUI see if it is an instance of BasicSliderUI, if so you can cast it to BasicSliderUI and use the paintThumb(Graphics g) on it, otherwise default to the way you are doing it already. I don't know if this actually works, but it would be my first attempt if I needed it.
I don't have Linux or Mac to check the source if their JSliderUIs are derived from BasicSliderUI as well, but if you do then check out the java source code.

You can use RangeSlider class from
Jide Commons Layer
This is opensource.

Related

Java/Swing Volume Slider

Part of my application has a media component, and I'm looking for a nice volume slider I can use rather than a JSlider which looks a bit ugly for this purpose (or specifically, an extended JSlider with custom visuals would be nice). I could write one, but I don't really want to reinvent the wheel.
In terms of "nice" volume sliders - I'm looking on the lines of something like VLC:
Is there a (free) component like this already out there that I'm missing?
Jasper Potts has a nice blog post about how you can skin the slider using Nimbus Look and Feel: Skinning a slider with Nimbus.
Here is how it looks like:
By following the blog post, it's not very hard to make your own custom look on the slider. You may also be interested in my answer about customizing the JScrollPane using Nimbus Look and Feel with a full code example.
Since you will want a mute operation if clicking on the speaker, I suggest that you implement your own JSlider and plug it in to the look-and-feel; however, I would also highly suggest that you reused the BoundedRangeModel that the JSlider implements.
Or, you could subclass a JPanel and package two widgets in it internally; however, this technique will require a mediator pattern to keep the two widget's displays in sync with the one shared BoundedRangeModel.
Look at the old Sun documentation about making custom widgets, and use the source code for JSlider as a starting point. It's not as hard as it may seem; however, it does take some time to get it "just right".

how do I create a Java swing UI like this?

Below UI is something I'd like to aim for. But I have no idea, how they have the "skin" of the app. On my end, the Java application looks like it was made in 1990s. I want to change a look to a more modern style.
What components are they using possibly here? JSplitpane for one. but I'm not sure how they created that "Dokument/Vorschau" tabs.
First using a look and feel like Nimbus, can easily change the look of your application to a have a more modern feel.
Second, you will need to customize the font, color, borders, node icons, etc. of each component type to achieve a non-standard look. Some of these can be changed with updates to the UIDefaults of the look and feel but many will be made by calling methods on the specific instance of the component you are dealing with.
Try a javax.swing.JTabbedPane.
If you really want to change the complete look-and-feel of your application from scratch, you should take a look at Synth L'n'F. You can define style and appearance of components and bind them to components which match certain criteria.
(My opinion on that matter: heavily themed apps usually look and feel out-of-the-place and only make it harder to use the app, so I'd actually try to avoid themeing)

look and feel in java

I work a lot with look and feel in java and it works well but the only problem that the only component that has no change is the title bar(caption) still have the same native look and feel of os(windows platform as example)
i want to know what's is the reason and how to fix this? any body help..........
thanks in advance
Check out Substance https://substance.dev.java.net/see.html
You can change the title bar look and feel with substance support.
Here are a few screenshots:
Some PL&Fs support rendering frame decorations, and some do not. I believe the Sun cross-platform PL&Fs (for instance Metal) support it, but platfrom-specific PL&F (for instance Windows) do not.
The feature is not on by default. To switch it on for all new frames use JFrame.setDefaultLookAndFeelDecorated. The API docs for the method show how to switch it on for frames individually.
If you want to create your own borderless window, instead of using a Frame/JFrame use a Window/JWindow. Frame/JFrame are extensions to Window/JWindow that provide borders and the maximize, minimize and close buttons. Usually those widgets are provided by the OS, but you can override them.
Use
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
Before any JFrame or JDialog creation.
Usually into main.

Can I use two different look and feels in the same Swing application?

I'm using the Flamingo ribbon and the Substance Office 2007 look and feel.
Of course now every control has this look and feel, even those on dialog boxes.
What I want is something like in Office 2007, where the ribbons have their Office 2007 look, but other controls keep their native Vista/XP look.
Is it possible to assign certain controls a different look and feel? Perhaps using some kind of chaining or a proxy look and feel?
I just discovered: Since Substance 5.0 the SKIN_PROPERTY is available.
It allows assigning different skins to different JRootPanes (i.e. JDialog, JFrame, JInternalFrame)
A little trick: I override JInternalFrame to remove the extra border and the title pane so that it looks just like a borderless panel. That way it is possible to create the impression, that different parts of a form/dialog have different looks.
Here is a library which will automaticaly change the look and feel. I am not sure it this will done for every component in a different way, but you should take a look at it. pbjar.org
This book should be useful if you want to go deep into look and feel /java-look-and-feel-design-guidelines-second-edition
I would be glad to see some code example, if someone can write it, feel free to get starting.
EDIT:
In this forum thread Thread i found the following description
Swing uses a Look & Feel (a PLAF).
PLAFs aren't attached on a per-JFrame
level. They are attached on a per-VM
level. It is almost impossible to mix
PLAFs within one application. I have
seen a few attempts, all failed.
Swing unfortunately does lots of "psuedo-global" things behind the scenes. AFAIK, the only way to do it consistently is to use the private AppContext API. Each AppContext has its own event dispatch thread and other "psuedo-globals".

Modifying graphics context in Java

I have a form that tries to modify a JComponent's graphics context. I use, for example,
((Graphics2D) target.getGraphics()).setStroke(new BasicStroke(5));
Now, immediately after I set the value and close the form, the change is not visible. Am I not allowed to modify a JComponent's graphics context? How else would I modify the stroke, color and transformations?
Thanks,
Vlad
There are several problems with that approach. The first is that most components will set these things themselves when ever they are asked to repaint themselves. This means that your change will be lost every time the component gets to the point where it would actually use it. But, on an even more fundamental level than that, Graphics2D objects are not persistant. They are typically instantiated every time the component is redrawn, meaning that the Graphics2D object you got won't be the same the component will be using when redrawing.
What you need to do, to achieve this kind of thing is either to reimplement the specific component yourself, or implement a new look and feel that will affect the entire set of swing components. Have a look at the following link for further details about this:
http://today.java.net/pub/a/today/2006/09/12/how-to-write-custom-look-and-feel.html
Nobody to answer? I have let some time to see if there is any good answer before mine: I am not a specialist of such question...
First, I don't fully understand your question: you change a setting then close the form?
Anyway, I am not too sure, but somewhere in the process, the graphics context might be recomputed or taken from default. Perhaps if you do this operation in the paint() method, you can get some result, although I am not sure.
For a number of changes, you usually use a decorator. I explored a bit this topic when answering a question on SO: How do I add a separator to a JComboBox in Java?. I had to paint my own border there (asymmetrical), but often you just take an existing one, so it is quite simple.
I hope I provided some information, if it didn't helped, perhaps you should give more details on what you want to do (and perhaps a simple, minimal program illustrating your problem).
OK, I've worked around the problem like this: The custom JComponent now holds a Stroke object, which is set by the "Choose stroke" form when the user clicks OK. Then, in the paint method of the JComponent, I set the stroke of the graphics context passed as parameter to paint to the one contained in the object.
I have experimented and found out that, for some reason, JComponent.getGraphics().set* doesn't work.

Categories

Resources