This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
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 OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "JTable problem" Watch "JTable problem" New topic
Author

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.
Donna
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) {

setSize(500,500);
this.cl = cl;
cards = p;


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

};
String[] columnNames = {"stockId",
"name",
"availability"};
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 {
column.setPreferredWidth(50);
}
}

table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

table.setRowSelectionAllowed(false);


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){
}
}
 
 
subject: JTable problem