The Auto Completion For JTable Using J2s [Auto com

2019-06-01 07:04发布

问题:

With refrence To The Question! By abg and answer By mKorBel.

And Using J2s Auto Complete Combo Box!

The Code in the Answer By mKorBel For My Case Is Not Working When I use Item Class instead of String for ArrayList items.

   Vector<Vector<String>> comboData=util.GetComboData();
   ArrayList<String> listSomeString =      util.GetListForComboBox(comboData);
   Java2sAutoComboBox comboBox = new Java2sAutoComboBox(listSomeString);
   comboBox.setDataList(listSomeString);
   column.setCellEditor(new DefaultCellEditor(comboBox));

The Code For Function GetListForComboBox is as follows

   public ArrayList GetListForComboBox(Vector<Vector<String>> comboData) {


    //Vector model = new Vector();

    ArrayList<Item_> listSomeString = new ArrayList<Item_>();        
    try {

    String strdata;
    for (int i = 0; i < comboData.size(); i++) {
    String id = comboData.get(i).elementAt(0).toString();

    if (comboData.get(i).elementAt(1) != null) {
        strdata = comboData.get(i).elementAt(1).toString();
    } else {
        strdata = "";
    }

    Item_ it = new Item_(id, strdata);
    //model.addElement(it);
    listSomeString.add(it);
    }

    } catch (Exception ex) {
    ex.printStackTrace();
    }


    return listSomeString;
    }

The Code for Item_ class is as follows

           public class Item_     
    {       

    private String description;   
    private String id; 
    public Item_( String id,String description)        
    {            

    this.description = description;    
    this.id = id;   
    }          
    public String getId()         
    {             return id;         
    }         

    public String getDescription()    
    {             return description;        
    }         
    public String toString()        
    {             return description;         
    } 

    public void setDescription(String desc)    
    {              description=desc;        
    } 

    }

回答1:

no idea, have to create an HashMap or MutableComboBoxModel, because AutoComplete is strictly based on Array, have to change constructor

my curiosity about Bug (I can't be able to simulating this issue), is presented from this code too (disable comboBox.setRenderer(new ItemRenderer());)

import java.awt.BorderLayout;
import javax.swing.DefaultCellEditor;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.plaf.basic.BasicComboBoxRenderer;

public class TableRenderDemo extends JPanel {

    private static final long serialVersionUID = 1L;

    public TableRenderDemo() {
        super(new BorderLayout(5, 5));
        final JTable table = new JTable(new MyTableModel());
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        table.setFillsViewportHeight(true);
        table.setRowHeight(20);
        JScrollPane scrollPane = new JScrollPane(table);
        initColumnSizes(table);
        setUpSportColumn(table, table.getColumnModel().getColumn(2));
        add(scrollPane, BorderLayout.CENTER);
        JButton resetButton = new JButton("Reset to default");
        resetButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                for (int i = 0; i < table.getRowCount(); i++) {
                    table.getModel().setValueAt("None of the above", i, 2);
                }
            }
        });
        add(resetButton, BorderLayout.SOUTH);
    }

    private void initColumnSizes(JTable table) {
        MyTableModel model = (MyTableModel) table.getModel();
        TableColumn column = null;
        Component comp = null;
        int headerWidth = 0;
        int cellWidth = 0;
        Object[] longValues = model.longValues;
        TableCellRenderer headerRenderer = table.getTableHeader().getDefaultRenderer();
        for (int i = 0; i < 5; i++) {
            column = table.getColumnModel().getColumn(i);
            comp = headerRenderer.getTableCellRendererComponent(null, column.getHeaderValue(), false, false, 0, 0);
            headerWidth = comp.getPreferredSize().width;
            comp = table.getDefaultRenderer(model.getColumnClass(i)).getTableCellRendererComponent(table, longValues[i], false, false, 0, i);
            cellWidth = comp.getPreferredSize().width;
            column.setPreferredWidth(Math.max(headerWidth, cellWidth));
        }
    }

    private void setUpSportColumn(JTable table, TableColumn sportColumn) {
        ArrayList<String> listSomeString = new ArrayList<String>();
        listSomeString.add("Snowboarding");
        listSomeString.add("Rowing");
        listSomeString.add("Knitting");
        listSomeString.add("Speed reading");
        listSomeString.add("Pool");
        listSomeString.add("None of the above");
        JComboBox comboBox = new JComboBox();
        comboBox.addItem(new Item(1, "-"));
        comboBox.addItem(new Item(2, "Snowboarding"));
        comboBox.addItem(new Item(3, "Rowing"));
        comboBox.addItem(new Item(4, "Knitting"));
        comboBox.addItem(new Item(5, "Speed reading"));
        comboBox.addItem(new Item(6, "Pool"));
        comboBox.addItem(new Item(7, "None of the above"));
        comboBox.setMaximumRowCount(3);
        comboBox.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                JComboBox comboBox = (JComboBox) e.getSource();
                Item item = (Item) comboBox.getSelectedItem();
                System.out.println(item.getId() + " : " + item.getDescription());
            }
        });
        comboBox.setRenderer(new ItemRenderer());
        sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
        renderer.setToolTipText("Click for combo box");
        sportColumn.setCellRenderer(renderer);
    }

    class ItemRenderer extends BasicComboBoxRenderer {

        @Override
        public Component getListCellRendererComponent(JList list, Object value,
                int index, boolean isSelected, boolean cellHasFocus) {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if (value != null) {
                Item item = (Item) value;
                setText(item.getDescription().toUpperCase());
            }
            if (index == -1) {
                Item item = (Item) value;
                setText("" + item.getId());
            }
            return this;
        }
    }

    class Item {

        private int id;
        private String description;

        public Item(int id, String description) {
            this.id = id;
            this.description = description;
        }

        public int getId() {
            return id;
        }

        public String getDescription() {
            return description;
        }

        @Override
        public String toString() {
            return description;
        }
    }

    class MyTableModel extends AbstractTableModel {

        private static final long serialVersionUID = 1L;
        private String[] columnNames = {"First Name", "Last Name", "Sport", "# of Years", "Vegetarian"};
        private Object[][] data = {{"Kathy", "Smith", "Snowboarding", new Integer(5), false},
            {"John", "Doe", "Rowing", new Integer(3), true}, {"Sue", "Black", "Knitting", new Integer(2), false},
            {"Jane", "White", "Speed reading", new Integer(20), true}, {"Joe", "Brown", "Pool", new Integer(10), false}};
        public final Object[] longValues = {"Jane", "Kathy", "None of the above", new Integer(20), Boolean.TRUE};

        @Override
        public int getColumnCount() {
            return columnNames.length;
        }

        @Override
        public int getRowCount() {
            return data.length;
        }

        @Override
        public String getColumnName(int col) {
            return columnNames[col];
        }

        @Override
        public Object getValueAt(int row, int col) {
            return data[row][col];
        }

        @Override
        public Class<?> getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }

        @Override
        public boolean isCellEditable(int row, int col) {
            if (col < 2) {
                return false;
            } else {
                return true;
            }
        }

        @Override
        public void setValueAt(Object value, int row, int col) {
            data[row][col] = value;
            fireTableCellUpdated(row, col);
            System.out.println("New value of data: " + getValueAt(row, col));
        }
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("TableRenderDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        TableRenderDemo newContentPane = new TableRenderDemo();
        frame.setContentPane(newContentPane);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }
}


