Skip to main content

Java Swing: JColorChooser, JTablePane, JSlider & JSpiner

Java JColorChooser

The JColorChooser class is used to create a color chooser dialog box so that user can select any color. It inherits JComponent class.

JColorChooser class declaration

Let's see the declaration for javax.swing.JColorChooser class.

  1. public class JColorChooser extends JComponent implements Accessible  

Commonly used Constructors:

ConstructorDescription
JColorChooser()It is used to create a color chooser panel with white color initially.
JColorChooser(color initialcolor)It is used to create a color chooser panel with the specified color initially.

Commonly used Methods:

MethodDescription
void addChooserPanel(AbstractColorChooserPanel panel)It is used to add a color chooser panel to the color chooser.
static Color showDialog(Component c, String title, Color initialColor)It is used to show the color chooser dialog box.


Java JColorChooser Example

  1. import java.awt.event.*;    
  2. import java.awt.*;    
  3. import javax.swing.*;     
  4. public class ColorChooserExample extends JFrame implements ActionListener {    
  5. JButton b;    
  6. Container c;    
  7. ColorChooserExample(){    
  8.     c=getContentPane();    
  9.     c.setLayout(new FlowLayout());         
  10.     b=new JButton("color");    
  11.     b.addActionListener(this);         
  12.     c.add(b);    
  13. }    
  14. public void actionPerformed(ActionEvent e) {    
  15. Color initialcolor=Color.RED;    
  16. Color color=JColorChooser.showDialog(this,"Select a color",initialcolor);    
  17. c.setBackground(color);    
  18. }    
  19.     
  20. public static void main(String[] args) {    
  21.     ColorChooserExample ch=new ColorChooserExample();    
  22.     ch.setSize(400,400);    
  23.     ch.setVisible(true);    
  24.     ch.setDefaultCloseOperation(EXIT_ON_CLOSE);    
  25. }    
  26. }    

Output:

JAVA Jcolorchooser 1

Java JColorChooser Example with ActionListener

  1. import javax.swing.*;  
  2. import java.awt.*;  
  3. import java.awt.event.*;  
  4. public class ColorChooserExample extends JFrame implements ActionListener{  
  5. JFrame f;  
  6. JButton b;  
  7. JTextArea ta;  
  8. ColorChooserExample(){  
  9.     f=new JFrame("Color Chooser Example.");  
  10.     b=new JButton("Pad Color");  
  11.     b.setBounds(200,250,100,30);  
  12.     ta=new JTextArea();  
  13.     ta.setBounds(10,10,300,200);  
  14.     b.addActionListener(this);  
  15.     f.add(b);f.add(ta);  
  16.     f.setLayout(null);  
  17.     f.setSize(400,400);  
  18.     f.setVisible(true);  
  19. }  
  20. public void actionPerformed(ActionEvent e){  
  21.     Color c=JColorChooser.showDialog(this,"Choose",Color.CYAN);  
  22.     ta.setBackground(c);  
  23. }  
  24. public static void main(String[] args) {  
  25.     new ColorChooserExample();  
  26. }  
  27. }     

Output:

JAVA Jcolorchooser 2 JAVA Jcolorchooser 3


Java JTabbedPane

The JTabbedPane class is used to switch between a group of components by clicking on a tab with a given title or icon. It inherits JComponent class.

JTabbedPane class declaration

Let's see the declaration for javax.swing.JTabbedPane class.

  1. public class JTabbedPane extends JComponent implements Serializable, Accessible, SwingConstants  

Commonly used Constructors:

ConstructorDescription
JTabbedPane()Creates an empty TabbedPane with a default tab placement of JTabbedPane.Top.
JTabbedPane(int tabPlacement)Creates an empty TabbedPane with a specified tab placement.
JTabbedPane(int tabPlacement, int tabLayoutPolicy)Creates an empty TabbedPane with a specified tab placement and tab layout policy.

