aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes adding components into a cell in jtable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "adding components into a cell in jtable" Watch "adding components into a cell in jtable" New topic
Author

adding components into a cell in jtable

Samer Telawi
Greenhorn

Joined: Mar 23, 2013
Posts: 18
Dears,
would you please suggest me what is the best way to add multiple components into a table's cell ( button,textfield, jlabel and icon ..)
should I define a jpanel that contains these components and insert this jpanel into the cell by defining a new renderer.
please see the photo
waiting for your suggestions ... if possible some few lines of code as an example
thank you very much


[Unbenannt.JPG]

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4664
    
    5

Samer Telawi wrote:should I define a jpanel that contains these components and insert this jpanel into the cell by defining a new renderer.

In a general way, yes.

Note that the corresponding slot in your table model should not contain Swing components, but rather an object of a class that encapsulates the state that is to be represented in the table cell. The custom renderer will then cast the value parameter to this class and extract the information for display.


luck, db
There are no new questions, but there may be new answers.
Ramesh Pramuditha Rathnayake
Ranch Hand

Joined: Oct 31, 2012
Posts: 175
    
    1

You can make a class by extending JPanel and implementing TableCellRenderer. You can add multiple components into that JPanel.



Give an object of that class as the cell renderer of the corresponding cell.


Ramesh-X
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4664
    
    5

Ramesh Pramuditha Rathnayake wrote:You can make a class by extending JPanel and implementing TableCellRenderer.

Give an object of that class as the cell renderer of the corresponding cell.


There's no call to extend JPanel. The class can contain a JPanel field. Favor composition over inheritance.

And the primary advantage of using a renderer is lost if a new JPanel is constructed for each get...RendererComponent(...) call. You should update the state to provide the desired visual representation of the value parameter and return the same instance.
Ramesh Pramuditha Rathnayake
Ranch Hand

Joined: Oct 31, 2012
Posts: 175
    
    1

Darryl Burke wrote: There's no call to extend JPanel. The class can contain a JPanel field. Favor composition over inheritance...


What class contains a JPanel field..?? I can't understand what you said..
DefaultTableCellRenderer class has defined by implementing TableCellRenderer. But it is extended by JLabel. Therefor this class cannot be used as the renderer here..
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8
Here is an example to get you started:

Samer Telawi
Greenhorn

Joined: Mar 23, 2013
Posts: 18
thank you all
thank you very much
Samer Telawi
Greenhorn

Joined: Mar 23, 2013
Posts: 18
Dears I tried what you've suggested and I made some progress but I still have some troubles that I can not understand.
here is my code:
public class ButtonTextCell extends AbstractCellEditor implements TableCellRenderer, TableCellEditor{
String value ="default";
JPanel panel = new JPanel();
JButton button;
JTextField text;

public ButtonTextCell(){
super();
button = new JButton("...");
button.setPreferredSize(new Dimension(16,16));
button.setFocusable(false);
button.setRolloverEnabled(false);
button.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//fireEditingStopped();
JOptionPane.showMessageDialog(null, "Hello");
//fireEditingStopped();
}
});
text = new JTextField(value);
text.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e) {
value = text.getText();
//fireEditingStopped();
}

});
panel.setLayout(new BorderLayout());
panel.add(button,BorderLayout.EAST);
panel.add(text,BorderLayout.CENTER);
}

@Override
public Object getCellEditorValue() {
// TODO Auto-generated method stub
return null;
}

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

@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
//text.setBackground(isSelected?table.getSelectionBackground():table.getBackground());
if(isSelected){
text.setBackground(table.getSelectionBackground());
button.setBackground(table.getSelectionBackground());

}else
text.setBackground(table.getBackground());
button.setBackground(table.getBackground());
if (value == null
|| value.toString().length() == 0)
return panel;


return panel;
}



}
and the problems are like the following:
1- sometimes when I run this code I get the table but the second column is empty, and I should do some action like resizing or clicking to show the components in the second column as photo number 1 shows.
2- when I click on the left column everything is ok and the complete row is selected, but when I click on the second column (the right one) just the right column is selected and the current one becomes white and all components disappear. beside when I click on the button sometimes the same scenario happens and the color of the button does not change when I click another one. it is shown in photo number 2

would you please check the code for me and tell what to do ... I believe there is something missing ...


[Thumbnail for 1.JPG]

[Thumbnail for 2.JPG]

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: adding components into a cell in jtable