[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

JTable Swing

Posted on 2009-04-28
3
Medium Priority
?
368 Views
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.
0
Comment
Question by:jdpositive
2 Comments
 
LVL 37

Accepted Solution

by:
zzynx earned 2000 total points
ID: 24257827
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"
0
 

Expert Comment

by:dilandesilva
ID: 26157028
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()
  {
    initialize();
  }

  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();
    scrollPane.setViewportView(table);
    table.setModel(tableModel);
    table.setDefaultRenderer(String.class, new StrinProgressBarDemoRenderer());
  }

  public static void main(String[] args)
  {
    JFrame jf = new JFrame("StrinProgressBarDemo");
    StrinProgressBarDemo e = new StrinProgressBarDemo();
    jf.setContentPane(e);
    jf.setSize(500, 200);
    jf.setLocation(100, 10);
    jf.setVisible(true);
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }

  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()
    {
      progressBar.setMinimum(0);
      progressBar.setMaximum(100);
    }

    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)
        {
          progressBar.setValue(Integer.parseInt(cellData));
          return progressBar;
        }
      }
      return null;
    }
  }
}

Open in new window

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses
Course of the Month18 days, 4 hours left to enroll

830 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