Tab in and out JComboBox in JTable : Table Renderer Editor « Swing JFC « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Swing JFC » Table Renderer EditorScreenshots 
Tab in and out JComboBox in JTable
 
/*
 
  Code reference: http://forum.java.sun.com/thread.jspa?threadID=384665&messageID=3310903  
Pratap Singh
Email:  pratap.singh at siemens.com
        at is @
 
 * */
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Calendar;
import java.util.EventObject;
import java.util.GregorianCalendar;

import javax.swing.ComboBoxEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.event.CellEditorListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.EventListenerList;
import javax.swing.plaf.basic.BasicComboBoxUI;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

public class SimpleTableTestTabComboBox extends JFrame {

  protected JTable table;

  public SimpleTableTestTabComboBox() {
    Container pane = getContentPane();
    pane.setLayout(new BorderLayout());
    TableValues tv = new TableValues();
    table = new JTable(tv);
    TableColumnModel tcm = table.getColumnModel();
    TableColumn tc = tcm.getColumn(TableValues.GENDER);
//    tc.setCellRenderer(new GenderRenderer());

    tc.setCellEditor(new GenderEditor());

    JScrollPane jsp = new JScrollPane(table);
    pane.add(jsp, BorderLayout.CENTER);
    pane.add(new JTextField(), BorderLayout.SOUTH);
  }

  public static void main(String[] args) {
    SimpleTableTestTabComboBox stt = new SimpleTableTestTabComboBox();
    stt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    stt.setSize(400300);
    stt.setVisible(true);
  }
}

class TableValues extends AbstractTableModel {

  public final static int FIRST_NAME = 0;

  public final static int LAST_NAME = 1;

  public final static int DATE_OF_BIRTH = 2;

  public final static int ACCOUNT_BALANCE = 3;

  public final static int GENDER = 4;

  public final static boolean GENDER_MALE = true;

  public final static boolean GENDER_FEMALE = false;

  public final static String[] columnNames = "First Name""Last Name""Date of Birth",
      "Account Balance""Gender" };

  public Object[][] values = {
      "Clay""Ashworth"new GregorianCalendar(1962, Calendar.FEBRUARY, 20).getTime(),
          new Float(12345.67)"three" },
      "Jacob""Ashworth"new GregorianCalendar(1987, Calendar.JANUARY, 6).getTime(),
          new Float(23456.78)"three1" },
      "Jordan""Ashworth"new GregorianCalendar(1989, Calendar.AUGUST, 31).getTime(),
          new Float(34567.89)"three2" },
      "Evelyn""Kirk"new GregorianCalendar(1945, Calendar.JANUARY, 16).getTime(),
          new Float(-456.70)"One" },
      "Belle""Spyres"new GregorianCalendar(1907, Calendar.AUGUST, 2).getTime(),
          new Float(567.00)"two" } };

  public int getRowCount() {
    return values.length;
  }

  public int getColumnCount() {
    return values[0].length;
  }

  public Object getValueAt(int row, int column) {
    return values[row][column];
  }

  public void setValueAt(Object aValue, int r, int c) {
    values[r][c= aValue;
  }

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

  public boolean isCellEditable(int r, int c) {
    return c == GENDER;
  }
}

class MyComboUI extends BasicComboBoxUI {

  public JList getList() {
    return listBox;
  }
}

class GenderRenderer extends JComboBox implements TableCellRenderer {

  public GenderRenderer() {
    super();
    addItem("three");
    addItem("three1");
    addItem("three2");
    addItem("One");
    addItem("two");
  }

