Jtable Class:The Jtable Class Is Used To Display Data in Tabular Form. It Is Composed of Rows and Columns
Jtable Class:The Jtable Class Is Used To Display Data in Tabular Form. It Is Composed of Rows and Columns
JTable(Object[][] rows, Object[] columns) Creates a table with the specified data.
JTable(int nrows, int ncols) Constructs a new JTable with nrows and neols of empty cells using
DelaultTableModel.
JTable(Vector rowvector, Vector Constructs a JTable to display the data available in vector of vectors,
colvector) rowvector, with column names available in colvector.
JTable(TableModel dm) Constructs a new JTable initialized to specify data model, dm, with
the default column model and the default selection model.
int getColumnCount() Returns the number of columns in the column model. (Note: number
of columns here may differ from the number of columns In the table
model).
int getSelectedColumn() Returns the index of first selected column or returns -1 if no column is
selected.
int getSelectedRow() Returns the index of first selected row or returns -1 if no row is
1
selected.
Object getValueAt(int r, int c) Returns the value at rth row and cth column of JTable.
Method Purpose
addTab (String, Icon, Component, String) Add a new tab to the tabbed pane. The first argument
2
addTab (String, Icon, Component) specifies the text of the tab. The Icon argument is
addTab (String, Component) optional and indicates icon tab. the
argument Component specifies the component that
the Tabbed pane should show when select the tab. The
fourth argument, if exists, specifies
the tooltip text for tab.
Method Purpose
Change the Appearance
Method Purpose
3
void setBackgroundAt(int, Color) Set or get the background
color or Used by foreground tab index Specified. By default,
Color getBackgroundAt(int)
a tab uses the colors of the tabbed pane.
void setForegroundAt(int, Color) For example, if the foreground color of Tabbed pane is black,
Color getForegroundAt(int) then all The tab titles are in black, except for those which you
specify another setForegroundAt using color.
4
HORIZONTAL_SPLIT and VERTICAL_SPLIT
continuousLayout can have one of the two values: true,if the components are resizable
when divider is moved, otherwise false (default value)
TopOrLeft specifies the object to be placed at top or left
BottomOrRight specifies the object to be placed at bottom or right
protected addImpl(Component comp, It cdds the specified component to this split pane.
void Object constraints, int index)
Component getBottomComponent() It returns the component below, or to the right of the divider.
Component getRightComponent() It returns the component to the right (or below) the divider.
SplitPaneU getUI() It returns the SplitPaneUI that is providing the current look and feel.
I
void setOrientation(int orientation) It gets the orientation, or how the splitter is divided.
1. import java.awt.FlowLayout;
2. import java.awt.Panel; import javax.swing.JComboBox;
3. import javax.swing.JFrame; import javax.swing.JSplitPane;
4. public class JSplitPaneExample {
5. private static void createAndShow() {
6. // Create and set up the window.
7. final JFrame frame = new JFrame("JSplitPane Example");
8. // Display the window.
9. frame.setSize(300, 300);
10. frame.setVisible(true);
11. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
12. // set flow layout for the frame
13. frame.getContentPane().setLayout(new FlowLayout());
14. String[] option1 = { "A","B","C","D","E" };
15. JComboBox box1 = new JComboBox(option1);
5
16. String[] option2 = {"1","2","3","4","5"};
17. JComboBox box2 = new JComboBox(option2);
18. Panel panel1 = new Panel();
19. panel1.add(box1);
20. Panel panel2 = new Panel();
21. panel2.add(box2);
22. JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panel1, panel2);
23. // JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, panel1, panel2);
24. frame.getContentPane().add(splitPane);
25. }
26. public static void main(String[] args) {
27. // Schedule a job for the event-dispatching thread:
28. // creating and showing this application's GUI.
29. javax.swing.SwingUtilities.invokeLater(new Runnable() {
30. public void run() {
31. createAndShow();
32. }
33. });
34. }
35. }
JTree Class:
The JTree class is used to display the tree structured data or hierarchical data. JTree is a complex component. It
has a 'root node' at the top most which is a parent for all nodes in the tree. It inherits JComponent class.
public class JTree extends JComponent implements Scrollable, Accessible
JTree(Object[] value) Creates a JTree with every element of the specified array as the child of a new root node.
JTree(TreeNode root) Creates a JTree with the specified TreeNode as its root, which displays the root node.
1. import javax.swing.*;
2. import javax.swing.tree.DefaultMutableTreeNode;
3. public class TreeExample {
4. JFrame f;
5. TreeExample(){
6. f=new JFrame();
7. DefaultMutableTreeNode style=new DefaultMutableTreeNode("Style");
8. DefaultMutableTreeNode color=new DefaultMutableTreeNode("color");
9. DefaultMutableTreeNode font=new DefaultMutableTreeNode("font");
10. style.add(color);
11. style.add(font);
12. DefaultMutableTreeNode red=new DefaultMutableTreeNode("red");
6
13. DefaultMutableTreeNode blue=new DefaultMutableTreeNode("blue");
14. DefaultMutableTreeNode black=new DefaultMutableTreeNode("black");
15. DefaultMutableTreeNode green=new DefaultMutableTreeNode("green");
16. color.add(red); color.add(blue); color.add(black); color.add(green);
17. JTree jt=new JTree(style);
18. f.add(jt);
19. f.setSize(200,200); f.setVisible(true);
20. }
21. public static void main(String[] args) {
22. new TreeExample();
23. }}
JComboBox Class:
The object of Choice class is used to show popup menu of choices. Choice selected by user is shown on the top
of a menu. It inherits JComponent class.
public class JComboBox extends JComponent implements ItemSelectable, ListDataListener, ActionListener,
Accessible
JComboBox() Creates a JComboBox with a default data model.
JComboBox(Object[] items) Creates a JComboBox that contains the elements in the specified array.
JComboBox(Vector<?> items) Creates a JComboBox that contains the elements in the specified Vector.
Methods:
void addItem(Object anObject) It is used to add an item to the item list.
void removeAllItems() It is used to remove all the items from the list.
JSlider Class: The Java JSlider class is used to create the slider. By using JSlider, a user can select a value
from a specific range.
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.
7
creates a slider using the given orientation, min, max and value.
Methods:
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.
1. import javax.swing.*;
2. public class SliderExample1 extends JFrame{
3. public SliderExample1() {
4. JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 50, 25);
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. }
JList Class: The object of JList class represents a list of text items. The list of text items can be set up so that
the user can choose either one item or multiple items. It inherits JComponent class.
public class JList extends JComponent implements Scrollable, Accessible
JList(ary[] listData) Creates a JList that displays the elements in the specified array.
JList(ListModel<ary> dataModel) Creates a JList that displays elements from the specified, non-null, model.
Void It is used to add a listener to the list, to be notified each time a change to the
addListSelectionListener(ListSelecti selection occurs.
onListener listener)
8
ListModel getModel() It is used to return the data model that holds a list of items displayed by the
JList component.
void setListData(Object[] listData) It is used to create a read-only ListModel from an array of objects.
JMenu Class: The JMenuBar class is used to display menubar on the window or frame. It may have several
menus.
The object of JMenu class is a pull down menu component which is displayed from the menu bar. It inherits the
JMenuItem class.
The object of JMenuItem class adds a simple labeled menu item. The items used in a menu must belong to the
JMenuItem or any of its subclass.
public class JMenu extends JMenuItem implements MenuElement, Accessible
1. import javax.swing.*;
2. class MenuExample
3. {
4. JMenu menu, submenu;
5. JMenuItem i1, i2, i3, i4, i5;
6. MenuExample(){
7. JFrame f= new JFrame("Menu and MenuItem Example");
8. JMenuBar mb=new JMenuBar();
9. menu=new JMenu("Menu");
10. submenu=new JMenu("Sub Menu");
11. i1=new JMenuItem("Item 1");
12. i2=new JMenuItem("Item 2");
13. i3=new JMenuItem("Item 3");
14. i4=new JMenuItem("Item 4");
15. i5=new JMenuItem("Item 5");
16. menu.add(i1); menu.add(i2); menu.add(i3);
17. submenu.add(i4); submenu.add(i5);
18. menu.add(submenu);
19. mb.add(menu);
20. f.setJMenuBar(mb);
21. f.setSize(400,400); f.setLayout(null); f.setVisible(true);
22. }
23. public static void main(String args[])
24. {
25. new MenuExample();
26. }}
JPopUpMenu Class: PopupMenu can be dynamically popped up at specific position within a component. It
inherits the JComponent class.
9
public class JPopupMenu extends JComponent implements Accessible, MenuElement
1. import javax.swing.*;
2. import java.awt.event.*;
3. class PopupMenuExample
4. {
5. PopupMenuExample(){
6. final JFrame f= new JFrame("PopupMenu Example");
7. final JPopupMenu popupmenu = new JPopupMenu("Edit");
8. JMenuItem cut = new JMenuItem("Cut");
9. JMenuItem copy = new JMenuItem("Copy");
10. JMenuItem paste = new JMenuItem("Paste");
11. popupmenu.add(cut); popupmenu.add(copy); popupmenu.add(paste);
12. f.addMouseListener(new MouseAdapter() {
13. public void mouseClicked(MouseEvent e) {
14. popupmenu.show(f , e.getX(), e.getY());
15. }
16. });
17. f.add(popupmenu);
18. f.setSize(300,300); f.setLayout(null); f.setVisible(true);
19. }
20. public static void main(String args[])
21. {
22. new PopupMenuExample();
23. }}
JProgressBar Class:
The JProgressBar class is used to display the progress of the task. It inherits JComponent class.
public class JProgressBar extends JComponent implements SwingConstants, Accessible
JProgressBar(int min, int It is used to create a horizontal progress bar with the specified minimum and
max) maximum value.
JProgressBar(int orient) It 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, It is used to create a progress bar with the specified orientation, minimum and
10
int min, int max) maximum value.
void setOrientation(int It is used to set the orientation, it may be either vertical or horizontal by using
orientation) SwingConstants.VERTICAL and SwingConstants.HORIZONTAL constants.
void setValue(int value) It is used to set the current value on the progress bar.
1. import javax.swing.*;
2. public class ProgressBarExample extends JFrame{
3. JProgressBar jb;
4. int i=0,num=0;
5. ProgressBarExample(){
6. jb=new JProgressBar(0,2000);
7. jb.setBounds(40,40,160,30);
8. jb.setValue(0);
9. jb.setStringPainted(true);
10. add(jb);
11. setSize(250,150);
12. setLayout(null);
13. }
14. public void iterate(){
15. while(i<=2000){
16. jb.setValue(i);
17. i=i+20;
18. try{Thread.sleep(150);}catch(Exception e){}
19. }
20. }
21. public static void main(String[] args) {
22. ProgressBarExample m=new ProgressBarExample();
23. m.setVisible(true);
24. m.iterate();
25. }
26. }
JColorChooser Class:
The JColorChooser class is used to create a color chooser dialog box so that user can select any color. It
inherits JComponent class.
11
public class JColorChooser extends JComponent implements Accessible
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.
static Color showDialog(Component c, String title, Color It is used to show the color chooser dialog box.
initialColor)
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. }
12