Ein einfaches Java-Tabellenbeispiel (mit Code)

Java Code

© Johner Images / Getty Images

Der folgende Code zeigt, wie Sie mit der JTable-Klasse eine einfache Tabelle als Teil einer GUI erstellen . Die AbstractTableModel-Klasse wird als Datenmanager für die JTable verwendet.

//Imports are listed in full to show what's being used
//could just import javax.swing.* and java.awt.* etc..
import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.JComboBox;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumn;
import javax.swing.DefaultCellEditor;
public class TableExample {
//Note: Typically the main method will be in a
//separate class. As this is a simple one class
//example it's all in the one class.
public static void main(String[] args) {
//Use the event dispatch thread for Swing components
EventQueue.invokeLater(new Runnable()
public void run()
new TableExample();
public TableExample()
JFrame guiFrame=new JFrame();
//make sure the program exits when the frame closes
guiFrame.setTitle("Creating a Table Example");
//This will center the JFrame in the middle of the screen
//Create the JTable using the ExampleTableModel implementing
//the AbstractTableModel abstract class
JTable table=new JTable(new ExampleTableModel());
//Set the column sorting functionality on
//Uncomment the next line if you want to turn the grid lines off
// table.setShowGrid(false);
//Change the colour of the table - yellow for gridlines
//blue for background
//String array to populate the combobox options
String[] countries={"Australia", "Brazil", "Canada", "China"
,"France", "Japan", "Norway", "Russia", "South Korea"
, "Tunisia", "USA"};
JComboBox countryCombo=new JComboBox(countries);
//Set the default editor for the Country column to be the combobox
TableColumn countryColumn=table.getColumnModel().getColumn(2);
countryColumn.setCellEditor(new DefaultCellEditor(countryCombo));
//set the Event column to be larger than the rest and the Place column
//to be smaller
TableColumn eventColumn=table.getColumnModel().getColumn(3);
TableColumn placeColumn=table.getColumnModel().getColumn(4);
//Place the JTable object in a JScrollPane for a scrolling table
JScrollPane tableScrollPane=new JScrollPane(table);
//implement a table model by extending a class to use
//the AbstractTableModel
class ExampleTableModel extends AbstractTableModel{
//Two arrays used for the table data
String[] columnNames={"First Name", "Surname", "Country"
, "Event", "Place", "Time", "World Record" };
Object[][] data={
{"César Cielo", "Filho", "Brazil", "50m freestyle",1 , "21.30", false },
{"Amaury", "Leveaux", "France", "50m freestyle", 2, "21.45", false },
{"Alain", "Bernard", "France", "50m freestyle", 3, "21.49", false },
{"Alain", "Bernard", "France", "100m freestyle", 1, "47.21", false },
{"Eamon", "Sullivan", "Australia", "100m freestyle", 2, "47.32", false },
{"Jason", "Lezak", "USA", "100m freestyle", 3, "47.67", false },
{"César Cielo", "Filho", "Brazil", "100m freestyle", 3, "47.67", false },
{"Michael", "Phelps", "USA", "200m freestyle", 1, "1:42.96", true },
{"Park", "Tae-Hwan", "South Korea", "200m freestyle", 2, "1:44.85", false },
{"Peter", "Vanderkaay", "USA", "200m freestyle", 3, "1:45.14", false },
{"Park", "Tae-Hwan", "South Korea", "400m freestyle", 1, "3:41.86", false },
{"Zhang", "Lin", "China", "400m freestyle", 2, "3:42.44", false },
{"Larsen", "Jensen", "USA", "400m freestyle", 3, "3:42.78", false },
{"Oussama", "Mellouli", "Tunisia", "1500m freestyle",1 , "14:40.84", false },
{"Grant", "Hackett", "Australia", "1500m freestyle", 2, "14:41.53", false },
{"Ryan", "Cochrane", "Canada", "1500m freestyle", 3, "14:42.69", false },
{"Aaron", "Peirsol", "USA", "100m backstroke", 1, "52.54", true },
{"Matt", "Grevers", "USA", "100m backstroke", 2, "53.11", false },
{"Arkady", "Vyatchanin", "Russia", "100m backstroke", 3, "53.18", false },
{"Hayden", "Stoeckel", "Australia", "100m freestyle", 3, "53.18", false },
{"Ryan", "Lochte", "USA", "200m backstroke", 1, "1:53.94", true },
{"Aaron", "Peirsol", "USA", "200m backstroke", 2, "1:54.33", false },
{"Arkady", "Vyatchanin", "Russia", "200m backstroke", 3, "1:54.93", false },
{"Kosuke", "Kitajima", "Japan", "100m breaststroke", 1, "58.91", true },
{"Alexander", "Dale Oen", "Norway", "100m breaststroke", 2, "59.20", false },
{"Hugues", "Duboscq", "France", "100m breaststroke", 3, "59.37", false }
public int getRowCount()
return data.length;
public int getColumnCount()
return columnNames.length;
public Object getValueAt(int row, int column)
return data[row][column];
//Used by the JTable object to set the column names
public String getColumnName(int column) {
return columnNames[column];
//Used by the JTable object to render different
//functionality based on the data type
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
public boolean isCellEditable(int row, int column)
if (column == 0 || column == 1)
return false;
return true;


Eine   schrittweise Anleitung zu diesem Beispielcode finden Sie unter Erstellen einer einfachen Tabelle .

Similar Posts

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.