Java - Display Different Time Zones

How to Display Times From Different Time Zones In JTable Using Java Netbeans

Java Display Different Time Zones


In this Java Tutorial we will see How To Create a JTable (ClockTable) to display time zones and their respective times In Java Using Netbeans.
We Will Create JPanel class (ClockPanel) to display the current time.
We Will also create a search panel above the table with a label, text field, and search button, so the users can search for a specific time zone using the search field.

What We Are Gonna Use In This Project:

- Java Programming Language.
- NetBeans Editor.





Project Source Code:


package new_tutorials;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.RowFilter;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableRowSorter;

/**
 *
 * @author 1BestCsharp
 */
public class WorldTimeApp {
    
    public static void main(String[] args) {
        
        // Set Nimbus Look and Feel
        try{
            UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
        }
        catch(ClassNotFoundException | IllegalAccessException | InstantiationException | UnsupportedLookAndFeelException ex){
            System.out.println(ex.getMessage()); 
        }
        
        // Create the main JFrame
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500,500);
        frame.setLocationRelativeTo(null);
        
        // Create ClockPanel and ClockTable
        ClockPanel clockPanel = new ClockPanel();
        ClockTable clockTable = new ClockTable();
        
        // Set BorderLayout for the main frame
        frame.setLayout(new BorderLayout());
        frame.add(clockPanel, BorderLayout.NORTH);
        
        // Add searchPanel between ClockPanel and JTable
        JPanel mainPanel = new JPanel(new BorderLayout());
        mainPanel.add(new JScrollPane(clockTable), BorderLayout.CENTER);
        mainPanel.add(clockTable.createSearchPanel(), BorderLayout.NORTH);
        frame.add(mainPanel, BorderLayout.CENTER);
        
        frame.setVisible(true);
        
    }

}

// Create a class to display the time
class ClockPanel extends JPanel{
    
    private final Timer timer;
    private final SimpleDateFormat timeFormat;
    
    public ClockPanel(){
        // Set background color and preferred size
        setBackground(Color.BLACK);
        setPreferredSize(new Dimension(500,100));
        // Initialize timer and time format
        timeFormat = new SimpleDateFormat("hh:mm:ss a");
        timer = new Timer(1000, e -> repaint());
        timer.start();
    }
    
    
    @Override
    protected void paintComponent(Graphics g){
        super.paintComponent(g);
        // Get current time and format it
        Date now = new Date();
        String time = timeFormat.format(now);
        
        // Draw the formatted time on the panel
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setColor(Color.ORANGE);
        g2d.setFont(new Font("Arial",Font.BOLD, 36));
        g2d.drawString(time, getWidth()/2 - 100, getHeight()/2);
    }
    
}


// create a class table to display the time zones
class ClockTable extends JTable{
    
    private final DefaultTableModel model;
    private final TableRowSorter<DefaultTableModel> sorter;
    
    public ClockTable(){
        // Initialize table model and columns
        model = new DefaultTableModel();
        model.addColumn("Time Zone");
        model.addColumn("Time");
        
        // Populate the table with time zone data
        String[] timeZones = TimeZone.getAvailableIDs();
        for(String timeZoneId : timeZones){
            TimeZone timeZone = TimeZone.getTimeZone(timeZoneId);
            model.addRow(new Object[]{timeZoneId, getFormattedTime(timeZone)});
        }
        
        // Set model 
        setModel(model);
        
        setFont(new Font("Arial", Font.PLAIN, 18));
        setRowHeight(25);
        
        // Initialize table sorter
        sorter = new TableRowSorter<>(model);
        setRowSorter(sorter);
        
        // Customize table header
        JTableHeader header = getTableHeader();
        header.setFont(new Font("Arial", Font.BOLD, 18));
        
    }
    
    // Format time according to the given time zone
    private String getFormattedTime(TimeZone timeZone){
        SimpleDateFormat timeFormat = new SimpleDateFormat("hh:mm:ss a");
        timeFormat.setTimeZone(timeZone);
        return timeFormat.format(new Date());
    }
    
    
    // Create searchPanel
    public JPanel createSearchPanel(){
        
        JLabel searchLabel = new JLabel("Search: ");
        JTextField searchField = new JTextField();
        JButton searchButton = new JButton("Search");
        
        searchLabel.setFont(new Font("Arial", Font.PLAIN,18));
        searchField.setFont(new Font("Arial", Font.PLAIN,18));
        searchButton.setFont(new Font("Arial", Font.PLAIN,18));
        
        
        JPanel searchPanel = new JPanel();
        searchPanel.setBackground(Color.WHITE);
        searchPanel.setLayout(new BorderLayout());
        searchPanel.add(searchLabel, BorderLayout.WEST);
        searchPanel.add(searchField, BorderLayout.CENTER);
        searchPanel.add(searchButton, BorderLayout.EAST);
        
        searchButton.addActionListener((e) -> {
            // Apply row filter based on search text
            String text = searchField.getText();
            if(text.length() == 0){
                sorter.setRowFilter(null);
            }
            else{
                try{ sorter.setRowFilter(RowFilter.regexFilter(text, 0)); }
                
                catch(Exception ex){ System.out.println(ex.getMessage()); }
            }
            
        });
        
        return searchPanel;
        
    }
   
}


The Final Result:

How To Get And Display Time Zones In Java Netbeans






Share this

Related Posts

Previous
Next Post »