  public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
      boolean hasFocus, int row, int column) {

    if (hasFocus) {
      table.editCellAt(row, column);
      JComboBox combo = (JComboBoxtable.getColumnModel().getColumn(TableValues.GENDER)
          .getCellEditor();
      ComboBoxEditor editor = (ComboBoxEditorcombo.getEditor();
      JTextField tf = (JTextFieldeditor.getEditorComponent();
      tf.requestFocusInWindow();
      tf.selectAll();
    }
    if (isSelected) {
      setForeground(table.getSelectionForeground());
      super.setBackground(table.getSelectionBackground());
    else {
      setForeground(table.getForeground());
      setBackground(table.getBackground());
    }

    setSelectedItem(value);
    return this;
  }

}

class GenderEditor extends JComboBox implements TableCellEditor, CaretListener {

  protected EventListenerList listenerList = new EventListenerList();

  protected ChangeEvent changeEvent = new ChangeEvent(this);

  private MyComboUI comboUi = new MyComboUI();

  private JTextField tf;

  Object newValue;

  public GenderEditor() {
    super();
    setUI(comboUi);
    // comboUi.get
    addItem("three");
    addItem("three1");
    addItem("three2");
    addItem("One");
    addItem("two");
    setEditable(true);
    tf = (JTextFieldgetEditor().getEditorComponent();
    tf.addCaretListener(this);

    tf.addKeyListener(new KeyAdapter() {
      public void keyReleased(KeyEvent e) {
        if (e.getKeyCode() == e.VK_ENTER) {
          Object value = comboUi.getList().getSelectedValue();
          tf.transferFocus();
          hidePopup();
          System.out.println("enter pressed, selected value, when enter pressed: " + value);
          fireEditingStopped();
        }
      }
    });

    addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent event) {
        // fireEditingStopped();
      }
    });
  }

  public void addCellEditorListener(CellEditorListener listener) {
    listenerList.add(CellEditorListener.class, listener);
  }

  public void removeCellEditorListener(CellEditorListener listener) {
    listenerList.remove(CellEditorListener.class, listener);
  }

  protected void fireEditingStopped() {
    System.out.println("fireEditingStopped called ");

    CellEditorListener listener;
    Object[] listeners = listenerList.getListenerList();
    for (int i = 0; i < listeners.length; i++) {
      if (listeners[i== CellEditorListener.class) {
        listener = (CellEditorListenerlisteners[i + 1];
        listener.editingStopped(changeEvent);
      }
    }
  }

  protected void fireEditingCanceled() {
    CellEditorListener listener;
    Object[] listeners = listenerList.getListenerList();
    for (int i = 0; i < listeners.length; i++) {
      //if (listeners == CellEditorListener.class) {
      if (listeners[i== CellEditorListener.class) {
        listener = (CellEditorListenerlisteners[i + 1];
        listener.editingCanceled(changeEvent);
      }
    }
  }

  public void cancelCellEditing() {
    System.out.println("cancelCellEditing called ");
    fireEditingCanceled();
  }

  public boolean stopCellEditing() {
    System.out.println("stopCellEditing called ");
    fireEditingStopped();
    return true;
  }

  public boolean isCellEditable(EventObject event) {
    return true;
  }

  public boolean shouldSelectCell(EventObject event) {
    return true;
  }

  public Object getCellEditorValue() {
    System.out.println("getCellEditorValue called returning vlaue: " + newValue);
    tf.setText(newValue.toString());
    return newValue;
    // return super.getSelectedItem();
  }

  public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected,
      int row, int column) {

    return this;
  }

  public void caretUpdate(CaretEvent e) {
    if (!isPopupVisible() && tf.isShowing() && tf.hasFocus())
      showPopup();

    JTextField tf = (JTextFielde.getSource();
    String text = tf.getText().toLowerCase();

    int index = -1;
    for (int i = 0; i < super.getItemCount(); i++) {
      String item = ((StringgetItemAt(i)).toLowerCase();
      if (item.startsWith(text)) {
        index = i;
        break;
      }
    }
    if (index != -1)
      comboUi.getList().setSelectedIndex(index);
    else
      comboUi.getList().clearSelection();

    newValue = comboUi.getList().getSelectedValue();
    System.out.println("new value set to: " + newValue);
  }

}           
         
  
Related examples in the same category
1. Shading Rows and Columns in a JTable Component
2. Shades every other column yellow
3. Using Built-In Cell Renderers and Editors in a JTable Component
4. Get Default cell Renderer
5. Get Default Cell Editor
6. Creating a Custom Cell Renderer in a JTable Component
7. Creating a Class-Based Custom Cell Renderer in a JTable Component
8. A slider renderer for volume values in a tableA slider renderer for volume values in a table
9. Table Cell Editor: ComboBoxCellEditor
10. Table with a custom cell renderer and editor for the color dataTable with a custom cell renderer and editor for the color data
11. Table with initialized column sizes and a combo box editorTable with initialized column sizes and a combo box editor
12. StockTable 3: CellRendererStockTable 3: CellRenderer
13. Colored Table CellRendererColored Table CellRenderer
14. Color Table CellRendererColor Table CellRenderer
15. Change Table cell background with column renderer Change Table cell background with column renderer
16. Install different Table Renderer for even and odd rowsInstall different Table Renderer for even and odd rows
17. A table that allows the user to pick a color from a pulldown listA table that allows the user to pick a color from a pulldown list
18. MixerModel and sliders for rendering volume valuesMixerModel and sliders for rendering volume values
19. Table Editor: Editable Color ColumnTable Editor: Editable Color Column
20. Creating a Custom Table Cell Editor in a JTable Component
21. Preventing Invalid Values in a Cell in a JTable Component
22. Setting the Activation Click Count for a Table Cell Editor in a JTable Component
23. Using a JComboBox in a Cell in a JTable Component
24. Using a List JSpinner as a Cell Editor in a JTable Component
25. Rendering an image in a JTable column
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.