Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

HimorriveL

JTable e JComboBox

Recommended Posts

Estou usando NetBeans 7.0.1 e estou tentando colocar um JComboBox dentro de uma das linhas da JTable porem está dando um erro,o código do JTable está assim:

JComboBox teste = new JComboBox();
       teste.addItem ("01");
       teste.addItem ("02");
       teste.addItem ("03");
jTable1 = new javax.swing.JTable();

jTable1.setModel(new javax.swing.table.DefaultTableModel(
   new Object [][] {
       {teste, null, null, null},
       {null, null, null, null},
       {null, null, null, null},
       {null, null, null, null}
   },
   new String [] {
       "Title 1", "Title 2", "Title 3", "Title 4"
   }
));

jScrollPane1.setViewportView(jTable1);

O que está faltando?

Compartilhar este post


Link para o post
Compartilhar em outros sites

É Basicamente isso mesmo ..

 

package application;

import java.awt.Component;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;


public class Application {

      public static void main ( String [ ] args ) {
             JTable table = new JTable ( ) ;
             DefaultTableModel model = ( DefaultTableModel ) table.getModel ( ) ;

             model.addColumn ( "Coluna 1" ) ;
             model.addColumn ( "Coluna 2" ) ;

             model.addRow ( new Object [ ] { null , "Ma Me Mi Mo Mu" } ) ; 


             String [ ] values = new String [ ] { "Valor 1" , "Valor 2" } ;
             TableColumn column = table.getColumnModel ( ).getColumn ( 0 ) ;
             column.setCellEditor ( new CellEditor ( values ) );
             column.setCellRenderer ( new CellRenderer ( values ) ) ;

             table.setModel ( model ) ; 

             // ...
      }

}

class CellEditor extends DefaultCellEditor {

      public CellEditor ( String [ ] options ) {
             super ( new JComboBox ( options ) ) ;
      }

}

class CellRenderer extends JComboBox implements TableCellRenderer {

      public CellRenderer ( String[] items ) {
             super ( items ) ;
      }

      @Override
      public Component getTableCellRendererComponent
      ( JTable table , Object value , boolean isSelected , boolean hasFocus , int row , int column ) {
             if ( isSelected ) {
                    setForeground ( table.getSelectionForeground ( ) );
                    super.setBackground ( table.getSelectionBackground ( ) );
             } else {
                    setForeground ( table.getForeground ( ) );
                    setBackground ( table.getBackground ( ) );
             }
             setSelectedItem ( value );
             return this;
      }
}

 

Depois só exibir a tabela ..

JFrame frame = new JFrame ( ) ;
frame.getContentPane ( ).add ( new JScrollPane ( table ) ) ;
frame.pack ( );

frame.setVisible ( true ) ;
// ...             

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.