Java JTabbedPane Example

  1. import javax.swing.*;  
  2. public class TabbedPaneExample {  
  3. JFrame f;  
  4. TabbedPaneExample(){  
  5.     f=new JFrame();  
  6.     JTextArea ta=new JTextArea(200,200);  
  7.     JPanel p1=new JPanel();  
  8.     p1.add(ta);  
  9.     JPanel p2=new JPanel();  
  10.     JPanel p3=new JPanel();  
  11.     JTabbedPane tp=new JTabbedPane();  
  12.     tp.setBounds(50,50,200,200);  
  13.     tp.add("main",p1);  
  14.     tp.add("visit",p2);  
  15.     tp.add("help",p3);    
  16.     f.add(tp);  
  17.     f.setSize(400,400);  
  18.     f.setLayout(null);  
  19.     f.setVisible(true);  
  20. }  
  21. public static void main(String[] args) {  
  22.     new TabbedPaneExample();  
  23. }}  

Output:

JAVA Jtabbedpane 1


Java JSlider

The Java JSlider class is used to create the slider. By using JSlider, a user can select a value from a specific range.

Commonly used Constructors of JSlider class

ConstructorDescription
JSlider()creates a slider with the initial value of 50 and range of 0 to 100.
JSlider(int orientation)creates a slider with the specified orientation set by either JSlider.HORIZONTAL or JSlider.VERTICAL with the range 0 to 100 and initial value 50.
JSlider(int min, int max)creates a horizontal slider using the given min and max.
JSlider(int min, int max, int value)creates a horizontal slider using the given min, max and value.
JSlider(int orientation, int min, int max, int value)creates a slider using the given orientation, min, max and value.

Commonly used Methods of JSlider class

MethodDescription
public void setMinorTickSpacing(int n)is used to set the minor tick spacing to the slider.
public void setMajorTickSpacing(int n)is used to set the major tick spacing to the slider.
public void setPaintTicks(boolean b)is used to determine whether tick marks are painted.
public void setPaintLabels(boolean b)is used to determine whether labels are painted.
public void setPaintTracks(boolean b)is used to determine whether track is painted.

Java JSlider Example

  1. import javax.swing.*;  
  2. public class SliderExample1 extends JFrame{  
  3. public SliderExample1() {  
  4. JSlider slider = new JSlider(JSlider.HORIZONTAL, 05025);  
  5. JPanel panel=new JPanel();  
  6. panel.add(slider);  
  7. add(panel);  
  8. }  
  9.   
  10. public static void main(String s[]) {  
  11. SliderExample1 frame=new SliderExample1();  
  12. frame.pack();  
  13. frame.setVisible(true);  
  14. }  
  15. }  

Output:

example of JSlider class

Java JSlider Example: painting ticks

  1. import javax.swing.*;  
  2. public class SliderExample extends JFrame{  
  3. public SliderExample() {  
  4. JSlider slider = new JSlider(JSlider.HORIZONTAL, 05025);  
  5. slider.setMinorTickSpacing(2);  
  6. slider.setMajorTickSpacing(10);  
  7. slider.setPaintTicks(true);  
  8. slider.setPaintLabels(true);  
  9.   
  10. JPanel panel=new JPanel();  
  11. panel.add(slider);  
  12. add(panel);  
  13. }  
  14. public static void main(String s[]) {  
  15. SliderExample frame=new SliderExample();  
  16. frame.pack();  
  17. frame.setVisible(true);  
  18. }  
  19. }  

Output:

example of JSlider class

Java JSpinner

The object of JSpinner class is a single line input field that allows the user to select a number or an object value from an ordered sequence.


JSpinner class declaration

Let's see the declaration for javax.swing.JSpinner class.

  1. public class JSpinner extends JComponent implements Accessible  

Commonly used Contructors:

ConstructorDescription
JSpinner()It is used to construct a spinner with an Integer SpinnerNumberModel with initial value 0 and no minimum or maximum limits.
JSpinner(SpinnerModel model)It is used to construct a spinner for a given model.

Commonly used Methods:

MethodDescription
void addChangeListener(ChangeListener listener)It is used to add a listener to the list that is notified each time a change to the model occurs.
Object getValue()It is used to return the current value of the model.

