Win a copy of Functional Design and Architecture this week in the Functional programming forum!
  • 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

what am I doing wrong? JTable cell renderer

 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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("");
}
}
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic