[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Using JSlider with font

Posted on 2005-05-16
3
Medium Priority
?
378 Views
Last Modified: 2013-12-03
I need to modify my JSlider program to increase font of my name.

Right now I have it increasing and decreasing a circle.  ie:


import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JSlider;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;

public class SliderFrame extends JFrame
{
   private JSlider diameterJSlider; // slider to select diameter
   private OvalPanel myPanel; // panel to draw circle

   // no-argument constructor
   public SliderFrame()
   {
      super( "Slider Demo" );

      myPanel = new OvalPanel(); // create panel to draw circle
      myPanel.setBackground( Color.YELLOW ); // set background to yellow

      // set up JSlider to control diameter value
      diameterJSlider =
         new JSlider( SwingConstants.HORIZONTAL, 0, 200, 10 );
      diameterJSlider.setMajorTickSpacing( 10 ); // create tick every 10
      diameterJSlider.setPaintTicks( true ); // paint ticks on slider

      // register JSlider event listener
      diameterJSlider.addChangeListener(

         new ChangeListener() // anonymous inner class
         {  
            // handle change in slider value
            public void stateChanged( ChangeEvent e )
            {
               myPanel.setDiameter( diameterJSlider.getValue() );
            } // end method stateChanged
         } // end anonymous inner class
      ); // end call to addChangeListener

      add( diameterJSlider, BorderLayout.SOUTH ); // add slider to frame
      add( myPanel, BorderLayout.CENTER ); // add panel to frame
   } // end SliderFrame constructor
} // end class SliderFrame


But I want to change the circle to be the font of my initials NKM.  Can't I just modify a couple of lines to get the same effect?
0
Comment
Question by:NikkitaKMiles
  • 2
3 Comments
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 14015443
> myPanel.setDiameter( diameterJSlider.getValue() );

use a JLabel to display your initials and change that line to

initialsLabel.setFont(initialsLabel.getFont().deriveFont((float) diameterJSlider.getValue() ));
0
 

Author Comment

by:NikkitaKMiles
ID: 14015625
OK, problem.

This places my initials in the center of the borderlayout however the JSlider is not enlarging the initials:  Where am I going wrong?

// Fig. 22.11: SliderFrame.java
// Using JSliders to size an oval.
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;

public class SliderFrame extends JFrame
{
   private JSlider diameterJSlider; // slider to select diameter
  private OvalPanel myPanel; // panel to draw circle
  private JLabel initialsLabel;
   // no-argument constructor
   public SliderFrame()
   {
      super( "Slider Demo" );

      myPanel = new OvalPanel(); // create panel to draw circle
      myPanel.setBackground( Color.YELLOW ); // set background to yellow

      // set up JSlider to control diameter value
      diameterJSlider =
         new JSlider( SwingConstants.HORIZONTAL, 0, 200, 10 );
      diameterJSlider.setMajorTickSpacing( 10 ); // create tick every 10
      diameterJSlider.setPaintTicks( true ); // paint ticks on slider
      //      set up label to display text
      initialsLabel = new JLabel( "NKM", SwingConstants.CENTER );
      initialsLabel.setFont(initialsLabel.getFont().deriveFont((float) diameterJSlider.getValue() ));

      // register JSlider event listener
      diameterJSlider.addChangeListener(

         new ChangeListener() // anonymous inner class
         {  
            // handle change in slider value
            public void stateChanged( ChangeEvent e )
            {
            myPanel.setDiameter( diameterJSlider.getValue() );
                  //
            } // end method stateChanged
         } // end anonymous inner class
      ); // end call to addChangeListener

      add( diameterJSlider, BorderLayout.SOUTH ); // add slider to frame
      add( myPanel, BorderLayout.CENTER ); // add panel to frame
       add(initialsLabel, BorderLayout.CENTER );
   } // end SliderFrame constructor
} // end class SliderFrame
0
 

Author Comment

by:NikkitaKMiles
ID: 14015640
Got it.  Nevermind.  I needed to replace the line.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Suggested Courses
Course of the Month17 days, 18 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question