1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
package test;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import javax.swing.JLabel;
public class TestJTable extends JFrame
{
private static final long serialVersionUID = 1L;
private JScrollPane scroll;
private JTable MaTable;
String[] columnNames = {"Séq.", "Artère", "Adresse", "N°", "Bis/Te", "Quartier", "Année" };
Object[][] data = {{"", "", "","", "", "", ""},{"", "", "","", "", "", ""} , {"", "", "","", "", "", ""}, {"", "", "","", "", "", ""}}; // 4 lignes
private DefaultTableModel model = new DefaultTableModel(data, columnNames);
int row=0, col =0;
private JTextField affichage;
public TestJTable()
{
JPanel panneau = new JPanel();
this.setSize(533, 339);
panneau.setBounds(0, 0, 405, 202);
getContentPane().add(panneau);
panneau.setLayout(null);
MaTable = new JTable(model);
scroll = new JScrollPane(MaTable);
scroll.setBounds(0, 0, 527, 93);
panneau.add( scroll);
MaTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
MaTable.getColumnModel().getColumn(0).setPreferredWidth(38);
MaTable.getColumnModel().getColumn(1).setPreferredWidth(80);
MaTable.getColumnModel().getColumn(2).setPreferredWidth(140);
MaTable.getColumnModel().getColumn(3).setPreferredWidth(40);
MaTable.getColumnModel().getColumn(4).setPreferredWidth(54);
MaTable.getColumnModel().getColumn(5).setPreferredWidth(100);
MaTable.getColumnModel().getColumn(6).setPreferredWidth(54);
MaTable.setBackground(Color.orange);
MaTable.setFont(new Font("Times New Roman", Font.BOLD,10));
JLabel msg = new JLabel(" ");
msg.setForeground(Color.RED);
msg.setBounds(29, 224, 423, 21);
panneau.add(msg);
affichage = new JTextField();
affichage.setBounds(139, 183, 229, 30);
panneau.add(affichage);
affichage.setColumns(10);
JButton Lire = new JButton("Case suivante");
Lire.setBounds(170, 250, 147, 30);
panneau.add(Lire);
Lire.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
model.fireTableCellUpdated(row, col); // n'a aucun effet
String wk = (String) MaTable.getValueAt(row,col);
affichage.setText(wk);
msg.setText("Ligne : " + (row+1) + " Colonne : " + (col+1));
col++;
if (col >6) {col=0; row++;}
if (row >3 )
{
msg.setText("Toutes les cases sont lues. On repart à zéro.");
row=0; col =0;
}
}
});
setVisible(true);
setLocationRelativeTo(null);
}
public static void main(String args[]) throws Exception
{ new TestJTable(); }
} |