The javax.swing package contains the JTable class which is used for representing tabular data in a graphical user interface. One of the methods available for JTable is the setBackground() method which is used to set the background color of the JTable.
Example 1:
// Create a new JTable JTable table = new JTable();
// Set the background color to red table.setBackground(Color.RED);
// Add the table to a JScrollPane and display it on a JFrame JScrollPane scrollPane = new JScrollPane(table); JFrame frame = new JFrame(); frame.add(scrollPane); frame.pack(); frame.setVisible(true);
In this example, we create a new JTable and set its background color to red using the setBackground() method. We then display the table on a JFrame using a JScrollPane.
Example 2:
// Create a new JTable with custom data and column names String[] columnNames = {"Name", "Age", "Gender"}; Object[][] data = { {"John", 25, "Male"}, {"Sarah", 30, "Female"}, {"Tom", 40, "Male"} }; JTable table = new JTable(data, columnNames);
// Set the background color to blue table.setBackground(Color.BLUE);
// Add the table to a JScrollPane and display it on a JFrame JScrollPane scrollPane = new JScrollPane(table); JFrame frame = new JFrame(); frame.add(scrollPane); frame.pack(); frame.setVisible(true);
In this example, we create a new JTable with custom data and column names. We set the background color to blue and display the table on a JFrame.
Both examples use the javax.swing package and its JTable class to create and display tables. The setBackground() method is used to change the background color of the JTable in both examples.
Java JTable.setBackground - 30 examples found. These are the top rated real world Java examples of javax.swing.JTable.setBackground extracted from open source projects. You can rate examples to help us improve the quality of examples.