svp ,comment ajouter un button qui permet d'ajouter une ligne a un tableau swing
je suis débutante
voici mon code:
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import java.awt.Dimension;
import java.awt.*;
import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.*;
import javax.swing.table.TableModel;
import javax.swing.event.*;
import javax.swing.table.TableModel;
import javax.swing.event.*;
import java.awt.Dimension;
import java.awt.event.*;
import java.lang.*;
public class hh extends JPanel implements TableModelListener {
private boolean DEBUG = false;
public hh() {
super(new FlowLayout());
JTable table = new JTable(new MyTableModel());
table.setPreferredScrollableViewportSize(new Dimension(800, 700));
table.setFillsViewportHeight(true);
table.getModel().addTableModelListener(this);
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
//Add the scroll pane to this panel.
add(scrollPane);
//JLABEL
JLabel REMPLIR = new JLabel("Remplir les valeurs " );
add(REMPLIR);
//LES BUTTONS
JButton CALCULER;
CALCULER=new JButton("CALCULER" );
add(CALCULER);
JButton Ajouter;
Ajouter=new JButton("AJOUTER" );
add(Ajouter);
}
public void tableChanged(TableModelEvent e) {
int row = e.getFirstRow();
int column = e.getColumn();
TableModel model = (TableModel)e.getSource();
String columnName = model.getColumnName(column);
Object data = model.getValueAt(row, column);
}
class MyTableModel extends AbstractTableModel {
private String[] columnNames = {"V", "R", "Cl", "T","F", "C", "N", "C", "F", "T", "P", "C"};
private Object[][] data = {{" 1", "", "", "","","", "", "", "","","","",""},
{" 2", "", "", "","","", "", "", "","","","",""},
{" 3", "", "", "","","", "", "", "","","","",""}};
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public boolean isCellEditable(int row, int col) {
if (col < 1) {
return false;
} else {
return true;
}
}
public void setValueAt(Object value, int row, int col) {
if (DEBUG) {
System.out.println("Setting value at " + row + "," + col
+ " to " + value
+ " (an instance of "
+ value.getClass() + " )" );
}
data[row][col] = value;
fireTableCellUpdated(row, col);
if (DEBUG) {
System.out.println("New value of data:" );
printDebugData();
}
}
private void printDebugData() {
int numRows = getRowCount();
int numCols = getColumnCount();
for (int i=0; i < numRows; i++) {
System.out.print(" row " + i + ":" );
for (int j=0; j < numCols; j++) {
System.out.print(" " + data[i][j]);
}
System.out.println();
}
System.out.println("SSS" );
}
}
// Create the GUI and show it. For thread safety,
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("Outil versionnement" );
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
hh newContentPane = new hh();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}