Java JSpinner Example

  1. import javax.swing.*;    
  2. public class SpinnerExample {  
  3.     public static void main(String[] args) {    
  4.     JFrame f=new JFrame("Spinner Example");    
  5.     SpinnerModel value =  
  6.              new SpinnerNumberModel(5//initial value  
  7.                 0//minimum value  
  8.                 10//maximum value  
  9.                 1); //step  
  10.     JSpinner spinner = new JSpinner(value);   
  11.             spinner.setBounds(100,100,50,30);    
  12.             f.add(spinner);    
  13.             f.setSize(300,300);    
  14.             f.setLayout(null);    
  15.             f.setVisible(true);     
  16. }  
  17. }  

Output:

Java Jspiner 1


Java JSpinner Example with ChangeListener

imp
  1. ort javax.swing.*;    
  2. import javax.swing.event.*;  
  3. public class SpinnerExample {  
  4.     public static void main(String[] args) {    
  5.     JFrame f=new JFrame("Spinner Example");    
  6.     final JLabel label = new JLabel();            
  7.              label.setHorizontalAlignment(JLabel.CENTER);    
  8.              label.setSize(250,100);    
  9.     SpinnerModel value =  
  10.              new SpinnerNumberModel(5//initial value  
  11.                 0//minimum value  
  12.                 10//maximum value  
  13.                 1); //step  
  14.     JSpinner spinner = new JSpinner(value);   
  15.             spinner.setBounds(100,100,50,30);    
  16.             f.add(spinner);  f.add(label);  
  17.            f.setSize(300,300);    
  18.            f.setLayout(null);    
  19.            f.setVisible(true);     
  20.            spinner.addChangeListener(new ChangeListener() {  
  21.         public void stateChanged(ChangeEvent e) {  
  22.          label.setText("Value : " + ((JSpinner)e.getSource()).getValue());  
  23.         }  
  24.      });  
  25. }  
  26. }  

Output:

Java Jspiner 2


Anurag Rana Educator CSE/IT

Comments

Popular posts from this blog

JAVA Scrollbar, MenuItem and Menu, PopupMenu

ava AWT Scrollbar The  object  of Scrollbar class is used to add horizontal and vertical scrollbar. Scrollbar is a  GUI  component allows us to see invisible number of rows and columns. AWT Scrollbar class declaration public   class  Scrollbar  extends  Component  implements  Adjustable, Accessible   Java AWT Scrollbar Example import  java.awt.*;   class  ScrollbarExample{   ScrollbarExample(){               Frame f=  new  Frame( "Scrollbar Example" );               Scrollbar s= new  Scrollbar();               s.setBounds( 100 , 100 ,  50 , 100 );               f.add(s);               f.setSize( 400 , 400 );               f.setLayout( null );               f.setVisible( true );   }   public   static   void  main(String args[]){           new  ScrollbarExample();   }   }   Output: Java AWT Scrollbar Example with AdjustmentListener import  java.awt.*;   import  java.awt.event.*;   class  ScrollbarExample{        ScrollbarExample(){               Frame f=  new  Frame( "Scro

Difference between net platform and dot net framework...

Difference between net platform and dot net framework... .net platform supports programming languages that are .net compatible. It is the platform using which we can build and develop the applications. .net framework is the engine inside the .net platform which actually compiles and produces the executable code. .net framework contains CLR(Common Language Runtime) and FCL(Framework Class Library) using which it produces the platform independent codes. What is the .NET Framework? The Microsoft .NET Framework is a platform for building, deploying, and running Web Services and applications. It provides a highly productive, standards-based, multi-language environment for integrating existing investments with next-generation applications and services as well as the agility to solve the challenges of deployment and operation of Internet-scale applications. The .NET Framework consists of three main parts: the common language runtime, a hierarchical set of unified class librari

C++ this Pointer, static, struct and Enumeration

C++ this Pointer In C++ programming,  this  is a keyword that refers to the current instance of the class. There can be 3 main usage of this keyword in C++. It can be used  to pass current object as a parameter to another method. It can be used  to refer current class instance variable. It can be used  to declare indexers. C++ this Pointer Example Let's see the example of this keyword in C++ that refers to the fields of current class. #include <iostream>    using   namespace  std;   class  Employee {       public :           int  id;  //data member (also instance variable)               string name;  //data member(also instance variable)            float  salary;          Employee( int  id, string name,  float  salary)             {                   this ->id = id;                  this ->name = name;                  this ->salary = salary;            }             void  display()             {                 cout<<id<< "  " <<name<&