Witam
Stworzyłam JTable, w niej umieszczam dane z tabeli oraclowej. Nastepnie chce wybrać wiersz z tabeli i wyświetlić w JTextArea na tej samej formatce. Czytam tutoriale i almanachy ale jakoś nie moge zrozumieć jak :-( Ustawiam setRowSelectionAllowed(true), setSelectionMode(ListSelectionModel.Single_selection)
i.... czy mam do tego wykorzystać Anchor Cell ? mozecie mi jakoś podpowiedziec co robić w jakiej kolejności zeby : a) wybrac wiersz b) zapisac go po kliknieciu myszka do pola textowego. Ponizej moje próby :-)Dzieki z gory za jakies podpowiedzi.
Pozdrawiam
public class dialog extends JFrame {
private JTextField P1;
private JTable table;
private DefaultTableModel model;
public static void main(String args[]) {
try {
dialog frame = new dialog();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
public dialog() {
super();
setBounds(100, 100, 529, 445);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
setTitle("Tabela");
final JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(20, 20, 460, 230);
getContentPane().add(scrollPane);
final JTable table = new JTable(50,2);
table.setColumnSelectionAllowed(false);
table.setRowSelectionAllowed(true);
model= new DefaultTableModel(50,2);
int vColIndex=0;
int vColIndex2=1;
table.getColumnModel().getColumn(vColIndex).setHeaderValue("Grupa");
table.getColumnModel().getColumn(vColIndex2).setHeaderValue("Numer inwent.");
table.getTableHeader().resizeAndRepaint();
table.setShowVerticalLines(true);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
scrollPane.setViewportView(table);
final JButton B2 = new JButton();
B2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
});
B2.setText("Anuluj");
B2.setBounds(254, 347, 95, 33);
getContentPane().add(B2);
P1 = new JTextField();
P1.setBounds(176, 267, 175, 25);
getContentPane().add(P1);
final JLabel L = new JLabel();
L.setText("Wybrany wiersz:");
L.setBounds(55, 270, 101, 20);
getContentPane().add(L);
String driver="jdbc:oracle:thin:";
String host=*******;
String port="1521";
String SERVICE_NAME="ZEUS";
String user=********;
String password=****;
String sql= "SELECT GRUPA,NR_INW FROM BADANIE";
Connection conn;
Statement stmt;
ResultSet rset;
String connString=driver+"@"+host+":"+port+":"+SERVICE_NAME;
try
{
int i=0;
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
conn=DriverManager.getConnection(connString,user,password);
stmt=conn.createStatement();
rset=stmt.executeQuery(sql);
while (rset.next())
{
table.setValueAt(rset.getString(1), i, 0);
table.setValueAt(rset.getString(2), i, 1);
i++;
if (i >= 50)
break;
}
rset.close();
stmt.close();
conn.close();
}
catch (SQLException e)
{
System.out.println(e.getMessage()+ "--> problem z polaczeniem");
}
final JButton B1 = new JButton();
B1.setText("Ok");
B1.setBounds(140, 346, 95, 33);
getContentPane().add(B1);
}
}