回答2:

have to play with Caret (quick implementations for Caret, sorry too lazy, without any idea), otherwise all editor is selected, backspace selected 1st Item by default

import java.awt.BorderLayout;
import javax.swing.DefaultCellEditor;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JList;
import javax.swing.JTextField;
import javax.swing.plaf.basic.BasicComboBoxRenderer;
import javax.swing.text.JTextComponent;

public class TableRenderDemo extends JPanel {

    private static final long serialVersionUID = 1L;

    public TableRenderDemo() {
        super(new BorderLayout(5, 5));
        final JTable table = new JTable(new MyTableModel());
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        table.setFillsViewportHeight(true);
        JScrollPane scrollPane = new JScrollPane(table);
        initColumnSizes(table);
        setUpSportColumn(table, table.getColumnModel().getColumn(2));
        add(scrollPane, BorderLayout.CENTER);
        JButton resetButton = new JButton("Reset to default");
        resetButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                for (int i = 0; i < table.getRowCount(); i++) {
                    table.getModel().setValueAt("None of the above", i, 2);
                }
            }
        });
        add(resetButton, BorderLayout.SOUTH);
    }

    private void initColumnSizes(JTable table) {
        MyTableModel model = (MyTableModel) table.getModel();
        TableColumn column = null;
        Component comp = null;
        int headerWidth = 0;
        int cellWidth = 0;
        Object[] longValues = model.longValues;
        TableCellRenderer headerRenderer = table.getTableHeader().getDefaultRenderer();
        for (int i = 0; i < 5; i++) {
            column = table.getColumnModel().getColumn(i);
            comp = headerRenderer.getTableCellRendererComponent(null, column.getHeaderValue(), false, false, 0, 0);
            headerWidth = comp.getPreferredSize().width;
            comp = table.getDefaultRenderer(model.getColumnClass(i)).getTableCellRendererComponent(table, longValues[i], false, false, 0, i);
            cellWidth = comp.getPreferredSize().width;
            column.setPreferredWidth(Math.max(headerWidth, cellWidth));
        }
    }

    private void setUpSportColumn(JTable table, TableColumn sportColumn) {
        ArrayList<String> listSomeString = new ArrayList<String>();
        listSomeString.add("-");
        listSomeString.add("Snowboarding");
        listSomeString.add("Rowing");
        listSomeString.add("Knitting");
        listSomeString.add("Speed reading");
        listSomeString.add("Pool");
        listSomeString.add("None of the above");
        AutoComboBox comboBox = new AutoComboBox(listSomeString);
        comboBox.setDataList(listSomeString);
        comboBox.setMaximumRowCount(3);
        comboBox.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                JComboBox comboBox = (JComboBox) e.getSource();
                System.out.println(comboBox.getSelectedItem());
            }
        });
        JTextComponent editor = ((JTextField) comboBox.getEditor().getEditorComponent());
        editor.setCaretPosition(editor.getCaretPosition());
        editor.moveCaretPosition(0);
        editor.addFocusListener(focsListener);

        sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
        renderer.setToolTipText("Click for combo box");
        sportColumn.setCellRenderer(renderer);
    }

    private FocusListener focsListener = new FocusListener() {

        @Override
        public void focusGained(FocusEvent e) {
            dumpInfo(e);
        }

        @Override
        public void focusLost(FocusEvent e) {
            //dumpInfo(e);
        }

        private void dumpInfo(FocusEvent e) {
            //System.out.println("Source  : " + name(e.getComponent()));
            //System.out.println("Opposite : " + name(e.getOppositeComponent()));
            //System.out.println("Temporary: " + e.isTemporary());
            final Component c = e.getComponent();
            if (c instanceof JFormattedTextField) {
                EventQueue.invokeLater(new Runnable() {

                    public void run() {
                        ((JFormattedTextField) c).requestFocus();
                        ((JFormattedTextField) c).setText(((JFormattedTextField) c).getText());
                        ((JFormattedTextField) c).selectAll();
                    }
                });
            } else if (c instanceof JTextField) {
                EventQueue.invokeLater(new Runnable() {

                    public void run() {
                        ((JTextField) c).requestFocus();
                        ((JTextField) c).setText(((JTextField) c).getText());
                        ((JTextField) c).selectAll();
                    }
                });
            }
        }

        private String name(Component c) {
            return (c == null) ? null : c.getName();
        }
    };

    class ItemRenderer extends BasicComboBoxRenderer {

        @Override
        public Component getListCellRendererComponent(JList list, Object value,
                int index, boolean isSelected, boolean cellHasFocus) {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if (value != null) {
            }
            if (index == -1) {
            }
            return this;
        }
    }

    class MyTableModel extends AbstractTableModel {

        private static final long serialVersionUID = 1L;
        private String[] columnNames = {"First Name", "Last Name", "Sport", "# of Years", "Vegetarian"};
        private Object[][] data = {{"Kathy", "Smith", "Snowboarding", new Integer(5), false},
            {"John", "Doe", "Rowing", new Integer(3), true}, {"Sue", "Black", "Knitting", new Integer(2), false},
            {"Jane", "White", "Speed reading", new Integer(20), true}, {"Joe", "Brown", "Pool", new Integer(10), false}};
        public final Object[] longValues = {"Jane", "Kathy", "None of the above", new Integer(20), Boolean.TRUE};

        @Override
        public int getColumnCount() {
            return columnNames.length;
        }

        @Override
        public int getRowCount() {
            return data.length;
        }

        @Override
        public String getColumnName(int col) {
            return columnNames[col];
        }

        @Override
        public Object getValueAt(int row, int col) {
            return data[row][col];
        }

        @Override
        public Class<?> getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }

        @Override
        public boolean isCellEditable(int row, int col) {
            if (col < 2) {
                return false;
            } else {
                return true;
            }
        }

        @Override
        public void setValueAt(Object value, int row, int col) {
            data[row][col] = value;
            fireTableCellUpdated(row, col);
            System.out.println("New value of data: " + getValueAt(row, col));
        }
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("TableRenderDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        TableRenderDemo newContentPane = new TableRenderDemo();
        frame.setContentPane(newContentPane);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }
}


回答3:

I know this is an old question but in case it can help someone out there, here is how to do what OP asked:

In Java2sAutoCombobox, replace the function setSelectedValue(Object obj) by this one instead:

void setSelectedValue(String str)
{
    if(!isFired)
    {
        Object objectToSelect = null;

        for(Object obj: getDataList())
        {
            if( obj.toString().equals(str) )
            {
                objectToSelect = obj;
                break;
            }
        }

        isFired = true;
        setSelectedItem(objectToSelect);
        fireItemStateChanged(new ItemEvent(this, 701, selectedItemReminder,
                                           1));
        isFired = false;
    }
}

By doing this, JComboBox.setSelectedItem() will select the object from the datalist instead of its string representation.