• Post Reply Bookmark Topic Watch Topic
  • New Topic

Urgent!!!Want to add different panels in different rows..  RSS feed

 
SantoshKr Tripathi
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am trying to add different Panels in differnt rows of same column..
But each time i am getting the default panel which is being retuned by the MyTableCellRenderer class which i have implemented from TableCellRenderer .
Would you like reply whats wrong in my approach...Can anyone modify my code???



public class TablePanel extends JPanel
{

JTable alertInsertTable;
JScrollPane alertInsertPane;
String columnnames[]={"Insert Alert"};
Object[][] values={
{
new JPanel()
}
};

DefaultTableModel tableModel= new DefaultTableModel(values,columnnames);


TablePanel()
{

setLayout(new BorderLayout());
alertInsertTable= new JTable(tableModel);
alertInsertPane = new JScrollPane(alertInsertTable);
alertInsertTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);

this.add(alertInsertPane,BorderLayout.CENTER);

setDataInTable();

}

public void setDataInTable()
{
int count=0;

alertInsertTable.setRowHeight(80);


MyTableCellRenderer ob=new MyTableCellRenderer();
this.alertInsertTable.getColumn("Insert Alert").setCellRenderer(ob);

JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
panel1.add(new JLabel("HIiiiiiiiiiiiiiii"));
panel2.add(new JLabel("Hellooooooooooooo"));


Object[] ob1={panel1};
Object[] ob2={panel2};

model.addRow(ob1);
//its always adding to the Panel with label new JLabel("Oppsssss")
//which is the panel instance returned by MyTableCellRender

model.addRow(ob2);
// this also adding to the Panel with label new JLabel("Oppsssss")
//which is the panel instance returned by MyTableCellRender


// ? What code should i write here so that i can add panel1 and panel2
//which i have created here.

}

}








class MyTableCellRenderer extends JPanel implements TableCellRenderer
{

public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex)
{
JPanel panel = new JPanel();

panel.add(new JLabel("Oppsssss"));

return panel;
}
}
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


A TableCellRenderer provides a re-usable component for every table cell. Inside your getTableCellRendererComponent() method, you are creating a new panel, adding a label to it, and returning it. This means that for every table cell, a brand new JPanel gets created, and a brand new label with the String "Oppsssss" gets created and added to the panel, and that gets returned.

Inside the setDataInTable() method of your outer class, you are creating new panels, with new labels and setting them as the data in the model of your table. However, since you set the renderer for this table to always return a new panel with a new "Oppsssss" label every time, no matter what is in your model - a new panel with a new "Oppsssss" label will always be returned from the renderer.

Instead of adding Components (i.e. panels, labels, etc.) to your model, you should be adding Data (i.e. Strings, booleans, bigger "model" objects that hold "data", etc.). Your renderer should take that "Data" from your model and figure out how to present it (and it should reuse itself, it shouldn't create a new component every time...). For example, if in your "setDataInTable()" method, you could do something like -


The default table cell renderer knows how to render Strings, so this should be fine with no new renderer... however, if you wanted to do something special, like render alternating rows different colors, make the text green, and make the selected cell render as italic, you could do something like -
 
SantoshKr Tripathi
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hiii Nathan,
I didnt want to add Different type of Strings...
It was like i want to add different types of Panels in different rows,such as a panel containing Buttons,Labels etc...and another Panel containing
TextArea,Combo etc..
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not just add all the JPanels to an outer JPanel with a one column GridLayout? That seems a lot simpler.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!