• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 374
  • Last Modified:

JTable Swing

Can I have string in one row and progress bar in another row of the same column of JTable in swing.If yes can anyone help me in getting this down.
1 Solution
zzynxSoftware engineerCommented:
Yes, that's possible.
You'll have to write your own custom table cell renderer that will - depending on the value of the cell or the row number - show a String or a progress bar.
See http://java.sun.com/docs/books/tutorial/uiswing/components/table.html paragraph "Using Custom Renderers"
Solution is to use a custom cell rendererfor your table.
Here's a small demo:
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;

public class StrinProgressBarDemo extends JPanel
  private JTable table = null;

  private JScrollPane scrollPane = null;

  private StrinProgressBarTableModel tableModel = null;

  public StrinProgressBarDemo()

  private void initialize()
    GridBagConstraints gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.weightx = 1.0;
    gridBagConstraints.weighty = 1.0;
    gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
    this.setLayout(new GridBagLayout());
    table = new JTable();
    scrollPane = new JScrollPane();
    this.add(scrollPane, gridBagConstraints);
    tableModel = new StrinProgressBarTableModel();
    table.setDefaultRenderer(String.class, new StrinProgressBarDemoRenderer());

  public static void main(String[] args)
    JFrame jf = new JFrame("StrinProgressBarDemo");
    StrinProgressBarDemo e = new StrinProgressBarDemo();
    jf.setSize(500, 200);
    jf.setLocation(100, 10);

  class StrinProgressBarTableModel extends AbstractTableModel
    private String[][] data = new String[][] { { "Cell 1", "Cell 2", "Cell 3", "Cell 4" }, { "20", "50", "10", "80" } };

    public int getRowCount()
      return data.length;

    public int getColumnCount()
      return data.length == 0 ? 0 : data[0].length;

    public Object getValueAt(int rowIndex, int columnIndex)
      return data[rowIndex][columnIndex];

    public Class<?> getColumnClass(int columnIndex)
      return String.class;


  class StrinProgressBarDemoRenderer implements TableCellRenderer
    private JProgressBar progressBar = new JProgressBar();

    public StrinProgressBarDemoRenderer()

    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
                                                   int row, int column)
      if (value instanceof String)
        String cellData = (String) value;
        if (row == 0)
          return new JLabel(cellData);
        else if (row == 1)
          return progressBar;
      return null;

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now