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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
| import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Stream;
import javax.swing.JFrame;
import javax.swing.JLabel;
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 JTableSelectionExemple {
public static void main(String[] args) {
JFrame frame = new JFrame("Exemple");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String[] cols = {"col1","col2","col3"};
JTable table = createTable(cols);
JPanel tablePanel = new JPanel(new GridLayout());
tablePanel.add(new JScrollPane(table));
table.setAutoCreateRowSorter(true);
Form form = new Form(cols);
// sélection et remplissage du formulaire
table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if ( !e.getValueIsAdjusting() ) {
int selectedRow = table.getSelectedRow();
fillForm(table,selectedRow,form);
}
}
});
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx=0;
gbc.gridy=GridBagConstraints.RELATIVE;
gbc.fill=GridBagConstraints.BOTH;
gbc.weighty=1;
gbc.weightx=1;
panel.add(tablePanel,gbc);
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.weighty=0;
panel.add(form,gbc);
frame.add(panel);
frame.setSize(600, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
protected static void fillForm(JTable table, int selectedRow, Form form) {
if ( selectedRow<0 ) {
form.reset();
}
else {
for(int i=0; i<table.getColumnCount(); i++) {
int column = table.convertColumnIndexToView(i);
form.setText(i, String.valueOf(table.getValueAt(selectedRow,column)));
}
}
}
private static JTable createTable(String...cols) {
Object[][] data = Stream.generate(()->
Stream.generate(()->ThreadLocalRandom.current().nextInt(100)).limit(cols.length).toArray()
).limit(100).toArray(Object[][]::new);
return new JTable(data,cols);
}
public static class Form extends JPanel {
private final JTextField[] fields;
public Form(String...fields) {
super(new GridBagLayout());
this.fields = new JTextField[fields.length];
GridBagConstraints gbclabel = new GridBagConstraints();
gbclabel.gridx=0;
gbclabel.gridy = GridBagConstraints.RELATIVE;
gbclabel.anchor = GridBagConstraints.BASELINE_TRAILING;
gbclabel.insets = new Insets(5, 5, 5, 5);
GridBagConstraints gbcfield = new GridBagConstraints();
gbcfield.gridx=1;
gbcfield.gridy = GridBagConstraints.RELATIVE;
gbcfield.fill = GridBagConstraints.BOTH;
gbcfield.weightx = 1;
gbcfield.insets = new Insets(5, 0, 5, 5);
for(int i=0;i<fields.length; i++) {
JTextField textfield=new JTextField();
JLabel label = new JLabel(fields[i]+" :");
label.setLabelFor(textfield);
add(label, gbclabel);
add(textfield, gbcfield);
this.fields[i]=textfield;
}
}
public void reset() {
for(JTextField field : fields) {
field.setText("");
}
}
public void setText(int n,String text) {
fields[n].setText(text);
}
public String getText(int n) {
return fields[n].getText();
}
}
} |
Partager