• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

adding components into a cell in jtable

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
[Thumbnail for Unbenannt.JPG]
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 178
2
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 178
2
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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..
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is an example to get you started:

 
Samer Telawi
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you all
thank you very much
 
Samer Telawi
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ...
1.JPG
[Thumbnail for 1.JPG]
2.JPG
[Thumbnail for 2.JPG]
 
Sasparilla and fresh horses for all my men! You will see to it, won't you tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic