Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

JTable and combobox cell editor problem

Thread view: 
kim.berrisford@gmail.com - 27 Sep 2006 16:14 GMT
I have a JTable which uses a custom cell editor for comboboxes.  The
table starts out with one row and the user can add more rows by
clicking a button.  What is wrong is when the user selects a combobox
item in the first row, and then adds a second row, going to the
combobox column in the second row is bringing up what they selected in
the first row.  I want the combobox in the new row to initialize with
the first item in the combobox (in this case it's an empty string).
Here is my code, it should be able to be compiled and tested with the
second column in the table:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.*;
import java.util.*;

public class testTable extends JPanel implements ActionListener {
   String tableName;
   String prettyName;
   String buttonName;
   int maxRows;
   JTable table;
   Vector rows, columns;
   DefaultTableModel tableModel;
   JScrollPane scrollPane;
   JButton buttonAdd, buttonDelete;
   JPanel buttonPanel;

   public static void main(String[] args) {
      JFrame jf = new JFrame("Testframe");
      jf.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {System.exit(0);}
      });

      testTable jsF = new testTable();
      jf.getContentPane().add(jsF, BorderLayout.CENTER);
      jf.pack();
      jf.setVisible(true);
   }

   public testTable() {

       rows = new Vector();
       columns = new Vector();

       setFocusTraversalPolicy(new DefaultFocusTraversalPolicy());
       setFocusCycleRoot(true);

       maxRows = 10;

       String[] c = {"Col1", "Col2", "Col3"};
       addColumns(c);
       tableModel = new DefaultTableModel();
       tableModel.setDataVector(rows, columns);
       table = new JTable(tableModel) {
           public void changeSelection(int row, int column, boolean
toggle, boolean extend){
               super.changeSelection(row, column, toggle, extend);
               if(editCellAt(row, column))
                   getEditorComponent().requestFocusInWindow();
           }
       };
       table.setSurrendersFocusOnKeystroke(true);
       scrollPane = new JScrollPane(table);
       table.setRowSelectionAllowed(true);
       table.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
       setCellEditors();

       buttonPanel = new JPanel();
       buttonAdd = new JButton("Add New Row");
       buttonDelete = new JButton("Delete Row");

       buttonPanel.add(buttonAdd);
       buttonPanel.add(buttonDelete);
       buttonAdd.addActionListener(this);
       buttonDelete.addActionListener(this);

       setLayout(new BorderLayout());
       add("Center", scrollPane);
       add("South", buttonPanel);
       addRow();
   }

   public void addColumns(String[] c) {
       for(int i = 0; i < c.length; i++) {
           columns.addElement(c[i]);
       }
   }

   public void setCellEditors() {
       TableColumn col = table.getColumnModel().getColumn(1);
       Object[] vals = new Object[] {"", "Test1", "Test2", "Test3"};
       col.setCellEditor(new AutoCompletionComboBoxEditor(vals));
   }

   public static class AutoCompletionComboBoxEditor extends
AbstractCellEditor implements TableCellEditor {
       JComboBox cbx;

       public AutoCompletionComboBoxEditor(Object[] items){
           cbx = new JComboBox(items);
       }

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

       public Object getCellEditorValue() {
           return cbx.getSelectedItem();
       }
   }

   public void addRow() {
       if(table.getRowCount() < maxRows){
           Vector r = createBlankElement();
           rows.addElement(r);
           table.addNotify();
       }
   }

   public Vector createBlankElement() {
       Vector t = new Vector();
       t.addElement((String) "");
       t.addElement((String) "");
       t.addElement((String) "");
       return t;
   }

   public void deleteRow(int index) {
       if(index > -1){
           TableCellEditor cellEditor = table.getCellEditor();
           if(cellEditor!=null)
               cellEditor.stopCellEditing();
           ((DefaultTableModel)table.getModel()).removeRow(index);
           table.addNotify();
       }
   }

   public void actionPerformed(ActionEvent source) {
       if(source.getSource() == (JButton) buttonAdd){
           addRow();
       } else if(source.getSource() == (JButton) buttonDelete) {
           deleteRow(table.getSelectedRow());
       }
   }

}
Richard Wheeldon - 01 Oct 2006 11:10 GMT
>         public Component getTableCellEditorComponent(JTable table,
> Object value, boolean isSelected, int row, int column) {

 cbx.setSelectedItem(value);

>             return cbx;
>         }

Hope this helps,

Richard


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.