Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show "no rows found" inside a JTable row if not found entry while filtering

Tags:

swing

i have a JTable with row filter.

once fitered if i didn't get any row then i have to show a string like "Nothing found to display " inside table as first row.

please do needful.

Thanks , Narasimha

like image 290
Narasimha Rao Avatar asked Jan 21 '23 21:01

Narasimha Rao


1 Answers

I needed to solve this exact problem today and wasn't able to find a good answer online. In the end I came up with my own solution, which I think may be exactly what you want. I don't know if this is too late for your project, but perhaps it can help others:

JTable.paintComponent(Graphics g) will not be called unless the table has a height that is greater than 0. This causes a problem for empty tables, so we expand the JTable height if needed so that it will always be at least the size of the JViewport that is it's parent.

JTable tableName =  new JTable() {
  public boolean getScrollableTracksViewportHeight() {
    if(getParent() instanceof JViewport)
      return(((JViewport)getParent()).getHeight() > getPreferredSize().height);

    return super.getScrollableTracksViewportHeight();
  }

  protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    if(getRowCount() == 0) {
      Graphics2D g2d = (Graphics2D) g;
      g2d.setColor(Color.BLACK);
      g2d.drawString("Nothing found to display.",10,20);
    }
  }
}

containerName.add(new JScrollPane(tableName));
like image 199
MaverickXero Avatar answered Dec 24 '23 09:12

MaverickXero