işin garip tarafı new'den JFrame Form değilde Java class secip jTable,jFrame,jLabel[] i manuel oluşturup yerleştirince aynı kod hatasız çalışıyor
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class JTableListSelectionListener {
private static JTextField text[] = new JTextField[4];
public static void main(String[] a) {
JFrame frame = new JFrame();
JTextField j1=new JTextField();
JTextField j2=new JTextField();
JTextField j3=new JTextField();
JTextField j4=new JTextField();
text[0]=j1;text[1]=j2;text[2]=j3;text[3]=j4;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JTable table;
String[] columnTitles = { "A", "B", "C", "D" };
Object[][] rowData = { { "11", "12", "13", "14" }, { "21", "22", "23", "24" },
{ "31", "32", "33", "34" }, { "41", "42", "44", "44" } };
table = new JTable(rowData, columnTitles){
public boolean isCellEditable(int row,int column){
return false;
}
};
table.setCellSelectionEnabled(false);
table.setRowSelectionAllowed(true);
ListSelectionModel cellSelectionModel = table.getSelectionModel();
cellSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
cellSelectionModel.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
String selectedData = null;
int selectedRow = table.getSelectedRow();
int selectedColumns = table.getSelectedColumn();
selectedData = (String) table.getValueAt(selectedRow, selectedColumns);
for (int i = 0; i < text.length; i++) {
text[i].setText(table.getValueAt(selectedRow,i).toString());
}
System.out.println("Selected: " + selectedData);
}
});
table.setCellSelectionEnabled(false);
JPanel jp=new JPanel();
jp.setLayout(new GridLayout(1,4));
frame.add(new JScrollPane(table),BorderLayout.CENTER);
jp.add(j1);
jp.add(j2);
jp.add(j3);
jp.add(j4);
frame.add(jp,BorderLayout.SOUTH);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
işte bunu prjede olusturdugum JFrame de kullanamıyorum
bitiyo lan!
|