Foros del Web » Programación para mayores de 30 ;) » Java »

JcomboBox master, change the values of all jComboBoxes on a Jatable

Estas en el tema de JcomboBox master, change the values of all jComboBoxes on a Jatable en el foro de Java en Foros del Web. Hello Guys, I have one doubt, I'm using a single JcomboBox that must change all the Jcomboboxes of a jtable to the same value of ...
  #1 (permalink)  
Antiguo 01/09/2012, 17:35
 
Fecha de Ingreso: septiembre-2010
Mensajes: 350
Antigüedad: 13 años, 7 meses
Puntos: 9
Exclamación JcomboBox master, change the values of all jComboBoxes on a Jatable

Hello Guys,

I have one doubt, I'm using a single JcomboBox that must change all the Jcomboboxes of a jtable to the same value of the master jcombobox.
I have the attached code, but It doesnt´t work, I dont know where could be the issue :S, could anybody help me?
The master jComboBox is named: combo1 and this need to change all the comboboxes named: comboBox on the jtable called tbl.

You can answer this post in spanish, there's no problem with that.

You can find here the code: http://www.javaclub.com.mx/code/ComboBoxTable.java

Many thanks in advance!!!

Código PHP:
package comboBox;

import javax.swing.*;
import javax.swing.table.*;

import java.awt.Component;
import java.awt.event.*;

public class 
ComboBoxTable {
      private static 
JComboBox combo1;
  public static 
void main(String[] args) {
    try {
        
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (
Exception evt) {}
  
    
JFrame f = new JFrame("Combo Box Table");
    final 
JTable tbl = new JTable(new ComboBoxTableModel());
    
    
// Create the combo box editor
    
JComboBox comboBox = new JComboBox(ComboBoxTableModel.getValidStates());
    
comboBox.setEditable(true);
    
DefaultCellEditor editor = new DefaultCellEditor(comboBox);

    
// Assign the editor to the second column
    
TableColumnModel tcm tbl.getColumnModel();
    
tcm.getColumn(1).setCellEditor(editor);

    
// Set column widths
    
tcm.getColumn(0).setPreferredWidth(200);
    
tcm.getColumn(1).setPreferredWidth(100);

    
// Set row heighht
    
tbl.setRowHeight(20);

    
tbl.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    
tbl.setPreferredScrollableViewportSize(tbl.getPreferredSize());  

  

    
combo1=new JComboBox();
    
combo1.setBounds(350,10,80,20);
    
    
combo1.addItem("On order");
    
combo1.addItem("In stock");
    
combo1.addItem("Out of print");
    
combo1.setVisible(true);
    
    
ActionListener actionListener = new ActionListener() {
        public 
void actionPerformed(ActionEvent actionEvent) {
             
TableColumn col tbl.getColumnModel().getColumn(1);
                
col.setCellRenderer(new MyComboBoxRenderer(ComboBoxTableModel.getValidStates()));
        
        }
      };
      
combo1.addActionListener(actionListener);
    
f.add(combo1);
    
    
f.getContentPane().add(new JScrollPane(tbl), "Center");
    
f.pack();
    
f.setSize(500300);
    
f.addWindowListener(new WindowAdapter() {
      public 
void windowClosing(WindowEvent evt) {
        
System.exit(0);
      }
    });
    
f.setVisible(true);
  }
}

class 
ComboBoxTableModel extends AbstractTableModel {
  
// Implementation of TableModel interface 
  
public int getRowCount() {
    return 
data.length;
  }

  public 
int getColumnCount() {
    return 
COLUMN_COUNT;
  }

  public 
Object getValueAt(int rowint column) {
    return 
data[row][column];
  }

  public Class 
getColumnClass(int column) {
    return (
data[0][column]).getClass();
  }

  public 
String getColumnName(int column) {
    return 
columnNames[column];
  }

  public 
boolean isCellEditable(int rowint column) {
    return 
column == 1;
  }

  public 
void setValueAt(Object valueint rowint column) {
    if (
isValidValue(value)) {
      
data[row][column] = value;
      
fireTableRowsUpdated(rowrow);
    }
  }

  
// Extra public methods
  
public static String[] getValidStates() {
    return 
validStates;
  }
  



  
// Protected methods
  
protected boolean isValidValue(Object value) {
    if (
value instanceof String) {
      
String sValue = (String)value;

      for (
int i 0validStates.lengthi++) {
        if (
sValue.equals(validStates[i])) {
          return 
true;
        }
      }
    }

    return 
false;
  }

  
  
  protected static final 
int COLUMN_COUNT 2;
    
  protected static final 
String[] validStates = { 
    
"On order""In stock""Out of print"
  
};
  
  protected static final 
String[] validStatesMaster = { 
        
"On order""In stock""Out of print"
      
};

  protected 
Object[][] data = new Object[][] {
    { 
"Core Java Volume 1"validStates[0] },
    { 
"Core Java Volume 2"validStates[0] },
    { 
"Core Web Programming"validStates[0] },
    { 
"Core Visual Basic 5"validStates[0] },
    { 
"Core Java Foundation Classes"validStates[0] }
  };
  
  protected static final 
String[] columnNames = {
    
"Book Name""Status"
  
};

}

class 
MyComboBoxRenderer extends JComboBox implements TableCellRenderer {
      public 
MyComboBoxRenderer(String[] items) {
        
super(items);
      }

      public 
Component getTableCellRendererComponent(JTable tableObject valueboolean isSelected,
          
boolean hasFocusint rowint column) {
          
System.out.println("in");
        if (
isSelected) {
          
setForeground(table.getSelectionForeground());
          
super.setBackground(table.getSelectionBackground());
        } else {
          
setForeground(table.getForeground());
          
setBackground(table.getBackground());
        }
        
setSelectedItem(value);
        return 
this;
      }
    } 

Última edición por marco64yes; 01/09/2012 a las 21:02

Etiquetas: jcombobox, jtable
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 00:51.