• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable problem

 
Donna Harrington
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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){
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic