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

Iniciado por masaMuscular, 2 Septiembre 2012, 06:06 AM

0 Miembros y 1 Visitante están viendo este tema.

masaMuscular

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.

http://www.javaclub.com.mx/code/ComboBoxTable.java
http://www.javaclub.com.mx/code/ComboBoxTable.txt

Many thanks in advance!!!

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(500, 300);
    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 row, int 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 row, int column) {
    return column == 1;
  }

  public void setValueAt(Object value, int row, int column) {
    if (isValidValue(value)) {
      data[row][column] = value;
      fireTableRowsUpdated(row, row);
    }
  }

  // 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 = 0; i < validStates.length; i++) {
        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 table, Object value, boolean isSelected,
      boolean hasFocus, int row, int 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;
  }
}