Tuesday, 12 August 2014

Swing Components

JComboBox class:

The JComboBox class is used to create the combobox (drop-down list). At a time only one item can be selected from the item list.

Commonly used Constructors of JComboBox class:

JComboBox()
JComboBox(Object[] items)
JComboBox(Vector<?> items)

Commonly used methods of JComboBox class:

1) public void addItem(Object anObject): is used to add an item to the item list.
2) public void removeItem(Object anObject): is used to delete an item to the item list.
3) public void removeAllItems(): is used to remove all the items from the list.
4) public void setEditable(boolean b): is used to determine whether the JComboBox is editable.
5) public void addActionListener(ActionListener a): is used to add the ActionListener.
6) public void addItemListener(ItemListener i): is used to add the ItemListener.

Example of JComboBox class:

  1. import javax.swing.*;  
  2. public class Combo {  
  3. JFrame f;  
  4. Combo(){  
  5.     f=new JFrame("Combo ex");  
  6.       
  7.     String country[]={"India","Aus","U.S.A","England","Newzeland"};  
  8.       
  9.     JComboBox cb=new JComboBox(country);  
  10.     cb.setBounds(5050,90,20);  
  11.     f.add(cb);  
  12.       
  13.     f.setLayout(null);  
  14.     f.setSize(400,500);  
  15.     f.setVisible(true);  
  16.       
  17. }  
  18. public static void main(String[] args) {  
  19.     new Combo();  
  20.       
  21. }  
  22. JTable class :

    The JTable class is used to display the data on two dimensional tables of cells.

    Commonly used Constructors of JTable class:

    JTable(): creates a table with empty cells. 
  23. JTable(Object[][] rows, Object[] columns): creates a table with the specified data.

Example of JTable class:

  1. import javax.swing.*;  
  2. public class MyTable {  
  3.     JFrame f;  
  4. MyTable(){  
  5.     f=new JFrame();  
  6.       
  7.     String data[][]={ {"101","Amit","670000"},  
  8.               {"102","Jai","780000"},  
  9.                           {"101","Sachin","700000"}};  
  10.     String column[]={"ID","NAME","SALARY"};  
  11.       
  12.     JTable jt=new JTable(data,column);  
  13.     jt.setBounds(30,40,200,300);  
  14.       
  15.     JScrollPane sp=new JScrollPane(jt);  
  16.     f.add(sp);  
  17.       
  18.     f.setSize(300,400);  
  19. //  f.setLayout(null);  
  20.     f.setVisible(true);  
  21. }  
  22. public static void main(String[] args) {  
  23.     new MyTable();  
  24. }  
  25. }  
  26. ColorChooser class:

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

    Commonly used Constructors of JColorChooser class:

    • JColorChooser(): is used to create a color chooser pane with white color initially.
    • JColorChooser(Color initialColor): is used to create a color chooser pane with the specified color initially.

    Commonly used methods of JColorChooser class:

    public static Color showDialog(Component c, String title, Color initialColor): is used to show the color-chooser dialog box.

    Example of JColorChooser class:

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





    JProgressBar class:

    The JProgressBar class is used to display the progress of the task.

    Commonly used Constructors of JProgressBar class:

    • JProgressBar(): is used to create a horizontal progress bar but no string text.
    • JProgressBar(int min, int max): is used to create a horizontal progress bar with the specified minimum and maximum value.
    • JProgressBar(int orient): is used to create a progress bar with the specified orientation, it can be either Vertical or Horizontal by using SwingConstants.VERTICAL and SwingConstants.HORIZONTAL constants.
    • JProgressBar(int orient, int min, int max): is used to create a progress bar with the specified orientation, minimum and maximum value.

    Commonly used methods of JProgressBar class:

    1) public void setStringPainted(boolean b): is used to determine whether string should be displayed.
    2) public void setString(String s): is used to set value to the progress string.
    3) public void setOrientation(int orientation): is used to set the orientation, it may be either vertical or horizontal by using SwingConstants.VERTICAL and SwingConstants.HORIZONTAL constants..
    4) public void setValue(int value): is used to set the current value on the progress bar.

    Example of JProgressBar class:

    1. import javax.swing.*;  
    2. public class MyProgress extends JFrame{  
    3. JProgressBar jb;  
    4. int i=0,num=0;  
    5.   
    6. MyProgress(){  
    7. jb=new JProgressBar(0,2000);  
    8. jb.setBounds(40,40,200,30);  
    9.       
    10. jb.setValue(0);  
    11. jb.setStringPainted(true);  
    12.       
    13. add(jb);  
    14. setSize(400,400);  
    15. setLayout(null);  
    16. }  
    17.   
    18. public void iterate(){  
    19. while(i<=2000){  
    20.   jb.setValue(i);  
    21.   i=i+20;  
    22.   try{Thread.sleep(150);}catch(Exception e){}  
    23. }  
    24. }  
    25. public static void main(String[] args) {  
    26.     MyProgress m=new MyProgress();  
    27.     m.setVisible(true);  
    28.     m.iterate();  
    29. }  
    30. }  


    JSlider class:

    The JSlider 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:

    • 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:

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

    Simple example of JSlider class:

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


    Example of JSlider class that paints ticks:

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

     

Displaying graphics in swing:

java.awt.Graphics class provides many methods for graphics programming.

Commonly used methods of Graphics class:

  1. public abstract void drawString(String str, int x, int y): is used to draw the specified string.
  2. public void drawRect(int x, int y, int width, int height): draws a rectangle with the specified width and height.
  3. public abstract void fillRect(int x, int y, int width, int height): is used to fill rectangle with the default colorand specified width and height.
  4. public abstract void drawOval(int x, int y, int width, int height): is used to draw oval with the specified width and height.
  5. public abstract void fillOval(int x, int y, int width, int height): is used to fill oval with the default color and specified width and height.
  6. public abstract void drawLine(int x1, int y1, int x2, int y2): is used to draw line between the points(x1, y1) and (x2, y2).
  7. public abstract boolean drawImage(Image img, int x, int y, ImageObserver observer): is used draw the specified image.
  8. public abstract void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle): is used draw acircular or elliptical arc.
  9. public abstract void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle): is used to fill a circular or elliptical arc.
  10. public abstract void setColor(Color c): is used to set the graphics current color to the specified color.
  11. public abstract void setFont(Font font): is used to set the graphics current font to the specified font.

Example of displaying graphics in swing:

Example of displaying graphics in swing
  1. import java.awt.*;  
  2. import javax.swing.JFrame;  
  3.   
  4. public class DisplayGraphics extends Canvas{  
  5.       
  6.     public void paint(Graphics g) {  
  7.         g.drawString("Hello",40,40);  
  8.         setBackground(Color.WHITE);  
  9.         g.fillRect(13030,10080);  
  10.         g.drawOval(30,130,5060);  
  11.         setForeground(Color.RED);  
  12.         g.fillOval(130,130,5060);  
  13.         g.drawArc(3020040,50,90,60);  
  14.         g.fillArc(3013040,50,180,40);  
  15.           
  16.     }  
  17.         public static void main(String[] args) {  
  18.         DisplayGraphics m=new DisplayGraphics();  
  19.         JFrame f=new JFrame();  
  20.         f.add(m);  
  21.         f.setSize(400,400);  
  22.         //f.setLayout(null);  
  23.         f.setVisible(true);  
  24.     }  
  25.   }
  26.  

No comments:

Post a Comment