IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

AWT/Swing Java Discussion :

JComboBox dans JXTable avec Arraylist<object[]> comme model


Sujet :

AWT/Swing Java

  1. #1
    En attente de confirmation mail
    Inscrit en
    Février 2007
    Messages
    162
    Détails du profil
    Informations forums :
    Inscription : Février 2007
    Messages : 162
    Points : 72
    Points
    72
    Par défaut JComboBox dans JXTable avec Arraylist<object[]> comme model
    Bonjour a tous, j'ais regardé presque tout les topics sur la façon de mettre une Colonne de ma Jtable avec des Jcombobox y compris les tutos de sun, mais aucune n'a fonctionnée...
    Donc je fais appel a vous...

    Voici le code pour mettre une jcombobox que j'utilise mais qui ne fonctionne pas :
    (la Jcombobox ne s'affiche pas, seul le valeur 6 qui est définit dans mon arraylist s'affiche dans cette colonne).

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
     
    package myPack;
     
    import java.util.ArrayList;
    import javax.swing.DefaultCellEditor;
    import javax.swing.ImageIcon;
    import javax.swing.ListSelectionModel;
    import javax.swing.table.DefaultTableCellRenderer;
    import javax.swing.table.TableColumn;
     
    public class frame_search_info extends javax.swing.JFrame {
     
     
        public frame_search_info() {
     
            ...
    		//On initialize les données de la Arraylist<object[]>
    		initData();
    		//On initialise le model de la Jtable
            initModel();
    		...
        }
     
        private void initData(int type){
     
            ImageIcon myIconNext=new ImageIcon(System.getProperty("user.dir")+"/data/next.png");
     
            for(int z=0;z<10;z++){
    	        data_list.add(new Object[]{
    	                    myIconNext,
    	                    myIconNext,
    	                    "2",
    	                    "3",
    	                    "4",
    	                    "5",
    	                    "6"});
            }
     
        }
     
        private void initModel(){
     
            table_list.setModel(new SpecialTableModelObject(data_list, new String[] {
                                  "0",
                                  "1",
                                  "2",
                                  "3",
                                  "4",
                                  "5",
                                  "comboColumn"}));
     
            javax.swing.JComboBox myComboBox = new javax.swing.JComboBox();
            myComboBox.addItem("choice1");
            myComboBox.addItem("choice2");
            myComboBox.addItem("choice3");
     
     
     
            table_list.getColumnModel().getColumn(6).setCellEditor(new DefaultCellEditor(myComboBox));
     
        }
     
     
        private ArrayList<Object[]> data_list=new ArrayList<Object[]>(); 
    	....
        private org.jdesktop.swingx.JXTable table_list;
        // End of variables declaration                   
     
    }
    Et voici le code de ma SpecialTableModelObject

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
     
    package myPack;
     
    import java.util.ArrayList;
    import javax.swing.table.AbstractTableModel;
     
     
    public class SpecialTableModelObject extends AbstractTableModel {
     
    	private static final long serialVersionUID = 1;
            private String[] columnNames;
            private ArrayList<Object[]> data = new ArrayList<Object[]>();
     
     
            public SpecialTableModelObject( ArrayList<Object[]> dat, String[] cols){
            	super();
            	columnNames = cols;
            	data = dat;
            	}
     
            public SpecialTableModelObject( ArrayList<Object[]> dat){
            	super();
            	data = dat;
            	}
     
            public void misajour( ArrayList<Object[]> dat){
            	data = dat;
            	}
     
            public int getColumnCount() {
                return columnNames.length;
            	}
     
            public int getRowCount() {
                return data.size();
            	}
     
            public void setColumn(String[] column){
            	columnNames = column;
            	}
     
            public String getColumnName(int col) {
                return columnNames[col];
            	}
     
            public Object getValueAt(int row, int col) {
            	Object[] v = data.get(row);
                    return v[col];
     
            	}
     
            public Class<?> getColumnClass(int c) {
                return getValueAt(0, c).getClass();
            	}
     
            public boolean isCellEditable(int row, int col) {
            	return false;
            	}
     
            public void setValueAt(String value, int row, int col) {
            	Object[] v = data.get(row);
            	v[col] = value;
                fireTableCellUpdated(row, col);
            	}
     
        }

    Je vous remercie d'avance pour toute l'aide que vous pourrez m'apporter

  2. #2
    En attente de confirmation mail
    Inscrit en
    Février 2007
    Messages
    162
    Détails du profil
    Informations forums :
    Inscription : Février 2007
    Messages : 162
    Points : 72
    Points
    72
    Par défaut
    if(reponse==0 && wait==1day)System.out.println("Help");

  3. #3
    En attente de confirmation mail
    Inscrit en
    Février 2007
    Messages
    162
    Détails du profil
    Informations forums :
    Inscription : Février 2007
    Messages : 162
    Points : 72
    Points
    72
    Par défaut
    J'ai changé ça :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
     
        private void initModel(){
     
            table_list.setModel(new SpecialTableModelObject(data_list, new String[] {
                                  "0",
                                  "1",
                                  "2",
                                  "3",
                                  "4",
                                  "5",
                                  "comboColumn"}));
     
            javax.swing.JComboBox myComboBox = new javax.swing.JComboBox();
            myComboBox.addItem("choice1");
            myComboBox.addItem("choice2");
            myComboBox.addItem("choice3");
     
     
     
            table_list.getColumnModel().getColumn(6).setCellEditor(new DefaultCellEditor(myComboBox));
     
        }

    par ça :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
     
        private void initModel(){
     
            table_list.setModel(new SpecialTableModelObject(data_list, new String[] {
                                  "0",
                                  "1",
                                  "2",
                                  "3",
                                  "4",
                                  "5",
                                  "comboColumn"}));
     
     
            String[] values = new String[]{"item1", "item2", "item3"}; 
     
            TableColumn col = table_list.getColumnModel().getColumn(6);
            col.setCellEditor(new MyComboBoxEditor(values));
            col.setCellRenderer(new MyComboBoxRenderer(values));
     
     
        }
    avec la classe MyComboBoxEditor :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
     
    public class MyComboBoxEditor extends DefaultCellEditor {
            public MyComboBoxEditor(String[] items) {
                super(new JComboBox(items));
            }
        }

    Maintenant, je vois bien des Jcombobox dans ma colonne, mais quand je clique dessus rien ne se passe, la liste ne se déroule pas ...


    Comment faire ?

    PS : je suis avec une JXtable, mais je ne pense pas que ça change grand chose ...

    Merci d'avance

  4. #4
    En attente de confirmation mail
    Inscrit en
    Février 2007
    Messages
    162
    Détails du profil
    Informations forums :
    Inscription : Février 2007
    Messages : 162
    Points : 72
    Points
    72
    Par défaut
    J'ai essayé de changer mon data_list fait avec un arraylist par un data_list fait avec un object[][] comme dans le tuto de sun ici :

    http://java.sun.com/docs/books/tutor....html#combobox

    et cela marche donc ça viens bien du fait que j'utilise un arraylist...svp voyez vous ce qui cloche ?

  5. #5
    Membre émérite
    Avatar de xavlours
    Inscrit en
    Février 2004
    Messages
    1 832
    Détails du profil
    Informations forums :
    Inscription : Février 2004
    Messages : 1 832
    Points : 2 410
    Points
    2 410
    Par défaut
    Bonjour,

    là à froid je sèche un peu, mais d'après ce que j'ai vu de ton code, un DefaultTableModel et un DefaultCellEditor fonctionneront très bien. Je ne me suis pas plongé dans les détails de ton code, mais il y a des chances pour que le problème vienne de ton TableModel.

    Notamment, isCellEditable renvoyant toujours false, aucun CellEditor ne sera appelé.

  6. #6
    En attente de confirmation mail
    Inscrit en
    Février 2007
    Messages
    162
    Détails du profil
    Informations forums :
    Inscription : Février 2007
    Messages : 162
    Points : 72
    Points
    72
    Par défaut
    En fait maintenant j'ais réussit tout simplement en déplaçant mes données dans un object[][] et pas dans un arraylist.

    mais maintenant le probleme c'est que je n'arrive pas a trouver a appliquer des jcombobox différentes dans chaque cellules (avec des possiblités de choix différentes)

  7. #7
    Membre émérite
    Avatar de xavlours
    Inscrit en
    Février 2004
    Messages
    1 832
    Détails du profil
    Informations forums :
    Inscription : Février 2004
    Messages : 1 832
    Points : 2 410
    Points
    2 410
    Par défaut
    Dans ce cas, il faut passer par un CellEditor personnalisé. Dans ce genre :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    public class MyEditor extends DefaultCellEditor {
     
      private JComboBox cbox = new JComboBox();
     
      private Collection<ComboBoxModel> listModels;
     
      public MyEditor() {
        super(cbox);
        // init listModels;
      }
     
      private ComboBoxModel getModelForCell(int row, int col) {
        // à toi de voir
        // on peut aussi ajouter la JTable en paramètre afin d'accéder
        // au TableModel si c'est utile, ou la valeur de la case
      }
     
      public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        cbox.setModel(getModelForCell(row, column));
        return super.getTableCellEditorComponent(table, value, isSelected, row, column);
      }
    }
    Il se peut qu'étendre AbstractCellEditor soit plus approprié, je ne sais pas trop.

Discussions similaires

  1. Réponses: 0
    Dernier message: 30/06/2014, 12h10
  2. récupérer une variable dans SWF avec swf object
    Par yerom dans le forum ActionScript 3
    Réponses: 0
    Dernier message: 24/09/2009, 17h02
  3. Probleme avec la balise <object> dans une page jsp
    Par casawi dans le forum Struts 1
    Réponses: 0
    Dernier message: 18/06/2009, 20h59
  4. JComboBox dans une JXTable
    Par Hyperion99 dans le forum AWT/Swing
    Réponses: 7
    Dernier message: 15/05/2008, 16h58
  5. [GifDecoder] marche pas dans applet avec IE
    Par formentor dans le forum Applets
    Réponses: 2
    Dernier message: 06/05/2003, 10h43

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo