1

cuando lo instancia solo ,funcionan las teclas de desplazamiento pero cuando lo incluyo en un jtable para su utilización dejan de funcionar alguna idea como hacerlo funcionar

    TableColumn colWithCmb = jTable1.getColumnModel().getColumn(1);
    colWithCmb.setCellEditor(new ComboBoxCellEditor(combo));



    public void Run1(){
      List<List<?>> tableData = new ArrayList<List<?>>();

      for (productos productos : consulta.productos()) {
        tableData.add(new ArrayList<String>(  
        Arrays.asList( productos.getCodigo(),productos.getDescripcion(), productos.getPrecio_Inc())));
      }


      String[] columns = new String[]{"Codigo", "Descripcion", "Precio"};
      int[] widths = new int[]{100, 200, 150};



    combo = new DetailedComboBox(columns, widths, 1);
    combo.setTableData(tableData);
    combo.setSelectedIndex(-1);
    combo.setPopupAlignment(DetailedComboBox.Alignment.LEFT);
    combo.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        showDetails();

         int selectedRow1= 0;
               selectedRow1= jTable1.getSelectedRow();     

                //setValueAt(Object value, int row, int column)

      }
    });
    combo.addItemListener(new java.awt.event.ItemListener() {
    public void itemStateChanged(java.awt.event.ItemEvent evt) {
        jComboBox1ItemStateChanged(evt);

         if (evt.getStateChange() == java.awt.event.ItemEvent.SELECTED) {
//            productos brg =  consulta.productosdescrip(jComboBox1.getSelectedItem().toString());
//            List<productos> brg = consulta.productosdescrip(jComboBox1.getSelectedItem().toString());
            List<productocompra> brg = consulta.descripboxcompra(combo.getSelectedItem().toString());

            if (brg != null) {
                int selectedRow = jTable1.getSelectedRow();
                String codigo =brg.get(0).getCodigo().toString();
                String precio =brg.get(0).getPrecio_Inc().toString();
                String detalle = brg.get(0).getDescripcion().toString();
                  String unidad =brg.get(0).getUnidad().toString();
                //setValueAt(Object value, int row, int column)
                jTable1.setValueAt(codigo, selectedRow, 0);
                jTable1.setValueAt(precio, selectedRow, 3);
                jTable1.setValueAt(unidad, selectedRow, 2);
                  jTable1.setValueAt(detalle, selectedRow, 1);
                }
        }

    }
});

    AutoCompleteDecorator.decorate(combo);
}

 private void showDetails() {
    List<? extends Object> rowData = combo.getSelectedRow();
 }

este es el driver

package prueb;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.plaf.basic.*;
import javax.swing.plaf.metal.*;
import javax.swing.table.*;

/**
 * A JComboBox that has a JTable as a drop-down instead of a JList
 */
public class DetailedComboBox extends JComboBox

{
  public static enum Alignment {LEFT, RIGHT}

  private List<List<? extends Object>> tableData;
  private String[] columnNames;
  private int[] columnWidths;
  private int displayColumn;
  private Alignment popupAlignment = Alignment.LEFT;

  /**
   * Construct a TableComboBox object
   */
  public DetailedComboBox(String[] colNames, int[] colWidths,
                          int displayColumnIndex)
  {
    super();
    this.columnNames = colNames;
    this.columnWidths = colWidths;
    this.displayColumn = displayColumnIndex;

    setUI(new TableComboBoxUI());
    setEditable(false);
  }

  /**
   * Set the type of alignment for the popup table
   */
  public void setPopupAlignment(Alignment alignment)
  {
    popupAlignment = alignment;
  }

  /**
   * Populate the combobox and drop-down table with the supplied data.
   * If the supplied List is non-null and non-empty, it is assumed that
   * the data is a List of Lists to be used for the drop-down table.
   * The combobox is also populated with the column data from the
   * column defined by <code>displayColumn</code>.
   */
  public void setTableData(List<List<? extends Object>> tableData)
  {
    this.tableData = (tableData == null ?
        new ArrayList<List<? extends Object>>() : tableData);

    // even though the incoming data is for the table, we must also
    // populate the combobox's data, so first clear the previous list.
    removeAllItems();

    // then load the combobox with data from the appropriate column
    Iterator<List<? extends Object>> iter = this.tableData.iterator();
    while (iter.hasNext())
    {
      List<? extends Object> rowData = iter.next();
      addItem(rowData.get(displayColumn));
    }
  }

  public List<? extends Object> getSelectedRow()
  {
    return tableData.get(getSelectedIndex());
  }

  /**
   * The handler for the combobox's components
   */
  private class TableComboBoxUI extends MetalComboBoxUI
  {
    /**
     * Create a popup component for the ComboBox
     */
    @Override
    protected ComboPopup createPopup()
    {
      return new TableComboPopup(comboBox, this);
    }

    /**
     * Return the JList component
     */
    public JList getList()
    {
      return listBox;
    }
  }

