Results 1 to 4 of 4
  1. #1
    djc
    djc is offline Member
    Join Date
    Jul 2008
    Posts
    10
    Rep Power
    0

    Default Adding a progress bar to a table

    Hello everyone,

    I'm very new to netbeans, and I was wondering if it is possible to make one column of a table have progress bars instead of text fields. What I tried doing was declare a progress bar, and then use that object in the table model.

    private javax.swing.JProgressBar jProgressBar1;
    jProgressBar1 = new javax.swing.JProgressBar();
    jTable1.setModel(new javax.swing.table.DefaultTableModel(
    new Object [][] {
    {null, jProgressBar1, null, null},
    {null, null, null, null},
    {null, null, null, null},
    {null, null, null, null}
    },
    ...
    )

  2. #2
    djc
    djc is offline Member
    Join Date
    Jul 2008
    Posts
    10
    Rep Power
    0

    Default

    I think I should rephrase the question. I'm now wondering how to customize code in netbeans. I read here //java.sun.com/docs/books/tutorial/uiswing/components/table.html#combobox that you are able to change the editor for a column of a table to a combobox using this code:

    TableColumn sportColumn = table.getColumnModel().getColumn(2);
    JComboBox comboBox = new JComboBox();
    comboBox.addItem("Snowboarding");
    comboBox.addItem("Rowing");
    comboBox.addItem("Chasing toddlers");
    comboBox.addItem("Speed reading");
    comboBox.addItem("Teaching high school");
    comboBox.addItem("None");
    sportColumn.setCellEditor(new DefaultCellEditor(comboBox));

    I've put this code in every spot that I could in the code that is created by NetBeans when a table is inserted, but no changes have occurred.

    This is the default code generated by netbeans when a table is created:

    jTable1 = new javax.swing.JTable();

    jTable1.setModel(new javax.swing.table.DefaultTableModel(
    new Object [][] {
    {null, null, null, null},
    {null, null, null, null},
    {null, null, null, null},
    {null, null, null, null}
    },
    new String [] {
    "Title 1", "Title 2", "Title 3", "Title 4"
    }
    ));

    jTable1.setName("jTable1"); // NOI18N

    jScrollPane1.setViewportView(jTable1);

    I assumed that the best spot to place the custom code would be after the table model has been set, but as I said before I've tried putting the new chunk of code in every place possible. So my new question is, how the heck do you actually customize code in NetBeans?

  3. #3
    djc
    djc is offline Member
    Join Date
    Jul 2008
    Posts
    10
    Rep Power
    0

    Default

    Well, apparently the design preview isn't such a great tool after all. The code works when I actually compile the app and run it, but isn't shown with the preview. Sorry for the confusion everyone.

  4. #4
    djc
    djc is offline Member
    Join Date
    Jul 2008
    Posts
    10
    Rep Power
    0

    Default

    Ok, so I now am able to have table cells that are progress bars, but only show the progress bar and update the values of the progress bar if the cell is selected. What I want is for the cell to automatically update its value regardless of it being selected. Does anyone know if this is possible?

    This is my code for my progress bar cell editor. Most of it is borrowed from the tutorial in the link that I posted earlier.

    public class ProgressBarEditor extends AbstractCellEditor implements TableCellEditor, PropertyChangeListener {

    public JProgressBar percentBar;
    public int percent;
    public boolean done;

    class Task extends SwingWorker<Void, Void> {
    /*
    * Main task. Executed in background thread.
    */
    @Override
    public Void doInBackground() {
    Random random = new Random();
    int progress = 0;
    //Initialize progress property.
    setProgress(0);
    while (progress < 100) {
    //Sleep for up to one second.
    try {
    Thread.sleep(random.nextInt(1000));
    } catch (InterruptedException ignore) {}
    //Make random progress.
    progress += random.nextInt(10);
    setProgress(Math.min(progress, 100));
    }
    return null;
    }
    }


    public ProgressBarEditor() {
    percentBar = new JProgressBar();
    percentBar.setBorderPainted(true);
    percentBar.setStringPainted(true);
    percent = 0;
    done = false;
    Task task = new Task();
    task.addPropertyChangeListener(this);
    task.execute();
    }

    /**
    * Invoked when task's progress property changes.
    */
    public void propertyChange(PropertyChangeEvent evt) {
    if ("progress" == evt.getPropertyName()) {
    percent = (Integer) evt.getNewValue();
    percentBar.setValue(percent);
    }
    }



    @Override
    public Component getTableCellEditorComponent(JTable table, Object value,
    boolean isSelected, int row, int column) {
    return percentBar;
    }

    @Override
    public Object getCellEditorValue() {
    return percent;
    }

    Then in my NetBeans table code I added

    TableColumn progColumn = jTable1.getColumnModel().getColumn(2);
    progColumn.setCellEditor(new ProgressBarEditor());

    I assume that when a cell is not selected, getCellEditorValue() is called and that value is stored in the cell. I assume this because when I load the program, if I select a cell the progress bar displays and updates, however when I select a different cell, the value of the first cell that I selected stays constant at whatever progress it was last at. Yet, if I select it again, it's progress is much past the static value that was saved, meaning the progress is still updating but the cell value isn't. What I would like it to do is to keep calling getCellEditorValue() and updating the value of the cell even when it is not selected. However, I don't see any method in any of the APIs that manually sets a cell's value.
    Last edited by djc; 07-11-2008 at 12:34 AM.

Similar Threads

  1. Placing a progress bar in a SWT table
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-07-2008, 04:59 PM
  2. how to implement progress bar .....
    By roshithmca in forum AWT / Swing
    Replies: 1
    Last Post: 03-06-2008, 04:18 PM
  3. Console Progress Bar
    By new_2_java in forum New To Java
    Replies: 1
    Last Post: 02-16-2008, 02:18 AM
  4. Table Help
    By CoOlbOyCoOl in forum NetBeans
    Replies: 1
    Last Post: 05-06-2007, 04:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •