Back to: Java Tutorials For Beginners and Professionals
Working with Tables and Progress Bars in Java Swings
In this article, I am going to discuss Working with Tables and Progress bars in Java Swings with Examples. Please read our previous article, where we discussed Working with Images, Menus, and Files in Java Swings.
JTable Swing Control in Java
Swing package provides a JTable class for representing a table and the table was used for storing the data either statically or dynamically. This component is used to display the data in the form of rows and columns.
Syntax : JTable jt = new JTable();
JTable Constructors
- JTable (): It is used to create a table with empty cells.
- JTable(Object[][] rows, Object[] columns) : It is used to create a table with the specified data. Here, the row data represents a two-dimensional array and the column names represent a single dimensional array.
JTable Methods
- getRowCount(): This method returns the count of the number of rows available in the table.
- getColumnCount(): This method returns the count of the number of columns available in the table.
- getSelectedRow(): This method returns the index of the row that is selected. It returns -1 when number row is selected.
- getSelectedRows(): This method returns the indexes of the rows that are selected.
- getSelectedRowCount(): This method returns the count number of rows that are selected.
- getSelectedColumn(): This method returns the index of the column that is selected. It returns -1 if no column is selected.
- getSelectedColumnCount(): Returns the number of columns that are selected.
- getValueAt(row, column): This method returns the heading of the table.
- getJTableHeader(): This method returns the heading of the table.
Example to understand JTable Swing Control in Java:
import javax.swing.*; import javax.swing.table.*; import java.awt.*; public class JTableDemo { public static void main (String[]args) { JFrame jf = new JFrame (); jf.setSize (300, 300); String rows[][] = { {"john", "1001"}, {"Kumar", "1002"}, {"Kishor", "1003"} }; String cols[] = { "ename", "eno" }; JTable jt = new JTable (rows, cols); Font f1 = new Font ("Arial", Font.BOLD, 20); jt.setFont (f1); jt.setBorder (BorderFactory.createLineBorder (Color.red, 1)); JTableHeader jh = jt.getTableHeader (); jf.add (jh, BorderLayout.NORTH); jf.add (jt); jf.setVisible (true); } }
Output:
In the above application JTable constructor is used for binding rows and columns. Here, Font is a predefined class that encapsulates font style. Here, font style is of three i.e. font name, font type, font size. JTableHeader is a predefined class that belongs to javax.swing.table package.
JPogressBar Swing Control in Java
This component will display the progress of a task visually. It basically inherits JComponent class.
Declaration: public class JProgressBar extends JComponent implements SwingConstants, Accessible
Syntax : JProgressBar jb = new JProgressBar();
JProgressBar Constructors
- JProgressBar(): Creates a horizontal progress bar but no String text.
- JProgressBar(int orientation): Orientation will specify whether the progressBar should be displayed either horizontally or vertically.
JProgressBar Methods
- setMinimum(int): This method will set the minimum value of the progress bar.
- setMaximum(int): Set the maximum value if the progress bar.
- setValue(int): This method will set the maximum value of the progress bar. The value of this method must be within the range of min and max value.
- getMinimun(): This method returns the minimum value set to the progress bar.
- getMaximum(): The maximum value set to the progress bar.
- getValue(): The current status of the progress bar.
- setOrientation(int): This Method return is used to specify the Orientation of the progress bar.
- getOrientation(): This method will return the orientation of the progress bar.
- setStringPainted(boolean): This method will display the percentage of the task that is executed.
Example to understand JProgressBar Swing Control in Java:
import java.awt.BorderLayout; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JProgressBar; public class JProgressBarDemo extends JFrame { static JProgressBarDemo f1 = null; public static void main (String[]args) { JPanel panel1 = new JPanel (); JPanel panel2 = new JPanel (); JButton button = new JButton (); JProgressBarDemo f = null; f = new JProgressBarDemo (); f.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); f.setSize (700, 400); panel1.setLayout (new BorderLayout ()); panel1.setForeground (Color.white); button.setText ("Click"); panel1.add (button, BorderLayout.SOUTH); f.setContentPane (panel1); f.setVisible (true); f1 = new JProgressBarDemo (); f1.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); f1.setSize (457, 100); f1.setTitle ("Progress"); f1.setLocationRelativeTo (null); panel2.setLayout (new BorderLayout ()); panel2.setForeground (Color.white); JProgressBar progressBar = new JProgressBar (); progressBar.setValue (35); progressBar.setStringPainted (true); panel2.add (progressBar, BorderLayout.SOUTH); f1.setContentPane (panel2); button.addActionListener (new ActionListener () { public void actionPerformed (ActionEvent e) { f1.setVisible (true);} } ); } }
Output:
After clicking on the button, you will get the following output:
In the next article, I am going to discuss Enumerations in Java with Examples. Here, in this article, I try to explain Working with Tables and Progessbars in Java Swings and I hope you enjoy this Working with Tables in Java Swings article.