我有JTable
,如下图所示。单击行时,我希望它显示所有选定的行值。
viewMovie
public class viewMovie extends javax.swing.JFrame {
public viewMovie() throws Exception {
initComponents();
populateJTable();
}
// create a methode to populate data Into JTable from Mysql database And Displaying Picture
public void populateJTable() throws Exception{
MyQuery mq = new MyQuery();
ArrayList<Movie> list = mq.BindTable();
String[] columnName = {"Title","Date","Time","Price","Hall","Description","Image"};
Object[][] rows = new Object[list.size()][8];
for(int i = 0; i < list.size(); i++){
//rows[i][0] = list.get(i).getID();
rows[i][0] = list.get(i).getTitle();
rows[i][1] = list.get(i).getDate();
rows[i][2] = list.get(i).getTime();
rows[i][3] = list.get(i).getPrice();
rows[i][4] = list.get(i).getHall();
rows[i][5] = list.get(i).getDescription();
if(list.get(i).getImage() != null){
ImageIcon image = new ImageIcon(new ImageIcon(list.get(i).getImage()).getImage()
.getScaledInstance(150, 120, Image.SCALE_SMOOTH) );
rows[i][6] = image;
}
else{
rows[i][6] = null;
}
}
TheModel model = new TheModel(rows, columnName);
jTable1.setModel(model);
jTable1.setRowHeight(120);
jTable1.getColumnModel().getColumn(5).setPreferredWidth(150);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
ArrayList<Movie> list;
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jLabel1 = new javax.swing.JLabel();
back= new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jTable1.addMouseListener(new java.awt.event.MouseAdapter() { // row is clicked
public void mouseClicked(java.awt.event.MouseEvent evt) {
int selectedRowIndex = jTable1.getSelectedRow();
int selectedColumnIndex = jTable1.getSelectedColumn();
Object selectedObject = (Object) jTable1.getModel().getValueAt(selectedRowIndex, selectedColumnIndex);
System.out.println(selectedObject);
}
});
jScrollPane1.setViewportView(jTable1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1)
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addGap(228, 228, 228)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 291, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(261, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 272, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 194, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(viewMovie.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(viewMovie.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(viewMovie.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(viewMovie.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new viewMovie().setVisible(true);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
private javax.swing.JButton back;
// End of variables declaration
}
当我单击一个单元格时,它会显示所有行值,但它只显示单元格值。
答案 0 :(得分:2)
一种选择是打印所选行的数据向量:
DefaultTableModel model = (DefaultTableModel)jTable1.getModel();
System.out.println(model.getDataVector().get(selectedRowIndex));
或者您可以一个接一个地打印行的单元格值:
TableModel model = jTable1.getModel();
for(int i = 0; i < model.getColumnCount(); i++){
if(i > 0){
System.out.println(", ");
}
System.out.println(model.getValueAt(selectedRowIndex, selectedColumnIndex));
}
System.out.println();
答案 1 :(得分:1)
不是答案(JHead已经为您提供了示例代码),只是一个很长的评论。
点击行时
无论用户是使用鼠标还是键盘选择行,都应设计合适的GUI。
所以你应该使用ListSelectionListener
。阅读How to Write a ListSelectionListener上Swing教程中的部分以获取更多信息和示例。本教程使用JList,但JTable的概念也相同。
另外,如果选择了多行,该怎么办?
我希望它显示所有选定的行值。
有什么意义?所有数据都已显示在表格中。