programmation java

programmation java - Java - Programmation

Marsh Posté le 01-10-2010 à 14:46:07    

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();
            }
        });
    }
}

Reply

Marsh Posté le 01-10-2010 à 14:46:07   

Reply

Marsh Posté le 01-10-2010 à 15:12:21    

Et?


---------------
Si la vérité est découverte par quelqu'un d'autre,elle perd toujours un peu d'attrait
Reply

Marsh Posté le 01-10-2010 à 15:14:57    

je rien compris

Reply

Marsh Posté le 01-10-2010 à 15:21:49    

Non mais d'accord mais c'est quoi que tu nous demandes?


---------------
Si la vérité est découverte par quelqu'un d'autre,elle perd toujours un peu d'attrait
Reply

Marsh Posté le 01-10-2010 à 16:29:22    

De finir son TP

Reply

Marsh Posté le 01-10-2010 à 17:42:46    

Je cherche des tutoriels swing( l'évenement  sur les button)

Reply

Marsh Posté le 01-10-2010 à 18:03:04    

je ne cherche pas "de finir mon tp" mais d'avoir des idées car c'est la première fois que je manipule les interfaces swing
merci d'avance pour ton aide

Reply

Marsh Posté le 05-10-2010 à 00:51:10    

Reply

Marsh Posté le 05-10-2010 à 08:52:57    

MERCI POUR TON AIDE

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed