Results 1 to 2 of 2
  1. #1
    masaMuscular is offline Member
    Join Date
    Sep 2012
    Posts
    1
    Rep Power
    0

    Exclamation 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 any body help me?
    The master jComboBox is named: combo1 and this need to change all the comboboxes named: comboBox on the jtable called tbl.


    Many thanks in advance!!!

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

    PHP Code:
    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;
    	  }
    	}

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

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

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. java change master volume
    By mastodont in forum Java Applets
    Replies: 4
    Last Post: 09-07-2011, 10:01 AM
  2. Showing different values in JComboBox inside JTable
    By d3n1s in forum Advanced Java
    Replies: 2
    Last Post: 05-02-2011, 09:25 PM
  3. Replies: 1
    Last Post: 12-29-2010, 03:45 PM
  4. Values change when they're not supposed to
    By m00nchile in forum New To Java
    Replies: 7
    Last Post: 05-15-2010, 05:16 PM
  5. have an eventlistener change values
    By klmdb in forum New To Java
    Replies: 1
    Last Post: 03-20-2009, 01:42 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •