Showing posts with label TableCellEditor. Show all posts
Showing posts with label TableCellEditor. Show all posts

Tuesday, May 9, 2017

Programmtically start editing a JTable cell as if you have double clicked in it

There are times after you have finished something, you want the editing caret to flash in one of your JTable cell where you would like the user to pay attention to.

This is how you can programmtically make the editing caret flash in a JTable cell as if the user has double clicked in it.

JTable table = new JTable();
table.setModel(<table model>);

//programmatically edit the cell in row 2 and column 3
table.setSelectedRow(2);
table.setSelectedCol(3);
if (table.editCellAt(2, 3)){
      table.getEditorComponent().requestFocusInWindow();
}

If you would like any cell in a JTable always in editing mode whenever it gains focus, you can modify the changeSelection method of JTable.

JTable table = new JTable() {
      public void changeSelection(int row, int col, boolean toggle, boolean extend) {
            super.changeSelection(row, col, toggle, extend);

            if (table.editCellAt(row, col)){
                  table.getEditorComponent().requestFocusInWindow();
           }
      }
};

---------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

Wednesday, July 27, 2016

Solved - JTable: wrap text in a cell, column, or row

To wrap text in a JTable, you need to create a TableCellRenderer and TableCellEditor that use the JTextArea as the component. Then, use them for the cell, column, row, or the whole table where you want to wrap text. Following is a sample code.


public class TableWrapText extends JFrame {
    private DefaultTableModel tableModel = null;
 
    public TableWrapText() {
        String[][] data = new String[][]{
            new String[]{"Travel", "Go to somewhere else from where you live to enjoy spending money and getting lost in the esoterics."},
            new String[]{"Help", "Do something to make the person get what he or she wants for pay or for free, it is upto you."},
            new String[]{"Sleep", "A state of temperary unconscious due to the nerves system is busy with something else."}
        };
        tableModel = new DefaultTableModel(data, new String[]{"Name", "Content"});

        JTable table = new JTable(tableModel){
            public TableCellRenderer getCellRenderer(int row, int col){
                //Wrap text in a specific cell
                if (row == 0 && col == 1){
                    return new MyRenderer();
                //Wrap text in a particular column  
          //    if (col == 1) {
//                    return new MyRenderer();
//              }
                //Wrap text in a row
//              if (row == 0) {
//                    return new MyRenderer();
//              }    
                } else {
                    return new DefaultTableCellRenderer();
                }
            }

            public TableCellEditor getCellEditor(int row, int col){
                //Wrap text in a specific cell
                if (row == 0 && col == 1){
                    return new MyEditor();
                //Wrap text in a particular column  
          //    if (col == 1) {
//                    return new MyEditor();
//              }
                //Wrap text in a row
//              if (row == 0) {
//                    return new MyEditor();
//              }    
                } else {
                    return new DefaultCellEditor(new JTextField());
                }
            }
        };
        table.setRowHeight(0, table.getFont().getSize()*3);
//        table.setRowHeight(table.getFont().getSize()*3);
        JScrollPane sp = new JScrollPane(table);
        getContentPane().add(sp);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(600, 400);
        this.setVisible(true);
    }
 
    private class MyRenderer extends JTextArea implements TableCellRenderer{
        public MyRenderer() {
            setOpaque(true);
            setLineWrap(true);
            setWrapStyleWord(true);
        }
     
        public Component getTableCellRendererComponent(JTable table,Object value,
            boolean isSelected, boolean hasFocus, int row,int column) {
         
            this.setText(value == null ? "" : value.toString());
            return this;
        }

    }

    private class MyEditor extends AbstractCellEditor implements TableCellEditor {
        JTextArea comp = new JTextArea();
        JTable table;
        int row;
     
        public MyEditor() {
            comp.setLineWrap(true);
            comp.setWrapStyleWord(true);
            comp.addComponentListener(new ComponentAdapter() {
                public void componentResized(ComponentEvent e) {
                    super.componentResized(e);
                    table.setRowHeight(row, (int) (comp.getPreferredSize().getHeight()));
                }
            });
            comp.addKeyListener(new KeyAdapter() {
                public void keyTyped(KeyEvent e) {
                    super.keyTyped(e);
                    table.setRowHeight(row, (int) (comp.getPreferredSize().getHeight()));
                }
            });
        }

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

            comp.setText((String) value);
            comp.setFont(table.getFont());

            return comp;
        }

        public Object getCellEditorValue() {
            return comp.getText();
        }
    }
 
    public static void main(String[] args){
        new TableWrapText();
    }
}

-----------------------------------------------------------------------------------------------------------------
Watch the blessing and loving online channel: SupremeMasterTV live




If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book for free here.

Friday, December 27, 2013

TableCellEditor that implements ItemListener

This sample code uses the JComboBox as the component.

public class MyComboBoxEditor extends DefaultCellEditor implements PopupMenuListener, ItemListener {
        Component comp = null;
        int theRow = 0;
        int theCol = 0;
        public MyComboBoxEditor(JComboBox jcb) {
            super(jcb);
        }

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

            //critical to set the row and col before calling
            //super.getTableCellEditorComponent which fires the ItemListener
            //for the ItemListener to work properly not using previous row and col.
            theRow = row;
            theCol = column;
            comp = super.getTableCellEditorComponent(table, value, isSelected, row, column);
         
            ((JComboBox)comp).removeItemListener(this);
            ((JComboBox)comp).addItemListener(this);
            ((JComboBox)comp).removePopupMenuListener(this);
            ((JComboBox)comp).addPopupMenuListener(this);
         
            return comp;
        }
     
        @Override
        public void popupMenuCanceled(PopupMenuEvent e){
            table.stopCellEditing();
        }
   
        @Override
        public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
            table.stopCellEditing();
        }
     
        @Override
        public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
        }
     
        @Override
        public void itemStateChanged(ItemEvent e) {
            if (e == null) {
                return;
            }
            JComboBox source = (JComboBox) e.getSource();
            if (e.getStateChange() == ItemEvent.SELECTED) {
                  Object obj = e.gtItem();
                  //table.editCellAt(theRow, theCol); need this because of the calling table.stopCellEditing() above
                  //code for modifying the JTable related to theRow and theCol
           }
           e = null
      }
}

----------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.