• Post Reply Bookmark Topic Watch Topic
  • New Topic

what am I doing wrong? JTable cell renderer  RSS feed

 
john mattucci
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JTable with one column which will take a
MM/DD/YY argument. I want to repeat the MM/DD/YY string throughout the column. My problem now is 1)I cant for the life of me get the MM/DD/YY string to appear in each cell of the column and 2)
I need to place the cursor in each cell in order for it to read the users input properly, otherwise if i simply highlight it it wont work>>
What am I doing wrong I really need to solve this one. Thank you all
dm = new DefaultTableModel(5, 8);

table = new JTable(dm);
TableColumn column = table.getColumnModel ().getColumn(6);
column.setCellRenderer(new DateField());
public class DateField extends JTextField implements TableCellRenderer
{
Component stubRenderer = new NothingComponent ();
class NothingComponent extends JComponent
{
public void paint(Graphics g)
{
setText("MM/DD/YY");
}
}
}
public DateField()
{
setText("MM/DD/YY");
addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
textPressed(e);
}
public void keyReleased(KeyEvent e)
{
textReleased(e);
}
});
}
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column)
{
if(value != null && !value.equals("MM/DD/YY"))
{
setText((String)value);
return this;
}
else if(value != null && value.equals("MM/DD/YY"))
{
setText("MM/DD/YY");
return this;
}
else
{
return stubRenderer;
}
}
public void textPressed(KeyEvent e)
{
if(e.getKeyCode() != KeyEvent.VK_ENTER)
{
String s = getText();
if(s.equals("MM/DD/YY"))
{
setText("");
}
else if(s.length() == 2)
setText(s + "/");
else if(s.length() == 5)
setText(s + "/");
else if(s.length() == 8 && !s.equals("MM/DD/YY"))
setText(determineDayTwo(s));
else if(!s.equals("MM/DD/YY") && s.length() > 8)
setText("");
}
}
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!