Java - How to Select a Random Row From a JTable In Java

How to Randomly Select a Row From a JTable In Java Netbeans

How to Randomly Select a Row From a JTable In Java Netbeans




In this Java Tutorial we will see How To Make button to highlight and scroll to a randomly selected row in the table Using Netbeans.

What We Are Gonna Use In This Project:

- Java Programming Language.
- NetBeans Editor.

What This Project Do:

create a JFrame with a table displaying sample data. 
Users can click the "Random Selection" button to highlight and scroll to a randomly selected row in the table.





Project Source Code:





package new_tutorials;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Rectangle;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;

/**
 *
 * @author 1BestCsharp
 */
public class RandomTableSelection extends JFrame {

    private JTable table;
    private JButton randomSelectionButton;
    
    
    public RandomTableSelection(){
        setTitle("Random Table Selection");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400,400);
        setLocationRelativeTo(null);
        initializeTable();
        initializeGUI();
    }
    
    private void initializeTable(){
    
        Object[][] data = 
        {
            {"John", "Doe", 30},
            {"Jane", "Smith", 25},
            {"Michael", "Johnson", 35},
            {"Emily", "Williams", 28},
            {"Robert", "Brown", 42},
            {"Linda", "Jones", 29},
            {"David", "Miller", 37},
            {"Patricia", "Taylor", 31},
            {"Daniel", "Anderson", 26},
            {"Susan", "Clark", 39},
            {"Richard", "Martinez", 34},
            {"Nancy", "Thompson", 27},
            {"Matthew", "Hall", 33},
            {"Jennifer", "Walker", 30},
            {"Anthony", "White", 36},
            {"Lisa", "Harris", 29},
            {"Paul", "Young", 32},
            {"Mary", "Martin", 28},
            {"Andrew", "Lee", 38},
            {"Karen", "Lewis", 31},
            {"George", "Scott", 35},
            {"Margaret", "Robinson", 27},
            {"William", "King", 29},
            {"Elizabeth", "Wright", 34},
            {"Kenneth", "Lopez", 30},
            {"Maria", "Hill", 33},
            {"Steven", "Allen", 32},
            {"Jessica", "Green", 26},
        };
        String[] columnNames = {"First Name","Last Name","Age"};
        table = new JTable(data, columnNames);
        JScrollPane scrollPane = new JScrollPane(table);
        add(scrollPane, BorderLayout.CENTER);
    }
    
    
    private void initializeGUI(){
    
        randomSelectionButton = new JButton("Random Selection");
        randomSelectionButton.addActionListener((e) -> {
            selectRandomRow();
        });
        
        add(randomSelectionButton, BorderLayout.SOUTH);
    }
    
    private void selectRandomRow(){
        
        int rowCount = table.getRowCount();
        if(rowCount > 0){
            // Generate a random row index
            int randomIndex = new Random().nextInt(rowCount);
            // Select the random row
            table.setRowSelectionInterval(randomIndex, randomIndex);
            table.setSelectionBackground(Color.black);
            table.setSelectionForeground(Color.yellow);
            // Automatically scroll to the selected row
            Rectangle cellRect = table.getCellRect(randomIndex, 0, true);
            table.scrollRectToVisible(cellRect);
        }
        else{
            JOptionPane.showMessageDialog(this, "No data in the table");
        }
        
    }
    
    
    public static void main(String[] args) {
        
       new RandomTableSelection().setVisible(true);
        
    }
    
    
}



The Final Result:

How to Select a Random Row From a JTable In Java

Java JTable Random Row Selection










Aucun commentaire:

Enregistrer un commentaire