This document provides an example of how to use a JTable component in Java Swing to populate a table with multi-dimensional data. The code demonstrates creating a JFrame container, adding a JPanel and JTable to it. A JScrollPane is used to display the table, which is populated with sample data - a 2D Object array with columns for name, country, and telephone number. The frame is made visible to display the populated table.
32 Ways a Digital Marketing Consultant Can Help Grow Your BusinessBarry Feldman
How can a digital marketing consultant help your business? In this resource we'll count the ways. 24 additional marketing resources are bundled for free.
32 Ways a Digital Marketing Consultant Can Help Grow Your BusinessBarry Feldman
How can a digital marketing consultant help your business? In this resource we'll count the ways. 24 additional marketing resources are bundled for free.
Othello.java
package othello;
import core.Game;
import userInterface.OthelloUi;
public class Othello {
// main is \"static\" because it\'s going to be called by the java interpreter
// before any objects are created, thus, it does not need to be in an
// instance of the Othello class to be called, it can simply be called
// whenever.
public static void main(String[] args) {
Game game = new Game();
OthelloUi othelloUi = new OthelloUi(game);
}
}
OthelloUi.java
package userInterface;
import core.Game;
import userInterface.GameUi;
import userInterface.BoardUi;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
public class OthelloUi extends JFrame {
private Game game;
private GameUi gameUi;
private BoardUi boardUi;
public OthelloUi(Game game) {
this.game = game;
// Create components and put them in JFrame upon class
// instantiation.
initComponents();
}
private void initComponents() {
this.setPreferredSize(new Dimension(600, 600));
this.setMinimumSize(new Dimension(600, 600));
// This is done to close the actual application once the
// UI is closed via X.
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gameUi = new GameUi();
boardUi = new BoardUi();
// By default, JFrame uses BorderLayout as its LayoutManager,
// which is responsible for managing how UI components are placed
// within framses/panels.
this.add(gameUi, BorderLayout.NORTH);
this.add(boardUi, BorderLayout.CENTER);
this.setVisible(true);
}
}
GameUi.java
package userInterface;
import java.awt.Dimension;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class GameUi extends JPanel {
public GameUi() {
// Creates components and puts them in the JPanel on
// instantiation.
initComponents();
}
private JLabel nameOne;
private JLabel nameTwo;
private JLabel scoreOne;
private JLabel scoreTwo;
private void initComponents() {
// Set dimensions for JPanel.
this.setPreferredSize(new Dimension(200, 100));
this.setMinimumSize(new Dimension(200, 100));
// Label components.
nameOne = new JLabel(\"Player One\");
nameTwo = new JLabel(\"Player Two\");
scoreOne = new JLabel(\"0\");
scoreTwo = new JLabel(\"0\");
nameOne.setMinimumSize(new Dimension(100, 50));
nameTwo.setMinimumSize(new Dimension(100, 50));
scoreOne.setMinimumSize(new Dimension(100, 50));
scoreTwo.setMinimumSize(new Dimension(100, 50));
// JPanel uses FlowLayout layout manager, which adds components
// left to right in a row, until it runs out of space and creates
// a new row.
this.add(nameOne);
this.add(scoreOne);
this.add(nameTwo);
this.add(scoreTwo);
}
}
BoardUi.java
package userInterface;
import core.Constants;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
public class BoardUi extends JPanel {
private JButton[][] board;
private BoardListener listener;
public BoardUi() {
initComponents();
}
private voi.
Othello.java
package othello;
import core.Game;
import userInterface.OthelloUi;
public class Othello {
// main is \"static\" because it\'s going to be called by the java interpreter
// before any objects are created, thus, it does not need to be in an
// instance of the Othello class to be called, it can simply be called
// whenever.
public static void main(String[] args) {
Game game = new Game();
OthelloUi othelloUi = new OthelloUi(game);
}
}
OthelloUi.java
package userInterface;
import core.Game;
import userInterface.GameUi;
import userInterface.BoardUi;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
public class OthelloUi extends JFrame {
private Game game;
private GameUi gameUi;
private BoardUi boardUi;
public OthelloUi(Game game) {
this.game = game;
// Create components and put them in JFrame upon class
// instantiation.
initComponents();
}
private void initComponents() {
this.setPreferredSize(new Dimension(600, 600));
this.setMinimumSize(new Dimension(600, 600));
// This is done to close the actual application once the
// UI is closed via X.
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gameUi = new GameUi();
boardUi = new BoardUi();
// By default, JFrame uses BorderLayout as its LayoutManager,
// which is responsible for managing how UI components are placed
// within framses/panels.
this.add(gameUi, BorderLayout.NORTH);
this.add(boardUi, BorderLayout.CENTER);
this.setVisible(true);
}
}
GameUi.java
package userInterface;
import java.awt.Dimension;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class GameUi extends JPanel {
public GameUi() {
// Creates components and puts them in the JPanel on
// instantiation.
initComponents();
}
private JLabel nameOne;
private JLabel nameTwo;
private JLabel scoreOne;
private JLabel scoreTwo;
private void initComponents() {
// Set dimensions for JPanel.
this.setPreferredSize(new Dimension(200, 100));
this.setMinimumSize(new Dimension(200, 100));
// Label components.
nameOne = new JLabel(\"Player One\");
nameTwo = new JLabel(\"Player Two\");
scoreOne = new JLabel(\"0\");
scoreTwo = new JLabel(\"0\");
nameOne.setMinimumSize(new Dimension(100, 50));
nameTwo.setMinimumSize(new Dimension(100, 50));
scoreOne.setMinimumSize(new Dimension(100, 50));
scoreTwo.setMinimumSize(new Dimension(100, 50));
// JPanel uses FlowLayout layout manager, which adds components
// left to right in a row, until it runs out of space and creates
// a new row.
this.add(nameOne);
this.add(scoreOne);
this.add(nameTwo);
this.add(scoreTwo);
}
}
BoardUi.java
package userInterface;
import core.Constants;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
public class BoardUi extends JPanel {
private JButton[][] board;
private BoardListener listener;
public BoardUi() {
initComponents();
}
private voi.
1. Java Swing JTable Example and Tutorial
Written by Administrator
Saturday, 20 August 2011 14:29 - Last Updated Thursday, 01 September 2011 19:15
This Java Swing tutorial will demonstrate you how to use JTable companent in java swing and
also how you can use multi dimension Object array to populate JTable. As a container of the
Table I used Java Frame.
- Compile: javac SwingTable.java
- Run: java SwingTable
SwingTable.java
//copyrighted by topsourcecode.com import java.awt.Color; import javax.swing.*; public
class SwingTable{ public static void main(String[] args) { JFrame frame = new
JFrame("Populate Table with Data"); JPanel panel = new JPanel(); //Table
Parameters String col[] = {"Name","Country","Telephone"}; Object values[][] =
{{"John","USA","087689689"}, {"Sulaiman","Africa","989009980"},
{"Khan","India","098790998"}, {"Alex","Australia","989900098"},
{"Tsong","China","899866444"}}; JTable table = new JTable(values, col);
table.setBackground(Color.cyan); //Table Container Paramerters JScrollPane pane =
new JScrollPane(table); panel.add(pane); frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack();
frame.setVisible(true); } }
1 / 2
2. Java Swing JTable Example and Tutorial
Written by Administrator
Saturday, 20 August 2011 14:29 - Last Updated Thursday, 01 September 2011 19:15
Result
{loadposition myad}
2 / 2