0% found this document useful (0 votes)
67 views

Jtable Class:The Jtable Class Is Used To Display Data in Tabular Form. It Is Composed of Rows and Columns

The JTable class displays data in a tabular format with rows and columns. It can be initialized with empty cells using JTable() or with specified data using JTable(Object[][] rows, Object[] columns). The JTabbedPane class displays multiple tabbed components and allows the user to switch between them by clicking the tabs. It can be initialized with no tabs using JTabbedPane() or populated with tabs and components using methods like addTab(). The JSplitPane class divides a container into two resizable components.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

Jtable Class:The Jtable Class Is Used To Display Data in Tabular Form. It Is Composed of Rows and Columns

The JTable class displays data in a tabular format with rows and columns. It can be initialized with empty cells using JTable() or with specified data using JTable(Object[][] rows, Object[] columns). The JTabbedPane class displays multiple tabbed components and allows the user to switch between them by clicking the tabs. It can be initialized with no tabs using JTabbedPane() or populated with tabs and components using methods like addTab(). The JSplitPane class divides a container into two resizable components.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

JTable Class:The JTable class is used to display data in tabular form. It is composed of rows and columns.

JTable() Creates a table with empty cells.

JTable(Object[][] rows, Object[] columns) Creates a table with the specified data.

Constructors and Methods Description

JTable() Constructs a default JTable Initialized to default data model, default


column and selection model.

JTable(int nrows, int ncols) Constructs a new JTable with nrows and neols of empty cells using
DelaultTableModel.

JTable(Object[][] rowdata, Constructs a JTable to display the data contained in the two-


dimensional array, rowdata, having the column names specified in the
Object[ ] colnames)
array, colnames.

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.

jTable(TableModel dm, Constructs a JTable initialized to specified data model, dm, also


TableColumnModel cm) initialized to specified column model, cm, with default selection
model.

JTable(TableModel dm, Constructs a JTable initialized to specified data model, dm.


TableColumn specified column model, cm, and specified list selection model, Ism.
Model cm, ListSelectlonModel
lsm)

void addColumn(TableColumn Appends a column to the array of columns.


coI)

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 getRowCount() Returns the number of rows in the table.

int getSelectedColumn() Returns the index of first selected column or returns -1 if no column is
selected.

int getSelectedColumnCount() Returns the number of columns selected.

int[ ] getSelectedColumns() Returns the indices of all columns selected.

int getSelectedRow() Returns the index of first selected row or returns -1 if no row is
1
selected.

int getSelectedRowCount() Returns the number of rows selected.

int[ ] getSelectedRows() Returns the index of all the rows selected.

Object getValueAt(int r, int c) Returns the value at rth row and cth column of JTable.

void removeColumn(Table Deletes or removes a column from JTable's array of columns.


Column tc)
1. import javax.swing.*;    
2. public class TableExample {    
3.     JFrame f;    
4.     TableExample(){    
5.     f=new JFrame();    
6.     String data[][]={ {"101","Amit","670000"},    
7.                           {"102","Jai","780000"},    
8.                           {"101","Sachin","700000"}};    
9.     String column[]={"ID","NAME","SALARY"};         
10.     JTable jt=new JTable(data,column);    
11.     jt.setBounds(30,40,200,300);          
12.     JScrollPane sp=new JScrollPane(jt);    
13.     f.add(sp);           f.setSize(300,400);      f.setVisible(true);    
14. }     
15. public static void main(String[] args) {    
16.     new TableExample();    
17. }   }
JTabbedPane Class:
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 : Set multiple tabbed sheets, which may contain other components. The user can select the sheet
you want to see by tabs.
With the JTabbedPane class, we can have several components (usually objects JPanel) sharing the same
space. The user can choose which component see selecting the tab of the desired component.

Method Purpose

JTabbedPane () Creates a tabbed pane. the argument option should