  /**
   * The drop-down of the combobox, which is a JTable instead of a JList.
   */
  private class TableComboPopup extends BasicComboPopup
      implements ListSelectionListener, ItemListener
  {
    private final JTable table;

    private TableComboBoxUI comboBoxUI;
    private PopupTableModel tableModel;
    private JScrollPane scroll;
//    private JList list = new JList();
//    private ListSelectionListener selectionListener;
//    private ItemListener itemListener;

    /**
     * Construct a popup component that's a table
     */
    public TableComboPopup(JComboBox combo, TableComboBoxUI ui)
    {
      super(combo);
      this.comboBoxUI = ui;

      tableModel = new PopupTableModel();
      table = new JTable(tableModel);
      table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
      table.getTableHeader().setReorderingAllowed(false);

      TableColumnModel tableColumnModel = table.getColumnModel();
      tableColumnModel.setColumnSelectionAllowed(false);

      for (int index = 0; index < table.getColumnCount(); index++)
      {
        TableColumn tableColumn = tableColumnModel.getColumn(index);
        tableColumn.setPreferredWidth(columnWidths[index]);
      }

      scroll = new JScrollPane(table);
      scroll.setHorizontalScrollBarPolicy(
          JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
      scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

      ListSelectionModel selectionModel = table.getSelectionModel();
      selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
      selectionModel.addListSelectionListener(this);
      combo.addItemListener(this);

      table.addMouseListener(new MouseAdapter()
      {
        @Override
        public void mousePressed(MouseEvent event)
        {
          Point p = event.getPoint();
          int row = table.rowAtPoint(p);

          comboBox.setSelectedIndex(row);
          hide();
        }
      });

      table.setBackground(UIManager.getColor("ComboBox.listBackground"));
      table.setForeground(UIManager.getColor("ComboBox.listForeground"));
    }

    /**
     * This method is overridden from BasicComboPopup
     */
    @Override
    public void show()
    {
      if (isEnabled())
      {
        super.removeAll();

        int a=2;

        if (((Integer) UIManager.get("ScrollBar.width"))== null ){
        a=2;

        }else{
          a=((Integer) UIManager.get("ScrollBar.width")) ;
        }



        int scrollWidth = table.getPreferredSize().width +(a) + 1;
        int scrollHeight = comboBoxUI.getList().
                           getPreferredScrollableViewportSize().height;
        scroll.setPreferredSize(new Dimension(scrollWidth, scrollHeight));

        super.add(scroll);

        ListSelectionModel selectionModel = table.getSelectionModel();
        selectionModel.removeListSelectionListener(this);
        selectRow();
        selectionModel.addListSelectionListener(this);

        int scrollX = 0;
        int scrollY = comboBox.getBounds().height;

        if (popupAlignment == Alignment.RIGHT)
        {
          scrollX = comboBox.getBounds().width - scrollWidth;
        }

        show(comboBox, scrollX, scrollY);
      }
    }

    /**
     * Implemention of ListSelectionListener
     */
    public void valueChanged(ListSelectionEvent event)
    {
      comboBox.setSelectedIndex(table.getSelectedRow());
    }

    /**
     * Implemention of ItemListener
     */
    public void itemStateChanged(ItemEvent event)
    {
      if (event.getStateChange() != ItemEvent.DESELECTED)
      {
        ListSelectionModel selectionModel = table.getSelectionModel();
        selectionModel.removeListSelectionListener(this);
        selectRow();
        selectionModel.addListSelectionListener(this);
      }
    }

    /**
     * Sync the selected row of the table with the selected row of the combo.
     */
    private void selectRow()
    {
      int index = comboBox.getSelectedIndex();

      if (index != -1)
      {
        table.setRowSelectionInterval(index, index);
        table.scrollRectToVisible(table.getCellRect(index, 0, true));
      }
    }
  }

  /**
   * A model for the popup table's data
   */
  private class PopupTableModel extends AbstractTableModel
  {
    /**
     * Return the # of columns in the drop-down table
     */
    public int getColumnCount()
    {
      return columnNames.length;
    }

    /**
     * Return the # of rows in the drop-down table
     */
    public int getRowCount()
    {
      return tableData == null ? 0 : tableData.size();
    }

    /**
     * Determine the value for a given cell
     */
    public Object getValueAt(int row, int col)
    {
      if (tableData == null || tableData.size() == 0)
      {
        return "";
      }

      return tableData.get(row).get(col);
    }

    /**
     * All cells in the drop-down table are uneditable
     */
    @Override
    public boolean isCellEditable(int row, int col)
    {
      return false;
    }

    /**
     * Pull the column names out of the tableInfo object for the header
     */
    @Override
    public String getColumnName(int column)
    {
      String columnName = null;

      if (column >= 0 && column < columnNames.length)
      {
        columnName = columnNames[column].toString();
      }

      return (columnName == null) ? super.getColumnName(column) : columnName;
    }
  }
}
1
  • nadie tiene un comentario a esta consulta '
    – John Lopez
    el 20 dic. 2018 a las 3:23

0

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

Examina otras preguntas con la etiqueta o formula tu propia pregunta.