Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #4682

Re: adding border to JSlider using Nimbus

From "John B. Matthews" <nospam@nospam.invalid>
Newsgroups comp.lang.java.gui
Subject Re: adding border to JSlider using Nimbus
Date 2011-09-08 19:09 -0400
Organization The Wasteland
Message-ID <nospam-B2F053.19093808092011@news.aioe.org> (permalink)
References <daabdcd3-7d35-400f-9235-edf9539fef75@t30g2000prm.googlegroups.com>

Show all headers | View raw


In article 
<daabdcd3-7d35-400f-9235-edf9539fef75@t30g2000prm.googlegroups.com>,
 Fred <fred.l.kleinschmidt@boeing.com> wrote:

> I create a vertical JSlider with ticks and labels painted.
> Its width is x pixels (x depends on the font, etc.)
> 
> I then add a border using:
>   mySlider.setBorder( BorderFactory.createLineBorder( Color.black,
> 10 ) );
> 
> With Metal L&F, the internal part ofstays the same width, with
> a 10-pixel wide border around it; the component's width is now
> (x+20) pixels. This is what I would expect - the component
> wants to be x pixels wide (to display the slider and the scales)
> plus 10 pixelsl on each side for the border.
> 
> Using Nimbus, however, the component stays the same width,
> with the border inside it, clipping 10 pixels off each edge.
> 
> How can I add the border correctly using Nimbus?
> 
> Here's the code:
> 
> import java.awt.Color;
> import java.awt.FlowLayout;
> 
> import javax.swing.BorderFactory;
> import javax.swing.JFrame;
> import javax.swing.JPanel;
> import javax.swing.JSlider;
> import javax.swing.UIManager;
> import javax.swing.border.Border;
> 
> public class Slide extends JPanel {
>    public Slide() {
>       try {
>          UIManager.setLookAndFeel(
>            "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel" );
>       } catch ( Exception e ) {
>          System.out.println( "No nimbus!" );
>       }
> 
>       JSlider js = new JSlider();
>       js.setPaintTicks( true );
>       js.setPaintLabels( true );
>       Border b = BorderFactory.createLineBorder( Color.black, 10 );
>       js.setBorder( b );
>       js.setOrientation( 1 );
>       int imin = 0;
>       int imax = 200;
>       js.setMinimum( imin );
>       js.setMaximum( imax );
>       js.setMajorTickSpacing( ( imax - imin ) / 5 );
> 
>       setLayout( new FlowLayout() );
>       add( js );
>    }
> 
>    public static void main( String[] args ) {
>       JFrame f = new JFrame( "Slider" );
>       Slide mll = new Slide();
>       f.getContentPane().add( mll );
>       f.pack();
>       f.setVisible( true );
>    }
> }

"...the look and feel implementation of many standard Swing components 
doesn't work well with user-set border...we recommend that you put the 
component in a JPanel and set the border on the JPanel."

<http://download.oracle.com/javase/7/docs/api/javax/swing/JComponent.html#setBorder(javax.swing.border.Border)>

Also, Swing GUI objects should be constructed on the event dispatch 
thread.

<http://download.oracle.com/javase/tutorial/uiswing/concurrency/initial.html>

-- 
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

Back to comp.lang.java.gui | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

adding border to JSlider using Nimbus Fred <fred.l.kleinschmidt@boeing.com> - 2011-09-08 11:57 -0700
  Re: adding border to JSlider using Nimbus Tom <tom400f@gmail.com> - 2011-09-08 22:57 +0000
  Re: adding border to JSlider using Nimbus "John B. Matthews" <nospam@nospam.invalid> - 2011-09-08 19:09 -0400
  Re: adding border to JSlider using Nimbus Roedy Green <see_website@mindprod.com.invalid> - 2011-09-16 16:45 -0700
    Re: adding border to JSlider using Nimbus "John B. Matthews" <nospam@nospam.invalid> - 2011-09-16 23:19 -0400

csiph-web