Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Setting font of JSlider

646 views
Skip to first unread message

Fred

unread,
Nov 16, 2011, 5:39:48 PM11/16/11
to
How do I set the font of a JSlider after it has been created, such
that its size is correct if it is the child of a JPanel used to
display a border ? (I use the JPanel for the border instead of
putting the border on the JPanel because using Nimbus L&F the
JSlider's border gets drawn inside the JSlider, clipping its contents)

I create a JSlider and a Hashtable for the tickmark labels, then
create the ticmark labels.

Then later I try to set the font.
If I just set the font on the jSlider, nothing happens. I have to set
the font on each of the labels for the font to change. But then the
jSlider's width is incorrect, clipping the labels.

Even if I do not use Nimbus, the problem occurs.

///// the code:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.Hashtable;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.SwingConstants;
import javax.swing.UIDefaults;
import javax.swing.border.Border;

/**
* @author flk0018
*/
public class Test1 extends JPanel {

JComponent panel;
JSlider slider;
Font f;

public Test1() {
slider = new JSlider( SwingConstants.VERTICAL );
slider.setPaintTicks( true );
slider.setPaintLabels( true );
slider.setMajorTickSpacing( 20 );
f = new Font( "Monospaced", Font.BOLD, 20 );
slider.setFont( f );

Hashtable <Integer, JLabel> labelTable = new Hashtable <Integer,
JLabel>();
String smin = "10.000";
JLabel lab;
lab = new JLabel( smin );
// lab.setFont( f ); // If done here, the size is correct
labelTable.put( 0, lab );
slider.setLabelTable( labelTable );

panel = new JPanel();
Border border = BorderFactory.createLineBorder( Color.black );
panel.setBorder( border );
panel.add( slider );
add( panel );

setComponentFont( f ); // if done here, labels are clipped
}

public void setComponentFont( Font font ) {
Dimension d = panel.getPreferredSize();

UIDefaults def = new UIDefaults();
def.put( "font", font );

//slider.setFont( font );
Dictionary < ? , ? > labs = slider.getLabelTable();
if ( labs != null ) {
for ( Enumeration < ? > keys = labs.keys();
keys.hasMoreElements(); ) {
Object key = keys.nextElement();
JComponent label = (JComponent) labs.get( key );
// label.putClientProperty( "Nimbus.Overrides",
def );
//
label.putClientProperty( "Nimbus.Overrides.InheritDefaults", false );
label.setFont( font );
label.setPreferredSize( null );
d = label.getPreferredSize();
label.setSize( d );
}
}

slider.setPreferredSize( null );
d = slider.getPreferredSize();
slider.setSize( d );

panel.setPreferredSize( null );
d = panel.getPreferredSize();
panel.setSize( d );
}

protected static void createAndShowGUI() {
Test1 t = new Test1();
JFrame frame = new JFrame( "Test1" );
frame.getContentPane().add( t );
frame.pack();
frame.setVisible( true );
}

public static void main( String[] args ) {
// try {
// for ( LookAndFeelInfo info :
UIManager.getInstalledLookAndFeels() ) {
// if ( "Nimbus".equals( info.getName() ) ) {
//
UIManager.setLookAndFeel( info.getClassName() );
// break;
// }
// }
// } catch ( Exception e ) {
// System.out.println( "No nimbus found" );
// }

javax.swing.SwingUtilities.invokeLater( new Runnable() {
public void run() {
createAndShowGUI();
}
} );
}
}

--
Fred K

markspace

unread,
Nov 16, 2011, 7:04:50 PM11/16/11
to
On 11/16/2011 2:39 PM, Fred wrote:
> How do I set the font of a JSlider after it has been created, such
> that its size is correct if it is the child of a JPanel used to
> display a border ?


Yup, it clips. After about a font size of 22, I get pretty noticeable
clipping. Also, the labels tend to display on top of each other.

Dems the breaks. Code has bugs. Submit a bug report to Oracle, and it
might be fixed in Java 8.

22 is a pretty big font size. Do you really need labels that big?

Fred

unread,
Nov 17, 2011, 11:27:07 AM11/17/11
to
No, I don't. I was just using it in this example to make it obvious
that clipping happened.
--
Fred K

markspace

unread,
Nov 17, 2011, 11:36:20 AM11/17/11
to
Just to preface: I didn't copy and run your example, I made my own from
scratch, just to make certain that there weren't any hard to spot
assumptions in the code.

My code did not exhibit any clipping at all, that I could see, until at
LEAST 22 point. It was slightly visible on the bottom (first) 0 on the
scale. At 30-50 point, it was pretty unsightly. The font mattered too,
with some "bold" fonts not clipping at all until at least 50 point or so.

If you want my code as a base, I'll post it, but it's a little
complicated. I generated most of it with a GUI tool and the tool makes
a verbose amount of code. I think it's still readable though.


Roedy Green

unread,
Nov 18, 2011, 6:24:58 AM11/18/11
to
On Wed, 16 Nov 2011 14:39:48 -0800 (PST), Fred
<fred.l.kl...@gmail.com> wrote, quoted or indirectly quoted
someone who said :

>How do I set the font of a JSlider

See http://mindprod.com/jgloss/jslider.html
http://mindprod.com/jgloss/jspinner.html

I am hazy on the details, but most of the trouble I remember came from
composite components. You have to get at the inner components of the
composite, and fiddle them individually.

The notes I wrote when the details were fresh should explain it. I
know for sure that applies to spinners, but I am not so sure it also
applies to sliders.

When I wrote my DateSpinner class, I propagated setxxx on the whole to
the relevant components. Sun should have done the same.

--
Roedy Green Canadian Mind Products
http://mindprod.com
I can't come to bed just yet. Somebody is wrong on the Internet.

Roedy Green

unread,
Nov 18, 2011, 10:29:34 AM11/18/11
to
On Fri, 18 Nov 2011 03:24:58 -0800, Roedy Green
<see_w...@mindprod.com.invalid> wrote, quoted or indirectly quoted
someone who said :

>>How do I set the font of a JSlider
>
>See http://mindprod.com/jgloss/jslider.html
>http://mindprod.com/jgloss/jspinner.html

Another thing that will drive you batty is the rendering of such
elements can change quite drastically with the L&F. In other words,
there are L&F-specific bugs. You have check out how it looks in all
the L&Fs you permit.

see http://mindprod.com/jgloss/laf.html
0 new messages