How do I clear a table before writing all the rows? The data will be completely different every time I hit this code:
graphicsMemoryTable.setRedraw(false);
int count = 0;
for(int i=0; i<graphicsMemory.size() && count<1000; i+=8,count++)
{
TableItem item = new TableItem(graphicsMemoryTable, SWT.NONE);
item.setText(graphicsMemory.get(i).toString());
item.setText(1,graphicsMemory.get(i+1).toString());
item.setText(2,graphicsMemory.get(i+2).toString());
item.setText(3,graphicsMemory.get(i+3).toString());
item.setText(4,graphicsMemory.get(i+4).toString());
item.setText(5,graphicsMemory.get(i+5).toString());
item.setText(6,graphicsMemory.get(i+6).toString());
item.setText(7,graphicsMemory.get(i+7).toString());
}
graphicsMemoryTable.setRedraw(true);
EDIT:
For reference, calling removeAll() worked for me, calling clearAll did not
removeAll() method of Table will remove all elements in the table. Here is the javadoc;
public void removeAll()
Removes all of the items from the receiver.
You can use table.clearAll();
Clears all the items in the receiver. The text, icon and other
attributes of the items are set to their default values. If the table
was created with the SWT.VIRTUAL style, these attributes
are requested again as needed.
Related
I am developing an application in Netbeans using Java and have been told to use the GUI creation features that Netbeans offer. Due to this I cannot edit the initComponents(); method to edit the creation of the JList and add a default list model to it.
I have tried creating a new JList but that resulted in an infinite loop. I haven't ever created controls through coding them myself, only by an IDE's GUI creation tools.
This is what I have currently:
private void formWindowActivated(java.awt.event.WindowEvent evt) {
//String to hold current patients data
String patientDetails;
//Take the arraylist from the model
ArrayList<IAccountStrategy> unapprovedPatients;
unapprovedPatients = model.getObservers();
//Create default list model to store the patients details
DefaultListModel<String> unapprovedPatientModel = new DefaultListModel<>();
IAccountStrategy xx;
//For loop to iterate through each element of unapprovedPatients
for(int i = 0; i < unapprovedPatients.size(); i++){
//get the current patients details and store them in a string variable
xx = unapprovedPatients.get(i);
patientDetails = xx.getAccountID() + xx.getUsername() + xx.getFirstname() + xx.getLastname();
//Add string variable to list model
unapprovedPatientModel.addElement(patientDetails);
}
//add list model to existing JList
listPatients.addElement(unapprovedPatientModel);
}
I would like to output all the elements from the list model into the actual JList and then let the user interact with the list itself.
Thanks in advance!
is it not possible to use the list as I want
You just wrote code to create the DefaultListModel.
So now all you need is to add:
list.setModel( unapprovedPatientModel );
so the JList can use the newly created model.
Although the problem with this code is that the code will be executed every time the window is activated.
But the point is that all you need to do is update the list using the setModel() method. How you do this in the IDE is up to you.
I have a GWT DataGrid with a multi-selection model and check-boxes to show selection/select/deselect rows. That's all well and good.
But, I also want to have a second, independent selection model. If a user double-clicks on a row, I want to handle that event, and for the event handler to know which row was double-clicked. The double-clicking should not affect the check-box selection.
I tried this:
final SelectionModel<MyRecord> selectionModel = new MultiSelectionModel...
//Yes I need a MultiSelectionModel
dataGrid.addDomHandler(new DoubleClickHandler() {
public void onDoubleClick(DoubleClickEvent event) {
selectionModel.get??? //no suitable getter for double-clicked
}
}, DoubleClickEvent.getType());
But ran into a dead-end when I found now way to get the double-clicked row in the event handler. One way would be to register both a Multi- and Single- selection model, but doubt DataGrid will support that.
Neither can I work out how to get the clicked row from the DoubleClickEvent object.
I have implemented a button cell with a FieldUpdater. This works, but it's not ideal.
Any suggestions?
If I understand correctly you want to get the index of the row.
You could do it like this: (this way you'll get the "Real" index)
AbstractSelectionModel<T> selectionModel = (AbstractSelectionModel<T>)dataGrid.getSelectionModel();
ArrayList<Integer> intList = new ArrayList<Integer>();
List<Row> list = (List<Row>)_dataProvider.getList();
int i = 0;
for(Row row : list)
{
if( selectionModel.isSelected(row) )
intList.add(i);
i++;
}
UPDATE:
To get only the current row you could do that:
datagrid.getKeyboardSelectedRow()
I'm 3 years too late to the party, but I think the more correct solution would be:
dataGrid.addCellPreviewHandler(new CellPreviewEvent.Handler<YOUR_MODEL_TYPE>() {
#Override
public void onCellPreview(final CellPreviewEvent<YOUR_MODEL_TYPE> event) {
if (BrowserEvents.DBLCLICK.equalsIgnoreCase(event.getNativeEvent().getType())) {
int row = event.getIndex();
doStuff(row); // do whatever you need the row index for
}
}
});
I need to remove all items from the combo box
int itemCount = combo.getItemCount();
for(int i = 0; i < itemCount; i++){
combo.removeItemAt(0);
}
This code will remove all items except the last one. It gives a NullPointerException.
How to fix that?
The code in the question would normally work. However, it looks like a threading issue. Another thread may be messing with the items.
However, I sugeest you should better use the removeAllItems(); method:
combo.removeAllItems();
How about JComboBox.removeAllItems()?
You can use
this.combo.removeAllItems();
to remove all the items in JComboBox.
In second line:
combo.removeItemAt(0);
I think instead of 0 it should be i.
do it in reverse order as:
for(int i=combo.getItemCount()-1;i>=0;i--){
combo.removeItemAt(i);
}
But in my case combo.removeAllItems() works fine
use .removeAllItems() methods to remove all items from combo box.
The assumption that it is related to another thread is not always true. It can be the thread itself causing the issue.
This exception may happen because an event is triggered when a combo item is removed and in this event handling method you still refer to combobox items.
For example when you delete somewhere (other than in actionPeformed()) in your code the last item from a combo box with combo.removeItemAt(0) or removeAllItems() then still the event actionPerformed will be fired/executed. But very often the actionPerformed() method contains code to react on user actions (user clicked somewhere on the combobox). So, when the last item has been deleted there is no more item in the combobox and any reference to an item or index in actionPerformed() will cause an exception.
The solution to this is to move the code from actionPerformed() to e.g. mouseClicked() or another event handler depending on what you want to do.
removeAllItems() it does remove all things but after the add data to the combo box it will not show there ,the nullPointException will shows
Use this to remove all the elements from the combo box :
DefaultComboBoxModel model = (DefaultComboBoxModel) ComboBox.getModel();
model.removeAllElements();
Usually it happens because you have an event associated JComboBox. It is solved if you have control item in the JComboBox to act, for example:
jComboBoxExample.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent e) {
do_run ();
}
});
public void do_run() {
int n=jComboBoxPerfilDocumentos.getItemCount(); <--THIS IS THE SOLUTION
if (n> 0) {
String x = jComboBoxPerfilDocumentos.getSelectedItem (). ToString ();
}
}
Ok I have this table added to a JPanel added to a JFrame.
In the mousePressed Event there it instates a new class tData and calls a method which returns data from the database as a string contained in a vector element.
What I want to happen, every time the btn is pressed it updates the table,
What does happen, every time the btn is pressed it updates the table but adds the same data to the end of the table?
What would cause this to happen?
Object columnHeaders[] = {"Fname", "Mname", "Lname", "Age"};
Object data[][] = {{}};
final DefaultTableModel dtm = new DefaultTableModel(data, columnHeaders);
JTable tb = new JTable(dtm);
JScrollPane scrollPane = new JScrollPane(tb);
tableWrap.add(scrollPane);
btn.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent arg0) {
tData h = new tData(10));
Vector<String> v = h.getInfo();
for(int i = 0; i < v.size() ; i++) {
dtm.insertRow(i, new Object[]{v.get(i).split(",")[0], v.get(i).split(",")[1], v.get(i).split(",")[2], v.get(i).split(",")[3]});
}
dtm.fireTableDataChanged();
frame.repaint();
}
});
Several issues here:
It looks like you need to update the entire table whenever that button is pressed. You have three options here: update the DefaultTableModel using setDataVector, recreate a new model from scratch and set it on the JTable with setModel, implement your own TableModel based on AbstractTableModel and firing appropriate TableModel events. You could also update the model using addRow and removeRow, but that would be very uneffective as it would trigger many events.
To listen for "button pressed" events, rather user an ActionListener (and add it using addActionListener) or an Action (and set it with setAction).
In DefaultTableModel, when you modify the structure, it is not needed to manuall call fireTableDataChanged();. It automatically fires appropriate TableModel events whenever you modify the underlying data. Don't call frame.repaint() either, it is useless and can be time consuming.
Unrelated: Consider following Java coding conventions (Class name starts with a capital letter) and meaningful variable names.
There are quite a few issues with the code you've written here. First, let's address the issue you are having with the code in your mousePressed method:
You are getting a Vector from your data providing code and then setting rows 0...n of the table with your new data. The way to do this is to write a custom extension of AbstractTableModel that returns the data you need when the button is pressed.
Another issue is the way you are handling the button press. Adding a mouse listener to the button is absolutely not the correct way of handling that. You want to add an ActionListener to the button. That listener will be fired whenever the button is pressed and released.
I have JList that grows in size along with the JFrame. I have the following code:
defaultListModel = new DefaultListModel<SData>();
for (String string: listOfStrings) {
defaultListModel.addElement(string);
}
jList = new JList<String>(defaultListModel);
jList.setCellRenderer(jListCellRenderer);
jList.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent arg0) {
//codes to go
}
});
jList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
jList.setLayoutOrientation(JList.VERTICAL_WRAP);
scrollPane = new JScrollPane(jList);
If I set some value for setVisibleRowCount method, the row number becomes fixed and If I don't set value, default value of 8 comes to play. I want to have this value dynamically changing.
I just found that jList.setVisibleRowCount(0) makes it self adjustable, when resizing JList.
Echoing #kleopatras's comment, it's not clear what controls setVisibleRowCount(). This example grows the enclosing Window as rows are added, up to a predefined limit, then the scrollbar takes over. It might give you some ideas, or you can use it as the basis of your sscce as #Andrew suggests.
Addendum: If the size of JList will control the count, I'd start with half of the model's size(). Then add one visible row for every n added to the model, in a fixed ratio that is pinned to a predefined limit. To maintain a reliable count, you'll have to implement your own ListModel or override the mutators in DefaultListModel.