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
| /*
* TableDemo.java
*
*/
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
public class TableDemo extends JFrame {
private JButton btAdd;
private JComboBox combo;
private JPanel controls;
private JTable table;
private JTextField tf1;
private JTextField tf2;
private JTextField tf3;
private final Controleur con;
private final Vector<Object> data;
private final Vector<String> cols;
private final DefaultTableModel model;
public TableDemo() {
super("TableDemo");
initComponents();
con = new Controleur();
data = con.table1();
cols = con.nomcolonnes();
model = new DefaultTableModel(data, cols);
table.setModel(model);
}
private void initComponents() {
table = new JTable();
controls = new JPanel();
combo = new JComboBox();
tf1 = new JTextField(10);
tf2 = new JTextField(10);
tf3 = new JTextField(10);
btAdd = new JButton("Add");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
combo.setModel(new DefaultComboBoxModel(new String[]{"Item 1", "Item 2", "Item 3", "Item 4"}));
controls.add(combo);
tf1.setText("1.1");
controls.add(tf1);
tf2.setText("1.2");
controls.add(tf2);
tf3.setText("1.3");
controls.add(tf3);
btAdd.setFocusable(false);
btAdd.setHorizontalTextPosition(SwingConstants.CENTER);
btAdd.setVerticalTextPosition(SwingConstants.BOTTOM);
btAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
controls.add(btAdd);
getContentPane().add(controls, BorderLayout.PAGE_END);
setSize(600, 300);
setLocationRelativeTo(null);
}
private void jButton2ActionPerformed(ActionEvent evt) {
double surface = 555;
Vector<Object> rowData = con.addobj(combo.getSelectedItem().toString(),
Double.parseDouble(tf1.getText()),
Double.parseDouble(tf2.getText()),
Double.parseDouble(tf3.getText()),
surface);
data.add(rowData);
model.setDataVector(data, cols);
}
public static void main(final String[] args) {
Runnable gui = new Runnable() {
public void run() {
new TableDemo().setVisible(true);
}
};
//GUI must start on EventDispatchThread:
SwingUtilities.invokeLater(gui);
}
}
class Controleur {
public static Vector<Object> f;
public Vector<String> nomcolonnes() {
Vector<String> nomcolonnes = new Vector<String>();
nomcolonnes.add("forme Geo.");
nomcolonnes.add("longueur");
nomcolonnes.add("largeur");
nomcolonnes.add("hauteur");
nomcolonnes.add("surface");
return nomcolonnes;
}
public Vector<Object> table1() {
Vector<Object> v1 = new Vector<Object>();
v1.add(f);
return v1;
}
public Vector<Object> addobj(String forme, double longueur, double largeur, double hauteur, double surface) {
f = new Vector<Object>();
f.add(forme);
f.add(longueur);
f.add(largeur);
f.add(hauteur);
f.add(surface);
return f;
}
} |
Partager