0

In my small project, I am parsing some logfiles and showing on JTable. In Jtable there are 2 columns.

Second column is for => shows the searching sentence First column is for => shows where is the this sentence (in which log file ?)

There is a screenshot of my result screen.

enter image description here

Now, my problem is, I want to put JButtons on first column. But when I expect the show Jbutton I cant.

Here is the my code :

import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.GridLayout;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

public class ResultGui 
{
    public final static boolean RIGHT_TO_LEFT = false;
    private static JButton but;
    private static JTable table;
    public static void addComponentsToPane(Container contentPane,List<String> result, List<String>      logFiles) 
    {
       if (RIGHT_TO_LEFT)
       {
        contentPane.setComponentOrientation(
        ComponentOrientation.RIGHT_TO_LEFT);
       }
       contentPane.setLayout(new GridLayout(0,1));
       //list to array
       String[] data = result.toArray(new String[result.size()]);
       DefaultTableModel dm = new DefaultTableModel();
       Object [][] objectArray = new Object[logFiles.size()][result.size()];
       for(int i=0; i<logFiles.size(); i++)
       {
         but = new JButton("LogFile");
         but.setToolTipText(logFiles.get(i));;
         but.setOpaque(true);
         objectArray [i][0] = but;
         //objectArray [i][0] = logFiles.get(i);
         i++;
         objectArray [i][0] = "";
        }    
       for(int i=0; i<result.size(); i++)
       {
         objectArray[i][1] = result.get(i);
       } 
       dm.setDataVector(objectArray, new Object[] { "LOGFILES", "RESULTS" });
       //dm.addColumn("deneme", strArray2Vector(data));  
       table = new JTable(dm);
       TableColumn columnA = table.getColumn("LOGFILES");
       columnA.setMinWidth(30);
       columnA.setMaxWidth(100);
       JScrollPane scrollPane = new JScrollPane(table,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
       contentPane.add(scrollPane);
    }

    public static void createAndShowGUI(List<String> stringList, List<String> logFiles)   
    {
      JFrame.setDefaultLookAndFeelDecorated(true);
      JFrame frame = new JFrame("Result Screen");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      //Set up the content pane and components in GridLayout
      addComponentsToPane(frame.getContentPane(),stringList,logFiles);
      frame.setSize(1000,500);
      //frame.pack();
      frame.setVisible(true);
    }
}

What should I do for successfully create Jbutton in my table ? Any example, hint or advice is appreciated.

Ganesh Pravin
  • 79
  • 1
  • 13
Batuhan B
  • 1,835
  • 4
  • 29
  • 39
  • 2
    possible duplicate of [Making a JButton clickable inside a JTable](http://stackoverflow.com/questions/10347983/making-a-jbutton-clickable-inside-a-jtable) – RamonBoza Nov 15 '13 at 10:33

2 Answers2

1

I want to put JButtons on first column

Add text the the model the way you do for any other column. Then you can use the Table Button Column to render and handle button clicks.

camickr
  • 321,443
  • 19
  • 166
  • 288
-1

The way to show it is the folllowing

table.setDefaultRenderer(JComponent.class, new MyTableCellRenderer());
table.setDefaultEditor(JComponent.class, new MYTableCellEditor());


class MyTableCellRenderer extends DefaultTableCellRenderer implements TableCellRenderer {
    private static final long serialVersionUID = 1L;

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

class PFTableCellEditor extends AbstractCellEditor implements TableCellEditor {
    private static final long serialVersionUID = 1L;
    private JComponent item;

    @Override
    public Object getCellEditorValue() {
        return item;
    }

    @Override
    public Component getTableCellEditorComponent(JTable table,
        Object value, boolean isSelected, int row, int column) {
        item = (JComponent)value;
        item.setBackground(table.getSelectionBackground());
        return item;
    }       
}

and last

table.setModel(new DefaultTableModel() {    
    public boolean isCellEditable(int row, int column) {
            return column == 0;
    };
};

With This JTABLE setting you can put directly your jbutton as cell content

Matteo Gatto
  • 616
  • 11
  • 28
  • @BatuhanBardak: See also [*How to Use Tables—Concepts: Editors and Renderers*](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#editrender). – trashgod Nov 15 '13 at 11:31
  • I'd like to know the reason of the downvote... i think i will not answer anything next time cause of those people....... – Matteo Gatto Nov 15 '13 at 13:43
  • 2
    Well, I didn't down vote but this is not a good solution. You should NOT be adding Swing components to the TableModel. The whole point of using a renderer is to use one Swing component to render the text found in the TableModel. If this table had 1000 rows if would be terribly inefficient to store 1000 JButtons in the TableModel. So this approach shows a lack of understanding on how renderers/editors were designed to be used with a JTable and other Swing components that use renderers. – camickr Nov 15 '13 at 16:10