Ich habe eine JTable
, die als Bild unten sah. Wenn auf Zeile geklickt wird, möchte ich, dass alle ausgewählten Zeilenwerte angezeigt werden.Erhalten Sie ausgewählte Zeilen in 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
}
Wenn ich eine Zelle klicken, wird es suppose Anzeige alle Zeilenwert, sondern nur den Zellenwert angezeigt werden soll.
'Objekt selectedObject = (Objekt) jTable1.getModel(). GetValueAt (selectedRowIndex, selectedColumnIndex); System.out.println (selectedObject); 'schlägt mir vor, dass Sie die Zeile überhaupt nicht drucken – BoDidely
@BoDidely, was ist der richtige Weg? –
Sie wählen ein Objekt aus und drucken es. Sie müssen dies für jedes Objekt in der Zeile tun – BoDidely