• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JTable problem

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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){
}
}
 
Villains always have antidotes. They're funny that way. Here's an antidote disguised as a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic