jQuery in Action, 3rd edition
The moose likes Java in General and the fly likes JTable problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "JTable problem" Watch "JTable problem" New topic

JTable problem

Donna Harrington
Ranch Hand

Joined: Aug 08, 2003
Posts: 31
Hey people,
I have a problem with my JTable. I have an application which is connected to a database. I dont know how to get my data from my database and how to display it in the Object array in my JTable.
I hope somebody can help me,
Thanx for taking the time to read this.
Here is my code for my JTable:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
public class displayAvailVids extends JPanel implements ActionListener {

boolean DEBUG = true;
JPanel p1, custOpt, cards, box;
CardLayout cl;
JButton proceed;

public displayAvailVids(CardLayout cl,JPanel p) {

this.cl = cl;
cards = p;

box = new JPanel();
p1 = new JPanel();
Object[][] data = {
{" "," ", " ",},
{" ", " "," "},
{" "," "," "},
{" "," "," "},

String[] columnNames = {"stockId",
JTable table = new JTable(data, columnNames);

//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
table.setPreferredScrollableViewportSize(new Dimension(600, 100));
p1.add(scrollPane, BorderLayout.CENTER);

// making one of the columns bigger
TableColumn column = null;

for (int i = 0; i < 3; i++) {
column = table.getColumnModel().getColumn(i);
if (i == 1) {
column.setPreferredWidth(100); //name column is bigger
} else {



ListSelectionModel rowSM = table.getSelectionModel();
rowSM.addListSelectionListener(new SelectionHandler(table));
ListSelectionModel colSM = table.getColumnModel().getSelectionModel();
colSM.addListSelectionListener(new SelectionHandler(table));


public class SelectionHandler implements ListSelectionListener
JTable table;
TableModel model;
ListSelectionModel lsm;
int numRows;
int numCols;

public SelectionHandler(JTable theTable){
table = theTable;
numRows = table.getRowCount();
numCols = table.getColumnCount();
public void valueChanged(ListSelectionEvent e) {

if (e.getValueIsAdjusting()) return;

lsm = (ListSelectionModel)e.getSource();
int selectedRow = lsm.getMinSelectionIndex();
model = table.getModel();
System.out.println(model.getValueAt(selectedRow, 0));

public void actionPerformed(ActionEvent e){
I agree. Here's the link: http://aspose.com/file-tools
subject: JTable problem
It's not a secret anymore!