Working with Tables and Progress Bars in Java Swings

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 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
  1. JTable (): It is used to create a table with empty cells.
  2. 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
  1. getRowCount(): This method returns the count of the number of rows available in the table.
  2. getColumnCount(): This method returns the count of the number of columns available in the table.
  3. getSelectedRow(): This method returns the index of the row that is selected. It returns -1 when number row is selected.
  4. getSelectedRows(): This method returns the indexes of the rows that are selected.
  5. getSelectedRowCount(): This method returns the count number of rows that are selected.
  6. getSelectedColumn(): This method returns the index of the column that is selected. It returns -1 if not column is selected.
  7. getSelectedColumnCount(): Returns the number of columns that are selected.
  8. getValueAt(row, column): This method returns the heading of the table.
  9. getJTableHeader(): This method returns the heading of the table.
Sample Program to understand JTable 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:

Program to understand JTable in Java

In the above application JTable constructor 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

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
  1. JProgressBar(): Creates a horizontal progress bar but no String text.
  2. JProgressBar(int orientation): Orientation will specify whether the progressBar should be displayed either horizontally or vertically.
JProgressBar Methods
  1. setMinimum(int): This method will set the minimum value of the progress bar.
  2. setMaximum(int): Set the maximum value if the progress bar.
  3. 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.
  4. getMinimun(): This method returns the minimum value set to the progress bar.
  5. getMaximum(): The maximum value set to the progress bar.
  6. getValue(): The current status of the progress bar.
  7. setOrientation(int): This method returns is used to specify the Orientation of the progress bar.
  8. getOrientation(): This method will return the orientation of the progress bar.
  9. setStringPainted(boolean): This method will display the percentage of the task that is executed.
Sample Program to understand JProgressBar 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.

Leave a Reply

Your email address will not be published. Required fields are marked *