JTabbedPane (int) appear indicating where the tabs.
By default, the tabs are in the top. You can
specify these positions (as defined in the interface Swing
Constants that implements
Tabbed pane): TOP, BOTTOM, LEFT, LEFT.

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.

Insert, Delete, Find and Select  

Method Purpose

insertTab (String, Icon, Component, String, int) Insert a tab at the specified index, where the


first tab is at index 0. the arguments are the same
as for addTab.

remove (Component) Removes the tab at


removeTabAt (int) index or specified component.

removeAll () Removes all tabs.

indexOfComponent int (Component) Returns the index of the tab that has


indexOfTab int (String) the component, title, or icon.
indexOfTab int (Icon)

setSelectedIndex void (int) Select the tab that has the index


void or  specified component.
Selecting a flange has the effect of
setSelectedComponent (Component)
displaying its associated component.

int getSelectedIndex() Returns the indexor component


selected tab.
Component getSelectedComponent()

Change the Appearance  

Method Purpose

void setComponentAt(int,Component) Set or get which component is  associated


with index tab Specified. The first tab is at index 0.
Component getComponentA(int)

void setTitleAt(int, String) Set or getthe title of thetabspecified index.


String getTitleAt(int)

void setIconAt(int, Icon) Set or get icons displayed tab at the specified index.


Icon getIconAt(int)
void setDisabledIconAt(int, Icon)
Icon getDisabledIconAt(int)

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.

void setEnabledAt(int, boolean) Set or get the activated state of the tab at the specified index.


boolean isEnabledAt(int)
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);      tp.add("visit",p2);      tp.add("help",p3);    
14.     f.add(tp);  
15.     f.setSize(400,400);      f.setLayout(null);      f.setVisible(true);  
16. }  
17. public static void main(String[] args) {  
18.     new TabbedPaneExample();  
19. }}  
JSplitPane Class: JSplitPane is used to divide two components. The two components are divided based on the
look and feel implementation, and they can be resized by the user. If the minimum size of the two components
is greater than the size of the split pane, the divider will not allow you to resize it.
The two components in a split pane can be aligned left to right using JSplitPane.HORIZONTAL_SPLIT, or top
to bottom using JSplitPane.VERTICAL_SPLIT. When the user is resizing the components the minimum size of
the components is used to determine the maximum/minimum position the components can be set to.

Some of the constructors defined by JSplitPane class are as follows.


JSplitPane ()
JSpliPane(int orientation, boolean continuousLayout)
JSplitPane(int orientation, Component TopOrLeft,BottomOrRight)
JSplitPane(int orientation, boolean continuousLayout, Component TopOrLeft, Component BottomOrRight)
where,
orientation specifies how components will be arranged - can have one of the two values:

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)

Accessible getAccessibleContext() It gets the AccessibleContext associated with this JSplitPane.


Context

int getDividerLocation() It returns the last value passed to setDividerLocation.

int getDividerSize() It returns the size of the divider.

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

boolean isContinuousLayout() It gets the continuousLayout property.

boolean isOneTouchExpandable() It gets the oneTouchExpandable property.

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() Creates a JTree with a sample model.

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 removeItem(Object anObject) It is used to delete an item to the item list.

void removeAllItems() It is used to remove all the items from the list.

void setEditable(boolean b) It is used to determine whether the JComboBox is editable.

void addActionListener(ActionListener a) It is used to add the ActionListener.

void addItemListener(ItemListener i) It is used to add the ItemListener.

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.

JSlider(int orientation, int min, int max, int 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.

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.

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() Creates a JList with an empty, read-only, model.

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)

int getSelectedIndex() It is used to return the smallest selected cell index.

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

JPopupMenu() Constructs a JPopupMenu without an "invoker".

JPopupMenu(String label) Constructs a JPopupMenu with the specified title.

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() It is used to create a horizontal progress bar but no string text.

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 It is used to determine whether string should be displayed.


setStringPainted(boolean b)

void setString(String s) It is used to set value to the progress string.

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.

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

You might also like