elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Tutorial básico de Quickjs


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  Java
| | | |-+  JcomboBox master, change the values of all jComboBoxes on a Jatable
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: JcomboBox master, change the values of all jComboBoxes on a Jatable  (Leído 1,601 veces)
masaMuscular

Desconectado Desconectado

Mensajes: 1


Ver Perfil
JcomboBox master, change the values of all jComboBoxes on a Jatable
« en: 2 Septiembre 2012, 06:06 am »

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!!!

Código:
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;
  }
}


En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Uso de jTable y jComboBox
Java
garçon 3 10,192 Último mensaje 24 Junio 2009, 22:47 pm
por garçon
Ayuda JComboBox...
Java
visualfree 1 2,847 Último mensaje 17 Junio 2011, 02:07 am
por klaine
JComboBox en un JTable
Java
Xedrox 1 3,732 Último mensaje 22 Agosto 2011, 17:48 pm
por Leyer
Actualizar JComboBox
Java
reylagarto19 1 4,179 Último mensaje 26 Agosto 2012, 01:19 am
por sapito169
Values does not fall within the expected range, a que se debe?
Programación General
Blakmaller 2 1,970 Último mensaje 29 Julio 2014, 14:45 pm
por Blakmaller
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines