JTable Swing

Posted on 2009-04-28
Last Modified: 2013-11-23
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.
Question by:jdpositive
    LVL 37

    Accepted Solution

    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 paragraph "Using Custom Renderers"

    Expert Comment

    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


    Featured Post

    Gigs: Get Your Project Delivered by an Expert

    Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

    Join & Write a Comment

    In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
    Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project ( automates most of the tasks discussed in this article. You can even fin…
    The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
    The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now