|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

07-10-2008, 12:38 AM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 10
|
|
|
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}
},
...
)
|
|

07-10-2008, 09:27 PM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 10
|
|
|
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?
|
|

07-10-2008, 10:14 PM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 10
|
|
|
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.
|
|

07-11-2008, 02:31 AM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 10
|
|
|
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 02:34 AM.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|