Adding JButtons to JTable

Hello all,
How can I add JButtons to a JTable?
I found an article that is supposed to show you how to do just that:
http://www.devx.com/getHelpOn/10MinuteSolution/20425
I downloaded the code in the article and it works to an extent - I can see the buttons in the table but the buttons seem as if they are disabled :( Since I used this code in my application, I get the same behavior too.
Is there a bug in the code? Is there a simpler solution? What am I missing?
Raj

Thanks. That makes the button clickable. But now the button changes back to it's old value when you click on another cell. I suppose that's because we have 2 buttons, one for rendering and one for editing. So I added a setValueAt(Object value, int row, int column) to MyModel. This works throws class cast exception.
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class ButtonInTable extends JFrame {
    JTable t=new JTable(new MyModel());
    public ButtonInTable() {
        this.getContentPane().add(new JScrollPane(t));
        this.setBounds(50,50,300,200);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        t.setDefaultEditor(Object.class,new ButtonEditor());
        t.setDefaultRenderer(Object.class,new ButtonRenderer());      
    public static void main(String[] args) {
        ButtonInTable buttonInTable1 = new ButtonInTable();
        buttonInTable1.show();
    class ButtonEditor extends JButton implements TableCellEditor {
        Object currentValue;
        Vector listeners=new Vector();
        public ButtonEditor() {
                        this.addActionListener(new ActionListener() {
                            public void actionPerformed(ActionEvent e) {
                                currentValue=JOptionPane.showInputDialog("Input new value!");
                                if (currentValue!=null) {
                                    setText(currentValue.toString());
        public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
            currentValue=value;
//            this.setText(value.toString());
            this.setText( ((JButton)value).getText() );
            return this;
        public Object getCellEditorValue() {
            return currentValue;
        public boolean isCellEditable(EventObject anEvent) {
            return true;
        public boolean shouldSelectCell(EventObject anEvent) {
            return true;
        public boolean stopCellEditing() {
            ChangeEvent ce=new ChangeEvent(this);
            for (int i=0; i<listeners.size(); i++) {
                ((CellEditorListener)listeners.get(i)).editingStopped(ce);
            return true;
        public void cancelCellEditing() {
            ChangeEvent ce=new ChangeEvent(this);
            for (int i=0; i<listeners.size(); i++) {
                ((CellEditorListener)listeners.get(i)).editingCanceled(ce);
        public void addCellEditorListener(CellEditorListener l) {
            listeners.add(l);
        public void removeCellEditorListener(CellEditorListener l) {
            listeners.remove(l);
    class ButtonRenderer extends DefaultTableCellRenderer {
        public Component getTableCellRendererComponent
                (JTable table, Object button, boolean isSelected, boolean hasFocus, int row, int column) {
            return (JButton)button;
    class OldModel extends DefaultTableModel {
        public OldModel() {
            super(new String[][]{{"1","2"},{"3","4"}},new String[] {"1","2"});
    class MyModel extends AbstractTableModel {
        private String[] titles = { "A", "B" };
        private Object[][] summaryTable =
                { new JButton("11"), new JButton("12") },
                { new JButton("21"), new JButton("22") }
        public MyModel(){
            super();
        public int getColumnCount() {
            return titles.length;
        public String getColumnName(int col) {
            return titles[col];
        public int getRowCount() {
            return summaryTable.length;
        public Object getValueAt(int row, int col) {
            return summaryTable[row][col];
        public void setValueAt(Object value, int row, int column) {
            summaryTable[row][column] = value;
            fireTableCellUpdated(row, column);
        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        public boolean isCellEditable(int row, int col) {
            return true;
}

Similar Messages

  • Adding JButton into JTable cells

    Hi there!!
    I want to add a JButton into JTable cells.In fact I have got two classes.Class2 has been extended from the AbstractTableModel class and Class1 which is using Class2's model,,,here's the code,,
    Class1
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    public class Class1 extends javax.swing.JFrame {
       //////GUI specifications
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new TestTableButton().setVisible(true);
            Class2 model=new Class2();
            jTable1=new JTable(model);
            jScrollPane1.setViewportView(jTable1);
        // Variables declaration - do not modify                    
        private javax.swing.JScrollPane jScrollPane1;
        // End of variables declaration                  
        private javax.swing.JTable jTable1;
    }Class2
    import javax.swing.table.*;
    public class Class2 extends AbstractTableModel{
        private String[] columnNames = {"A","B","C"};
        private Object[][] data = {
        public int getColumnCount() {
            return columnNames.length;
        public int getRowCount() {
            return data.length;
        public String getColumnName(int col) {
            return columnNames[col];
        public Object getValueAt(int row, int col) {
            return data[row][col];
        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
         * Don't need to implement this method unless your table's
         * editable.
        public boolean isCellEditable(int row, int col) {
            //Note that the data/cell address is constant,
            //no matter where the cell appears onscreen.
                return false;
         * Don't need to implement this method unless your table's
         * data can change.
        public void setValueAt(Object value, int row, int col) {
            data[row][col] = value;
            fireTableCellUpdated(row, col);
    }what modifications shud I be making to the Class2 calss to add buttons to it?
    Can anybody help plz,,,,,??
    Thanks in advance..

    Hi rebol!
    I did search out a lot for this but I found my problem was a bit different,,in fact I want to use another class's model into a class being extended by JFrame,,so was a bit confused,,,hope you can give me some ideas about how to handle that scenario,,I know this topic has been discussed before here many a times and also have visited this link,,
    http://forum.java.sun.com/thread.jspa?threadID=465286&messageID=2147913
    but am not able to map it to my need,,,hope you can help me a bit...
    Thanks .....

  • Adding JButton, JTextField to scroll pane...

    I have developed an appln in which I have added JPanel to JFrame.
    To this JPanel I m adding JButton & JTextField. But the JButton & JTextField are increasing dynamically in my appln[according to DBMS query]. So I wanted to use scrolpane or something like so that the window can be scrolled vertically to view all the components[JButton & JTextField]. I m developing this as a standalone appln.
    Plz help..
    Thanking in advance.

    Thanx fouadk ,
    But my problem still persists. Now though I m able to add JPanel to JScrollPane, but when the components in the JPanel increase than the height of the Pane, the components at the dowm are not visible unless I resize the window by manually dragging the corners. Actually I wanted to keep the size of the window constant and make use of the VERTICAL SCROLLBAR.
    Plz help.
    Thanking in advance.
    Following is code:
    import javax.swing.*;
    public class testt extends JFrame {
    private JPanel p = new JPanel();
    private JScrollPane sp = new JScrollPane(p
    ,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS
    ,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    private JButton b[] = new JButton[15];
    public testt() {
    super("TEST");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(500, 400);
    setLocation(50, 20);
    this.add(sp);
    p.setLayout(null);
    sp.setAutoscrolls(true);
    for(int i = 0, y = 10; i < 15; i++, y += 40)
         b[i] = new JButton("BUTTON "+ (i+1));
         b.setBounds(10,y,100,20);
         p.add(b[i]);
    public static void main(String[] args) {
    testt t = new testt();
    t.setVisible(true);

  • Adding JButton in a JTable

    hi
    i know this has been discussed quite a number of times before, but i still couldn't figure it out..
    basically i just want to add a button to the 5th column of every row which has data in it.
    this is how i create my table (partially)
         private JTable clientTable;
         private DefaultTableModel clientTableModel;
    private JScrollPane scrollTable;
    clientTableModel = new DefaultTableModel(columnNames,100);
              clientTable = new JTable(clientTableModel);
              TableColumn tblColumn1 = clientTable.getColumn("Request ID");
              tblColumn1.setPreferredWidth(70);
              tblColumn1 = clientTable.getColumn("Given Name");
              tblColumn1.setPreferredWidth(300);
              tblColumn1 = clientTable.getColumn("Address");
              tblColumn1.setPreferredWidth(350);
              tblColumn1 = clientTable.getColumn("Card Serial");
              tblColumn1.setPreferredWidth(100);
              tblColumn1 = clientTable.getColumn("Print Count");
              tblColumn1.setPreferredWidth(70);
              tblColumn1 = clientTable.getColumn("Print?");
              tblColumn1.setPreferredWidth(40);
              clientTableModel.insertRow(0,data);
              //clientTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
              scrollTable = new JScrollPane(clientTable);and i call this function void listInfoInTable(){
              JButton cmdPrint[];
              Vector columnNames = new Vector();
              Object[] data={"","","","","",""};
              Statement stmt=null;
              ResultSet rs=null;
              PreparedStatement ps;
              String query=null;
              String retrieve=null;
              int i,j=0;
              TableColumnModel modelCol = clientTable.getColumnModel();
              try{
                   con = DriverManager.getConnection(url);
                   JOptionPane.showMessageDialog(null,"Please wait while the program retrieves data.");
                   query="select seqNo, givenName, address1, address2, address3, address4, cardSerNr, PIN1, PrintFlag from PendPINMail where seqNo<250;";
                 ps = con.prepareStatement(query);
                 rs=ps.executeQuery();
                 while (rs.next()){
                      data[0]= rs.getString("seqNo");
                      data[1]= rs.getString("givenName");
                      data[2]= rs.getString("address1");
                      data[3]= rs.getString("cardSerNr");
                      data[4]= rs.getString("PrintFlag");
    //                  modelCol.getColumn(5).setCellRenderer();
                      clientTableModel.insertRow(j,data);
                      j++;
              }catch (SQLException ex){
                   JOptionPane.showMessageDialog(null,"Database error: " + ex.getMessage());
         } to display data from database inside the table.
    How do I add JButton to the 5th column of each row? This documentation here http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#width says that i need to implement TableCellEditor to put JButton in the table. How do i really do it? I want the button to call another function which prints the data from the row (but this is another story).
    Any help is greatly appreciated. Thanks

    you would need CellRenderer i think that it is in
    javax.swing.table.*;
    see if you can get started with that.
    Davidthanks, i'll try and have a look at it
    Yes, that's definitely what you need to start with,
    but you also need a CellEditor to return the
    button as well, otherwise the button will not be
    clickable (the renderer just paints the cell for
    display, it doesn't allow you to interact with it).
    You could maintain a list of components for rendering
    each cell of the table so that your
    CellRenderer and CellEditor always
    return the same object (i.e. a JButton for any
    given cell).
    CB.thanks for the info.. could you point me to some examples? is sounds quite complicated for me....
    thanks again

  • JButton in JTable Help

    Hi
    I have successfully added a JButton to a table and am able to click on it and when you do it opens up a new window.
    I have set the cellEditor to my new class which performs the actions of a button. I am wanting the new window which opens up to either be presented in the original frame or update the original frame but I have no idea how to do this.
    Can anyone help me?

    I thought it might be hard to understand
    is a main window with information retrieved from a database. When you click on a button in the JTable it opens up a JDialog which edits information in the database. When you submit the changes it closes the second window down and i am wanting it to refresh the original window with the new information in the database.
    Hope this makes sense

  • Adding JCheckBox into JTable

    Hi all,
    I added a JCheckBox into a JTable, while clicking the check box the selection will not replicate into the JTable. Please help me to resolve this issue.
    * TableDemo.java
    * Created on September 5, 2006, 11:49 AM
    * To change this template, choose Tools | Template Manager
    * and open the template in the editor.
    package simPackage;
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.ButtonGroup;
    import javax.swing.DefaultCellEditor;
    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.*;
    * @author aathirai
    public class TableDemo extends JPanel{
       // public JRadioButton[] dataButton;
        /** Creates a new instance of TableDemo */
        public TableDemo() {
            JCheckBox firstCheck = new JCheckBox("");
            Object[][] data={{firstCheck,"2","4","5","2"}};
                    String[] columnNames={"qr No","supplier kt code","part no","start date","end date"};
                    JTable searchResultTable = new JTable(data,columnNames);
                    searchResultTable.getColumn("qr No").setCellRenderer(new RadioButtonRenderer());
                    searchResultTable.getColumn("qr No").setCellEditor(new RadioButtonEditor(firstCheck));
            JScrollPane scrollPane = new JScrollPane(searchResultTable);
            //Add the scroll pane to this panel.
            add(scrollPane);
    public static void main(String args[])   {
         JFrame jFrame  = new JFrame("testing");
         jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //Create and set up the content pane.
            TableDemo newContentPane = new TableDemo();
            newContentPane.setOpaque(true); //content panes must be opaque
            jFrame.setContentPane(newContentPane);
            //Display the window.
            jFrame.pack();
            jFrame.setVisible(true);
    class RadioButtonRenderer implements TableCellRenderer {
        public JCheckBox check1 = new JCheckBox("");
      public Component getTableCellRendererComponent(JTable table, Object value,
       boolean isSelected, boolean hasFocus, int row, int column) {
          check1.setBackground(Color.WHITE);
         return check1;
    class RadioButtonEditor extends DefaultCellEditor{
        public JCheckBox check1 = new JCheckBox("");
      boolean state;
      int r, c;
      JTable t;
      class RbListener implements ActionListener{
        public void actionPerformed(ActionEvent e){
          if (e.getSource() == check1){
            state = true;
          else{
            state = false;
          RadioButtonEditor.this.fireEditingStopped();
      public RadioButtonEditor(JCheckBox checkBox) {
        super(checkBox);
      public Component getTableCellEditorComponent(JTable table, Object value,
       boolean isSelected, int row, int column) {
        RbListener rb = new RbListener();
        t = table;
        r = row;
        c = column;
        if (value == null){
          return null;
        check1.addActionListener(rb);
         //check1.setSelected(((Boolean)value).booleanValue());
        /*if (((Boolean)value).booleanValue() == true){
          check1.setSelected(true);
        return check1;
      public Object getCellEditorValue() {
        if (check1.isSelected() == true){
          return Boolean.valueOf(true);
        else{
          return Boolean.valueOf(false);
    }Thanks in advance.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.table.*;
    class TableDemo extends JPanel
      public TableDemo()
        //JCheckBox firstCheck = new JCheckBox("");
        //Object[][] data={{firstCheck,"2","4","5","2"}};
        Object[][] data={{new Boolean(false),"2","4","5","2"}};
        String[] columnNames={"qr No","supplier kt code","part no","start date","end date"};
        JTable searchResultTable = new JTable(data,columnNames);
        //searchResultTable.getColumn("qr No").setCellRenderer(new RadioButtonRenderer());
        //searchResultTable.getColumn("qr No").setCellEditor(new RadioButtonEditor(firstCheck));
        TableColumn tc = searchResultTable.getColumnModel().getColumn(0);
        tc.setCellEditor(searchResultTable.getDefaultEditor(Boolean.class));
        tc.setCellRenderer(searchResultTable.getDefaultRenderer(Boolean.class));
        JScrollPane scrollPane = new JScrollPane(searchResultTable);
        add(scrollPane);
      public static void main(String args[])
        JFrame jFrame  = new JFrame("testing");
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        TableDemo newContentPane = new TableDemo();
        newContentPane.setOpaque(true); //content panes must be opaque
        jFrame.setContentPane(newContentPane);
        jFrame.pack();
        jFrame.setVisible(true);
    }

  • Problem adding JTextFeild on JTable

    Hi,
    I have a JTable and I added then added a JTextFeild to a particular column.For that i used the following code.
    public class PackageCellEditor extends DefaultCellEditor {
         public PackageCellEditor(JTextField jTextFeild) {
              super(jTextFeild);          
         public Component getTableCellEditorComponent(JTable table, Object value,
                   boolean isSelected, int row, int column) {
         if (value==null) return null;
    //     ((JTextField)getComponent()).setPreferredSize(new Dimension(2,2));
    //     ((JTextField)getComponent()).setMaximumSize(new Dimension(3,3));
    //     ((JTextField)getComponent()).setMinimumSize(new Dimension(2,2));
    //     ((JTextField)getComponent()).setOpaque(true);
    //     ((JTextField)getComponent()).set
         return (JTextField)getComponent();
    //     public Object getCellEditorValue(){
    }problem is while editing the text that i am typing is not visible completely since the size of the JTextFeild is more than the cell.Once the editing is complete the typed data is shown properly inside the cell.
    How to adjust the size of the JTextFeild inside the cell to fit it to the size of the cell itself.???
    Thnx
    Neel

    Try using getTableCellEditorComponent() instead of getComponent().

  • Multiple JButtons inside JTable cell - Dispatch mouse clicks

    Hi.
    I know this subject has already some discussions on the forum, but I can't seem to find anything that solves my problem.
    In my application, every JTable cell is a JPanel, that using a GridLayout, places vertically several JPanel's witch using an Overlay layout contains a JLabel and a JButton.
    As you can see, its a fairly complex cell...
    Unfortunately, because I use several JButtons in several locations inside a JTable cell, sometimes I can't get the mouse clicks to make through.
    This is my Table custom renderer:
    public class TimeTableRenderer implements TableCellRenderer {
         Border unselectedBorder = null;
         Border selectedBorder = null;
         public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                   boolean hasFocus, int row, int column) {
              if (value instanceof BlocoGrid) {
                   if (isSelected) {
                        if (selectedBorder == null)
                             selectedBorder = BorderFactory.createMatteBorder(2,2,2,2, table.getSelectionBackground());
                        ((BlocoGrid) value).setBorder(selectedBorder);
                   } else {
                        if (unselectedBorder == null)
                             unselectedBorder = BorderFactory.createMatteBorder(2,2,2,2, table.getBackground());
                        ((BlocoGrid) value).setBorder(unselectedBorder);
              return (Component) value;
    }and this is my custom editor (so clicks can get passed on):
    public class TimeTableEditor extends AbstractCellEditor implements TableCellEditor {
         private TimeTableRenderer render = null;
         public TimeTableEditor() {
              render = new TimeTableRenderer();
        public Component getTableCellEditorComponent(JTable table, Object value,
                boolean isSelected, int row, int column) {
             if (value instanceof BlocoGrid) {
                  if (((BlocoGrid) value).barras.size() > 0) {
                       return render.getTableCellRendererComponent(table, value, isSelected, true, row, column);
             return null;
        public Object getCellEditorValue() {
            return null;
    }As you can see, both the renderer and editor return the same component that cames from the JTable model (all table values (components) only get instantiated once, so the same component is passed on to the renderer and editor).
    Is this the most correct way to get clicks to the cell component?
    Please check the screenshot below to see how the JButtons get placed inside the cell:
    http://img141.imageshack.us/my.php?image=calendarxo9.jpg
    If you need more info, please say so.
    Thanks.

    My mistake... It worked fine. The cell span code was malfunctioning. Thanks anyway.

  • JButton in JTable with custom table model

    Hi!
    I want to include a JButton into a field of a JTable. I do not know why Java does not provide a standard renderer for JButton like it does for JCheckBox, JComboBox and JTextField. I found some previous postings on how to implement custom CellRenderer and CellEditor in order to be able to integrate a button into the table. In my case I am also using a custom table model and I was not able to create a clickable button with any of the resources that I have found. The most comprehensive resource that I have found is this one: http://www.java2s.com/Code/Java/Swing-Components/ButtonTableExample.htm.
    It works fine (rendering and clicking) when I start it. However, as soon as I incorporate it into my code, the clicking does not work anymore (but the buttons are displayed). If I then use a DefaultTableModel instead of my custom one, rendering and clicking works again. Does anyone know how to deal with this issue? Or does anyone have a good pointer to a resource for including buttons into tables? Or does anyone have a pointer to a resource that explains how CellRenderer and CellEditor work and which methods have to be overwritten in order to trigger certain actions (like a button click)?
    thanks

    Yes, you were right, the TableModel was causing the trouble, everything else worked fine. Somehow I had this code (probably copy and pasted from a tutorial - damn copy and pasting) in my TableModel:
            public boolean isCellEditable(int row, int col) {
                //Note that the data/cell address is constant,
                //no matter where the cell appears onscreen.
                if (col < 3) {
                    return false;
                } else {
                    return true;
    }A pretty stupid thing when you want to edit the 3rd column...

  • Problem in adding row in JTable

    Hi,
    I was trying to add a row in a table when the add button is clicked. But i am missing something that's why i am getting some exceptions.
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.DefaultTableColumnModel;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.TableColumn;
    import javax.swing.JButton;
    import java.util.Vector;
    import java.awt.event.*;
    public class TableDemo extends JFrame implements ActionListener {
            int rows = 1;
            int cols = 4;
            JTable table = new JTable();
            TModel model = new TModel();
            JButton button = new JButton("Add");
            Object[][] values = {
                                          {"Java","Linux","Hello","World"}
            class TModel extends DefaultTableModel {
                    public boolean isCellEditable(int parm1, int parm2){
                            return true;
                    public int getRowCount(){
                            return rows;
                    public int getColumnCount(){
                            return cols;
                    public void setValueAt(Object aValue, int aRow, int aColumn) {
                            values[aRow][aColumn] = aValue;
                    public Object getValueAt(int aRow, int aColumn) {
                            return values[aRow][aColumn];
                    public String getColumnName(int column) {
                            return "Error " + column;
            public TableDemo(String title){
                    super(title);
                    JPanel panel = new JPanel();
                    panel.setLayout(new BorderLayout());
                    table.setModel(model);
                    // Make the columns with gradually increasing width:
                    String columnName[] = {"Column", "Operator", "Values", "Logical"};
                    for (int i = 0; i < columnName.length; i++) {
                            TableColumn column = new TableColumn(i);
                            int width = 100+20*i;
                            column.setPreferredWidth(width);
                            column.setHeaderValue(columnName);
    model.addColumn(column);
    // Create the table, place it into scroll pane and place
    // the pane into this frame.
    JScrollPane scroll = new JScrollPane();
    // The horizontal scroll bar is never needed.
    scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    scroll.getViewport().add(table);
    button.addActionListener(this);
    panel.add(scroll, BorderLayout.CENTER);
    panel.add(button, BorderLayout.SOUTH);
    getContentPane().add(panel, BorderLayout.CENTER);
    public void actionPerformed(ActionEvent ae) {
    if(ae.getSource() == button) {
    Vector tempRow = new Vector();
    model.setRowCount(rows++);
    tempRow.addElement("One");//new JComboBox());
    tempRow.addElement("Two");
    tempRow.addElement("Three");
    tempRow.addElement("Four");
    model.addRow(tempRow);
    model.fireTableDataChanged();
    System.out.println("Hi");
    public static void main(String[] args) {
    TableDemo frame = new TableDemo("Table double click on the cell to edit.");
    frame.setSize(new Dimension(640, 400));
    frame.validate();
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    The code is generating the follwoing exception when add button is clicked :
    java.lang.ArrayIndexOutOfBoundsException: 2 > 1
    at java.util.Vector.insertElementAt(Vector.java:557)
    at javax.swing.table.DefaultTableModel.insertRow(DefaultTableModel.java:343)
    at javax.swing.table.DefaultTableModel.addRow(DefaultTableModel.java:319)
    at com.saijava.TableDemo.actionPerformed(TableDemo.java:96)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1786)
    at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1839)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:245)
    at java.awt.Component.processMouseEvent(Component.java:5100)
    at java.awt.Component.processEvent(Component.java:4897)
    at java.awt.Container.processEvent(Container.java:1569)
    at java.awt.Component.dispatchEventImpl(Component.java:3615)
    at java.awt.Container.dispatchEventImpl(Container.java:1627)
    at java.awt.Component.dispatchEvent(Component.java:3477)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3483)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3198)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3128)
    at java.awt.Container.dispatchEventImpl(Container.java:1613)
    at java.awt.Window.dispatchEventImpl(Window.java:1606)
    at java.awt.Component.dispatchEvent(Component.java:3477)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:480)
    at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)
    java.lang.ArrayIndexOutOfBoundsException: 1
    at com.saijava.TableDemo$TModel.getValueAt(TableDemo.java:55)
    at javax.swing.JTable.getValueAt(JTable.java:1771)
    at javax.swing.JTable.prepareRenderer(JTable.java:3724)
    at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:1149)
    at javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:1051)
    at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:974)
    at javax.swing.plaf.ComponentUI.update(ComponentUI.java:142)
    at javax.swing.JComponent.paintComponent(JComponent.java:541)
    at javax.swing.JComponent.paint(JComponent.java:808)
    at javax.swing.JComponent.paintChildren(JComponent.java:647)
    at javax.swing.JComponent.paint(JComponent.java:817)
    at javax.swing.JViewport.paint(JViewport.java:722)
    at javax.swing.JComponent.paintChildren(JComponent.java:647)
    at javax.swing.JComponent.paint(JComponent.java:817)
    at javax.swing.JComponent.paintWithOffscreenBuffer(JComponent.java:4787)
    at javax.swing.JComponent.paintDoubleBuffered(JComponent.java:4740)
    at javax.swing.JComponent._paintImmediately(JComponent.java:4685)
    at javax.swing.JComponent.paintImmediately(JComponent.java:4488)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:410)
    at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:117)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:189)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:478)
    at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)

    Umm. A few tips:
    1) You should NOT instantiate objects / initialize variables outside the constructor. And please use access-modifiers.
    2) Do not make your main-class extend JFrame or implement ActionListener. Instead create a JFrame in main and use an anonymous inner class as an ActionListener
    3) Why are you extending DefaultTableModel? To set the the column names as Error1, etc.? You can set the column names by using an appropriate constructor for JTable or by using setColumnIdentifiers(...). See the API.
    4) The actual problem most likely lies here:
    int rows = 1;
    int cols = 4;Why are you defining these outside the TableModel?

  • Problem in adding JComboBox in JTable

    Hi,
    I was trying to put some comboxes in a jtable. The problem is I am not able to add combo boxes in the 1st and 2nd column. But its adding from the 3rd column. What can be wrong in the following code?
    import javax.swing.DefaultCellEditor;
    import javax.swing.JComboBox;
    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.Dimension;
    import java.awt.GridLayout;
    import javax.swing.*;
    * TableRenderDemo is just like TableDemo, except that it
    * explicitly initializes column sizes and it uses a combo box
    * as an editor for the Sport column.
    public class TableRenderDemo1 extends JPanel {
        private boolean DEBUG = true;
            DefaultTableCellRenderer renderer =  new DefaultTableCellRenderer();
        public TableRenderDemo1() {
            //super(new GridLayout(1,0));
            JTable table = new JTable(new MyTableModel());
            table.setPreferredScrollableViewportSize(new Dimension(500, 250));
            //Create the scroll pane and add the table to it.
            JScrollPane scrollPane = new JScrollPane(table);
            //Set up column sizes.
            initColumnSizes(table);
            //Fiddle with the Sport column's cell editors/renderers.
            setUpNameColumn(table, table.getColumnModel().getColumn(3));
            //Fiddle with the Sport column's cell editors/renderers.
            setUpSportColumn(table, table.getColumnModel().getColumn(4));
            //Add the scroll pane to this panel.
            add(scrollPane);
            String strPlaf = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
            try {
                    UIManager.setLookAndFeel(strPlaf);
                    SwingUtilities.updateComponentTreeUI(scrollPane);
            } catch(Exception e) {
                    e.printStackTrace();
         * This method picks good column sizes.
         * If all column heads are wider than the column's cells'
         * contents, then you can just use column.sizeWidthToFit().
        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,
    false, false, 0, i);
    cellWidth = comp.getPreferredSize().width;
    if (DEBUG) {
    System.out.println("Initializing width of column "
    + i + ". "
    + "headerWidth = " + headerWidth
    + "; cellWidth = " + cellWidth);
    //XXX: Before Swing 1.1 Beta 2, use setMinWidth instead.
    column.setPreferredWidth(Math.max(headerWidth, cellWidth));
    public void setUpSportColumn(JTable table, TableColumn sportColumn) {
    //Set up the editor for the sport cells.
    JComboBox comboBox = new JComboBox();
    comboBox.addItem("Snowboarding");
    comboBox.addItem("Rowing");
    comboBox.addItem("Knitting");
    comboBox.addItem("Speed reading");
    comboBox.addItem("Pool");
    comboBox.addItem("None of the above");
    comboBox.setVisible(true);
    sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
    //Set up tool tips for the sport cells.
    renderer.setToolTipText("Click for combo box");
    sportColumn.setCellRenderer(renderer);
    public void setUpNameColumn(JTable table, TableColumn nameColumn) {
    //Set up the editor for the sport cells.
    JComboBox comboBox = new JComboBox();
    comboBox.addItem("One");
    comboBox.addItem("Two");
    comboBox.addItem("Three");
    comboBox.addItem("Four");
    comboBox.addItem("Five");
    comboBox.setVisible(true);
    nameColumn.setCellEditor(new DefaultCellEditor(comboBox));
    //Set up tool tips for the sport cells.
    renderer.setToolTipText("Click for combo box");
    nameColumn.setCellRenderer(renderer);
    class MyTableModel extends AbstractTableModel {
    private String[] columnNames = {"First Name",
    "Last Name",
    "Sport",
    "# of Years",
    "Vegetarian"};
    private Object[][] data = {
    {"Mary", "Saikat", "Snowboarding", new Integer(5), new Boolean(false)},
    {"Alison", "Srinath", "Rowing", new Integer(3), new Boolean(true)},
    {"Kathy", "Sheela", "Knitting", new Integer(2), new Boolean(false)},
    {"John", "Yashwant", "Something", new Integer(5), new Boolean(true)},
    {"Harry", "Jay", "Playing", new Integer(1), new Boolean(true)},
    public final Object[] longValues = {"Sharon", "Campione",
    "None of the above",
    new Integer(20), Boolean.TRUE};
    public int getColumnCount() {
    return columnNames.length;
    public int getRowCount() {
    return data.length;
    public String getColumnName(int col) {
    return columnNames[col];
    public Object getValueAt(int row, int col) {
    return data[row][col];
    * JTable uses this method to determine the default renderer/
    * editor for each cell. If we didn't implement this method,
    * then the last column would contain text ("true"/"false"),
    * rather than a check box.
    public Class getColumnClass(int c) {
    return getValueAt(0, c).getClass();
    * Don't need to implement this method unless your table's
    * editable.
    public boolean isCellEditable(int row, int col) {
    //Note that the data/cell address is constant,
    //no matter where the cell appears onscreen.
    if (col < 2) {
    return false;
    } else {
    return true;
    * Don't need to implement this method unless your table's
    * data can change.
    public void setValueAt(Object value, int row, int col) {
    if (DEBUG) {
    System.out.println("Setting value at " + row + "," + col
    + " to " + value
    + " (an instance of "
    + value.getClass() + ")");
    data[row][col] = value;
    fireTableCellUpdated(row, col);
    if (DEBUG) {
    System.out.println("New value of data:");
    printDebugData();
    private void printDebugData() {
    int numRows = getRowCount();
    int numCols = getColumnCount();
    for (int i=0; i < numRows; i++) {
    System.out.print(" row " + i + ":");
    for (int j=0; j < numCols; j++) {
    System.out.print(" " + data[i][j]);
    System.out.println();
    System.out.println("--------------------------");
    * Create the GUI and show it. For thread safety,
    * this method should be invoked from the
    * event-dispatching thread.
    private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("TableRenderDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //Create and set up the content pane.
    TableRenderDemo1 newContentPane = new TableRenderDemo1();
    newContentPane.setOpaque(true); //content panes must be opaque
    frame.setContentPane(newContentPane);
    //Display the window.
    frame.pack();
    frame.setVisible(true);
    public static void main(String[] args) {
    //Schedule a job for the event-dispatching thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    createAndShowGUI();
    Thanks in advance.

    First, I don't see where you are tying to set the combo box on the first two columns. I only see it for the 3 and 4.
    Also why are you putting combo boxes in columns that are not editable:
            public boolean isCellEditable(int row, int col) {
                //Note that the data/cell address is constant,
                //no matter where the cell appears onscreen.
                if (col < 2) {
                    return false;
                } else {
                    return true;
            }

  • How to get jbuttons under jtable and within menu?

    I've developed a menu and will like to display the results of reading a text file in a jtable with jbuttons underneath on the screen somewhere. Is there a way I can display my jbuttons underneath the jtable?, i.e.
    itemno item desc
    1111 bbb
    2222 ccc
    <button> <button>

    Yes.
    In future Swing related questions should be posted into the camickr forum.

  • Adding JButtons to a Java2d Graphics program

    Hi,
    I apologise to all you seasoned programmers if this seems an easy question, but I can't seem to see the solution to this one.
    I'm trying to add 2 JButtons to an existing Java2D graphics program. When I run the program I get the following error,
    'java.lang.IllegalArgumentException: adding a window to a container'
    I can't seem to see how to correct this error. My current code is as follows,
    import javax.swing.*;
    import javax.swing.event.*;
    import java.awt.*;
    import java.awt.geom.*;
    import java.awt.event.*;
    public class PacMan2Clicks extends JFrame implements ActionListener {
        public static int mode = 0;
        private static JButton rightButton;
        private static JButton leftButton;
        public PacMan2Clicks() {
            Container contentPane = getContentPane();
            JPanel panel = new JPanel();
            leftButton = new JButton("Leftt");
            leftButton.addActionListener(this);
            panel.add(leftButton);
            rightButton = new JButton("Right");
            rightButton.addActionListener(this);
            panel.add(rightButton);
            contentPane.add(panel, BorderLayout.SOUTH);
        public void paintComponent(Graphics g) {
            Dimension d = getSize();
            Graphics2D g2 = (Graphics2D)g;
            int size = 100;
            Ellipse2D.Double head =
                    new Ellipse2D.Double(0, 0, size, size);
            Ellipse2D.Double eye =
                    new Ellipse2D.Double(size/2-1, size/5-1,
                    size/10, size/10);
            GeneralPath mouth = new GeneralPath();
            mouth.moveTo(size, size/4);
            mouth.lineTo(size/8, size/2);
            mouth.lineTo(size, size*3/4);
            mouth.closePath();
            Area pacman = new Area(head);
            pacman.subtract(new Area(eye));
            pacman.subtract(new Area(mouth));
            g2.setPaint(Color.yellow);
            g2.fill(pacman);
            g2.setPaint(Color.black);
            g2.draw(pacman);
        public void actionPerformed(ActionEvent event) {
            if(event.getSource().equals(rightButton)) {
            } else if(event.getSource().equals(leftButton)) {
        public static void main(String[] args) {
            JFrame frame = new JFrame("Drawing stuff");
            frame.add(new PacMan2Clicks());
            frame.setSize(600, 600);
            //frame.setContentPane(new PacMan2Clicks());
            frame.setVisible(true);
            frame.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
    }Any help appreciated. Thank you.

    Your public class extends JFrame so it is a JFrame. In your main method you create a new JFrame and try to add the enclosing class (a JFrame by extension) to it. So you can either:
    1 &#8212; remove the JFrame extension from the class declaration and leave the code in the main method as&#8212;is:
    public class PM2C implements ActionListener {or,
    2 &#8212; leave the JFrame extension and remove the new JFrame instance in the main method.
    public class PM2C extends JFrame implements ActionListener {
        public PM2C(String title) {
            super(title);
            addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
            Container contentPane = getContentPane();
            JPanel panel = new JPanel();
            leftButton = new JButton("Leftt");
            leftButton.addActionListener(this);
            panel.add(leftButton);
            rightButton = new JButton("Right");
            rightButton.addActionListener(this);
            panel.add(rightButton);
            contentPane.add(panel, BorderLayout.SOUTH);
            setSize(600, 600);
            setVisible(true);
        public static void main(String[] args) {
            new PM2C("Drawing stuff");
    }

  • Adding JButton in JList

    Hi
    I wonder if it is possible to add JButton in a JList? Or how can I produce a list of JButtons? Each button represents a person (an object), is there anyway to add listener to the buttons so i can get the person thats represnted by the button? I can decide in advance how many button ther will be...that depends on the number of person in the db.

    Use a panel with a GridLayout. The Panel can be added to a JScrollPane. Read this section on "Layout Managers":
    http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html

  • Clicking JButton in JTable

    Hello all,
    I have populated a JTable with Buttons and need for them to appear to be clicked (become depressed) when they are clicked.
    Currently, I have found some way to handle click events on them, but the display is not updated to show that they are clicked.
    How can this be done??
    fyi, the click code adds a mouse listener to the table, which determines which cell is clicked based on location and forwards the event to appropriate handler. it goes something like this:
    private void forwardEventToHandlers(MouseEvent e) {
             TableColumnModel columnModel = _table.getColumnModel();
             _column = columnModel.getColumnIndexAtX(e.getX());
             _row    = e.getY() / _table.getRowHeight();
             Object value;
             JButton button = new JButton();
             MouseEvent buttonEvent;
             if(_row >= _table.getRowCount() || _row < 0 ||
                _column >= _table.getColumnCount() || _column < 0)
               return;
             value = _table.getValueAt(_row, _column);
                              // forward an event to handler...
             }

    I have a couple of attempts at this that you may be able to use or at least give you some ideas:
    a) Original attempt
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.table.*;
    public class TableButton extends JFrame
        public TableButton()
            String[] columnNames = {"Date", "String", "Integer", "Decimal", "Boolean"};
            Object[][] data =
                {new Date(), "A", new Integer(1), new Double(5.1), new JButton("Delete")},
                {new Date(), "B", new Integer(2), new Double(6.2), new JButton("Delete")},
                {new Date(), "C", new Integer(3), new Double(7.3), new JButton("Delete")},
                {new Date(), "D", new Integer(4), new Double(8.4), new JButton("Delete")}
            DefaultTableModel model = new DefaultTableModel(data, columnNames);
            final JTable table = new JTable( model )
                //  Returning the Class of each column will allow different
                //  renderers to be used based on Class
                public Class getColumnClass(int column)
                    return getValueAt(0, column).getClass();
                //  Don't edit the button column
                public boolean isCellEditable(int row, int column)
                    return column != 4;
            table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
              table.setPreferredScrollableViewportSize(table.getPreferredSize());
            JScrollPane scrollPane = new JScrollPane( table );
            getContentPane().add( scrollPane );
            //  Create button renderer
            TableCellRenderer buttonRenderer = new ButtonRenderer();
            table.setDefaultRenderer(JButton.class, buttonRenderer);
            //  Add table mouse listener
            table.addMouseListener( new ButtonListener(table, 4) );
        public static void main(String[] args)
    //        try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); }
    //        catch(Exception e) {}
            TableButton frame = new TableButton();
            frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
            frame.pack();
            frame.setLocationRelativeTo( null );
            frame.setVisible(true);
        class ButtonRenderer extends JButton implements TableCellRenderer
            public Component getTableCellRendererComponent(
                JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
                JButton button = (JButton)value;
                if (button == null)
                    setText( "" );
                    getModel().setPressed( false );
                    getModel().setArmed( false );
                else
                    setText( button.getText() );
                    getModel().setPressed( button.getModel().isPressed() );
                    getModel().setArmed( button.getModel().isArmed() );
                return this;
         *  1) Creation of class will determine the column to process mouse events on
         *  a) Mouse pressed will determine the row to process and paint pressed button
         *  b) Mouse clicked will do the actual processing
         *  c) Mouse released will paint the normal button
        class ButtonListener extends MouseAdapter
            private JTable table;
            //  Column from the data model to process mouse events on
            private int column;
            //  The table row when the mouse was pressed
            private int row;
            //  The table column when the mouse was pressed
            private int tableColumn;
            //  Repaint the button on mouse released event
            private boolean paintOnRelease;
            ButtonListener(JTable table, int column)
                this.table = table;
                this.column = column;
             *  Repaint button to show pressed state
            public void mousePressed(MouseEvent e)
                //  Make sure the MouseEvent was on the button column
                if ( !buttonColumn(e) ) return;
                //  Repaint the button for the current row/column
                row = table.rowAtPoint( e.getPoint() );
                tableColumn = table.columnAtPoint( e.getPoint() );
                paintButton( true );
                paintOnRelease = true ;
             *  Do table processing on this event
            public void mouseClicked(MouseEvent e)
                //  Make sure the MouseEvent was on the button column
                if ( !buttonColumn(e) ) return;
                //  Only process a single click
                if (e.getClickCount() > 1) return;
                //  Delete current row from the table
                DefaultTableModel model = (DefaultTableModel)table.getModel();
                model.removeRow( this.row );
                //  Row has been deleted, nothing to repaint
                paintOnRelease = false;
             *  Repaint button to show normal state
            public void mouseReleased(MouseEvent e)
                if (paintOnRelease)
                    paintButton( false );
                paintOnRelease = false;
            private boolean buttonColumn(MouseEvent e)
                //  In case columns have been reordered, we must map the
                //  table column to the data model column
                int tableColumn = table.columnAtPoint( e.getPoint() );
                int modelColumn = table.convertColumnIndexToModel(tableColumn);
                return modelColumn == column;
            private void paintButton(boolean pressed)
                //  Make sure we have a JButton before repainting
                Object o = table.getValueAt(row, tableColumn);
                if (o instanceof JButton)
                    JButton button = (JButton)o;
                    button.getModel().setPressed( pressed );
                    button.getModel().setArmed( pressed );
                    table.setValueAt(button, row, tableColumn);
    }b) Latest attempt:
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.table.*;
    public class TableButton3 extends JFrame
        public TableButton3()
            String[] columnNames = {"Date", "String", "Integer", "Decimal", ""};
            Object[][] data =
                {new Date(), "A", new Integer(1), new Double(5.1), "Delete0"},
                {new Date(), "B", new Integer(2), new Double(6.2), "Delete1"},
                {new Date(), "C", new Integer(3), new Double(7.3), "Delete2"},
                {new Date(), "D", new Integer(4), new Double(8.4), "Delete3"}
            DefaultTableModel model = new DefaultTableModel(data, columnNames);
            JTable table = new JTable( model )
                //  Returning the Class of each column will allow different
                //  renderers to be used based on Class
                public Class getColumnClass(int column)
                    return getValueAt(0, column).getClass();
            JScrollPane scrollPane = new JScrollPane( table );
            getContentPane().add( scrollPane );
            //  Create button column
            ButtonColumn buttonColumn = new ButtonColumn(table, 4);
        public static void main(String[] args)
            TableButton3 frame = new TableButton3();
            frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
            frame.pack();
            frame.setVisible(true);
        class ButtonColumn extends AbstractCellEditor
            implements TableCellRenderer, TableCellEditor, ActionListener
            JTable table;
            JButton renderButton;
            JButton editButton;
            String text;
            public ButtonColumn(JTable table, int column)
                super();
                this.table = table;
                renderButton = new JButton();
                editButton = new JButton();
                editButton.setFocusPainted( false );
                editButton.addActionListener( this );
                TableColumnModel columnModel = table.getColumnModel();
                columnModel.getColumn(column).setCellRenderer( this );
                columnModel.getColumn(column).setCellEditor( this );
            public Component getTableCellRendererComponent(
                JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
                if (hasFocus)
                    renderButton.setForeground(table.getForeground());
                    renderButton.setBackground(UIManager.getColor("Button.background"));
                else if (isSelected)
                    renderButton.setForeground(table.getSelectionForeground());
                     renderButton.setBackground(table.getSelectionBackground());
                else
                    renderButton.setForeground(table.getForeground());
                    renderButton.setBackground(UIManager.getColor("Button.background"));
                renderButton.setText( (value == null) ? "" : value.toString() );
                return renderButton;
            public Component getTableCellEditorComponent(
                JTable table, Object value, boolean isSelected, int row, int column)
                text = (value == null) ? "" : value.toString();
                editButton.setText( text );
                return editButton;
            public Object getCellEditorValue()
                return text;
            public void actionPerformed(ActionEvent e)
                fireEditingStopped();
                System.out.println( e.getActionCommand() + " : " + table.getSelectedRow());
    }

Maybe you are looking for

  • How is the best way to restore Thumbnails?

    I'm an experienced Mac user and have used iPhoto since it began. I have carefully arranged my iPhoto collection over the years so it has always served me well. I am currently running iPhoto 11. Yesterday, out of the blue, when I started iPhoto I rece

  • Windows 2008 Failover Cluster - Cannot add a generic service

    Trying to add a generic service in a failover cluster. Select the option Services and Application and it opens the wizard and then displays the error "An error was encountered while loading the list of services. QueryServiceConfig failed. The system

  • Register iPhone "Password cannot contain spaces"

    I want to register my iPhone but cannot login to my existing Apple ID because the screen shows an error saying my password cannot contain spaces... However, my actual password does contain space(s), and I have never experienced issues with logging in

  • CATS IDoc inbound problem - Transaction lock, LR002

    Customer is posting CATS records via interface from externa system to IDoc for CATS processing. Either the create BAPI (BAPI_...) is used or if there is a change to an already existing posting (BAPI_...). For his i have slightly modified the SAP stan

  • From LR 3 edit in Ps CS5 in 8 bit/channel

    When I take a photo from LR3 to Ps CS5 (running 64bit on Mac OSX 10.6.2), it is not coming as a 8 bits/channel. Some of my presets apply only on a 8 bits/ch picture. How can I send a file in 8 b/ch from LR3 to Ps? Other question is, where can I see t