I have an ArrayList called conditionList which stores condition names.
Whenever one is added/edited or deleted, the Lists on my GUI update no problems.
You can see below that i use 2 models... a DefaultListModel called condListModel and a DefaultComboBoxModel called conditionModel.
The code i have below is for the method editCondition(), at this stage the text is already changed on the GUI and is being submitted here. On my GUI, after ive submitted the change, the ComboBox and JList change no problem so im sure that the model changes are correct.
HOWEVER MY PROBLEM IS: When i save the ArrayList conditionList through serialization, and then load it back up, the change is gone. SO I think there is problem in my code with changing the String Value in the ArrayList(named conditionList), can anyone have a look and see if u notice an issue
String conString = jListCondition.getSelectedValue().toString();
for(String c: conditionList)
{
if(conString.compareTo(c) == 0)
{
String temp = entConName.getText();
c = temp;
//edit the Condition jList model
int x = condListModel.indexOf(conString);
condListModel.setElementAt(temp, x);
jListCondition.setModel(condListModel);
//edit the Condition comboBox model
int i = conditionModel.getIndexOf(conString);
conditionModel.insertElementAt(temp, i);
conditionModel.removeElement(conString);
entCondition.setModel(conditionModel);
//reset buttons
editConConfirm.setEnabled(false);
editCon.setEnabled(false);
deleteCon.setEnabled(false);
entConName.setText("");
addCon.setEnabled(true);
}
}
Related
I have a problem since yesterday.
I'v created a jTable with the WindowBuilder in Eclipse and currently are trying to get the number of the selected row, by using the .getSelectedRow() function, but it is always returning -1 (no row selected), even when I have selected something.
This is my current code for testing the output:
public void checkActiveItem() {
System.out.println(tableBills.getSelectedRow());
}
I try to let it run this way trough a timer and at least that seems working:
Timer time = new Timer();
time.schedule(new TimerTask() {
public void run() {
Frontend f = new Frontend();
f.checkActiveBill();
f.checkActiveItem();
}
}, 250, 250 );
The table currently has just one entry, but even the first row dosn't get returned on selection.
I can create new rows by clicking add (and name them by entering a name in the textfield next to the add button before)
To create a new row I use this code, maybe there is the problem?
public void addBill() {
//maybe need this value somewhere else to, so let -1 and +1 as it is
int numbersOfBills = tableBills.getRowCount() - 1;
Bill newBill = new Bill(txtBillName.getText(), numbersOfBills + 1);
DefaultTableModel billModel2 = (DefaultTableModel) tableBills.getModel();
int billNb = numbersOfBills + 1;
billModel2.addRow(new Object[] {newBill.getBillNr(),newBill.getBillName(), newBill.getItemsInBill()});
}
The reason why you are getting -1 from getSelectedRow() even though you seem to have "added a row" is, because the Frontend object you are calling checkActiveItem() on is a completely different Frontend object than the one you are seeing.
The issue is here, inside of your Timers run():
Frontend f = new Frontend();
You create a new Frontend object for each timer iteration. And you call checkActiveItem() on exactly this object, not on the frontend you are seeing and pressing buttons on. Hence, the incorrect output.
As a solution, don't create new Frontends, instead, call checkActiveItem() on your original frontend object, which you made visible.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
Load/refresh jTable everytime I inserted new set of data to arrayList
Already tried those lines I've put on comments but still nothing changed. Consider that adding of those data to arrayList are performed in separate class.So here, Action performed in a jButton so everytime I add set of data from jTextfields and stores it as Object to arrayList, only the first set of columns and rows I inserted to the arrayList are the only displayed data in jTable. Although the second set of data I inserted after clicking the jButton twice was saved to arrayList, the jTable didn't even refresh or load the new data I inserted. Any help will be appreciated
//BUTTON ACTION PERFORMED
DefaultTableModel tableModel = (DefaultTableModel) MyJTable.getModel();
tableModel.fireTableDataChanged();
//tableModel.setRowCount(0);
//tableModel.repaint();
//int rowCount = tableModel.getRowCount();
//for(int i = rowCount -1; i >= 0; i--){
//tableModel.removeRow(i);
//}
Object[][] displayOnTable = new Object[Data.data.size()][4];
for(int x=0; x < Data.data.size(); x++){
displayOnTable[x] = Data.data.get(x);
}
MyJTable.setModel(new javax.swing.table.DefaultTableModel(
disp,
new String [] {
"ID", "Name", "Gender", "Age"
} ));
// --- Declared in Separated class----
//public class Data {
// public static List<Object[]> data = new ArrayList<>();
// }
// ------------------------
Already tried those lines I've put on comments but still nothing changed.
Well start with something really simple.
Add two lines to your ActionListener:
DefaultTableModel tableModel = (DefaultTableModel) MyJTable.getModel();
tableModel.setRowCount(0);
If nothing happens to the data in the table, then it means you don't have a reference to the table that is visible on the frame. So you need to solve this problem.
Did the data in the table clear? If so this is a good sign as it means your code is referencing the TableModel of the JTable that has been added to the frame.
So now you can add data from the ArrayList.
The basic logic would be:
System.out.println( data.size() ); // to verify you actually have data in the List
for (Object[] row: data)
model.addRow( row );
That's it. No need to create a new TableModel. No need to create a 2D array. You will simply iterate through the ArrayList adding one row of data at a time to the model. Then the table will automatically repaint() itself.
Of course this will only work if you have data in the ArrayList.
I have populated a combobox B1 from database. When itemStateChanged event raises it should populate another combobox B2, but its not working.
ArrayList1 = //call method in database connection class()
for (int j = 0; j < ArrayList1.size(); j++)
{
if (j == 0)
{
combobox1.addItem("Select Any");
}
combobox1.addItem(ArrayList1.get(j));
}
combobox1.addItemListener(new ItemListener()
{
#Override
public void itemStateChanged(ItemEvent ie)
{
String catName = (String)combobox1.getSelectedItem();
if (!catName.equalsIgnoreCase("Select Any"))
{
ArrayList2=//call method in DB class with cat_name as argument
for(int i=0;i < ArrayList2.size();i++)
{
if (i == 0)
{
combobox2.addItem("Select Any");
}
combobox2.addItem(ArrayList2.get(i));
}
}
}
});
first combobox gets populated from database, but after selecting any item from it second combobox keeps empty.
and why debugging this my computer hangs on?
you have to implements ComboBoxModel and add/remove/change Items in the Model, not in the JComboBox, nor somewhere in the Array, List or Vector, sure is possible but you have to execute your code on EDT and always replace Array, List or Vector for concreted JComboBox, don't do it that this way :-)
maybe you have problem with Concurency in the Swing, maybe changes are done, but outside EDT, more about your issues pass events wrapped into invokeLater() and multiple-jcombobox
DefaultComboBoxModel model = new DefaultComboBoxModel(yourstringarray);
item_combobox.setModel( model );
n ma problem get solved....
You must read:
http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html
It will help you to deal with java comboboxes.
Seems you should use an ActionListener as event to populate second combobox.
For your debug problems you should check bug 6714678 from java bugtracker
-Dsun.awt.disablegrab=true
should solve your debug problem (since 2008)
See could not work for old jdks as on 2007 related bug 6517045 says:
after discussion we came to conclusion that this (debug on combobox events) is just one more place when it is not
wise to stop in debugger (the same is true for DnD, fullscreen).
I have generated a GUI from netbeans in which I have placed a combobox too.
By default, the items in combobox are item1, item2, item3, item4.
But I want my own items. Netbeans doesn't allow editing generated code so how can i edit the comnbobox according to me.
Note: I know one method by editing the "model" property of that jComboBox but I don't want to do it like that because I want various items (which are in an array) in that jComboBox so I want to pass that array in that jComboBox like as follows:
jComboBox2 = new javax.swing.JComboBox();
String [] date = new String[31];
for(int i = 0; i < 31; i++) {
date[i] = i + 1;
}
jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(date));
There are 2 approaches I am aware of:
Simple approach - After the call to initComponents() in the constructor add you code to build your model and call jComboBox2.setModel(myModel) to set it. So the constructor would look something like:
public SomeClass() {
initComponents();
String [] date = new String[31];
for(int i = 0; i < 31; i++) {
date[i] = i + 1;
}
jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(date));
}
Complex approach - add a readable property that holds the desired model. For example:
private ComboBoxModel getComboBoxModel()
{
String[] items = {"Item A", "Item B", "Item C"};
return new DefaultComboBoxModel(items);
}
Then, in the jComboBox2 property sheet, click the button to edit the model.
In the editor panel change the dropdown from Combo Box Model Editor to Value from existing component.
Select Property. Choose the comboBoxModel property. Click OK
I tried the second way once. Never really used it again. Too much work, no real much gain. Plus it displays an empty combo box in the designer which just makes layout harder.
I use the first approach, plus use NetBean's model editor to supply some representative values for the model. That gives me the sensible size behavior in the designer at the cost of one unnecessary line in initComments().
Using Netbeans NEON and other netbeans version
1. Go to the properties of the combobox
2. Then go to model
you can inject your code by using "custom code" feature in the GUI editor for the "model" of combobox
public NewJFrame() {
initComponents();
reformatComboBox();
}
private void reformatComboBox() {
JComboBoxName.removeAllItems();
JComboBoxName.addItem("item1");
JComboBoxName.addItem("item2");
}
Completing blurec answer (I cannot comment yet), in the GUI editor select the comboxbox, go properties, then model, then hit the three dots. Then select Custome Code and add your code, for instance:
new DefaultComboBoxModel<>(functionThatReturnsAnStringArray())
For the posterity:
Right click the ComboBox and select Customize Code. Here at the comboBox.setModel, in the left select custom property.
After new String, add your values in the following form:
Value 1: Integer.toString(myInt1)
Value 2: Integer.toString(myInt2)
If your variables are int of course. If not just put the String variable and you are done.
Hope it helps.
I got a doubt regarding pre-selecting(setSelectedIndex(index)) an item in a ListBox, Im using Spring + GWT.
I got a dialog that contains a panel, this panel has a FlexPanel, in which I've put a couple ListBox, this are filled up with data from my database.
But this Panel is for updates of an entity in my database, thus I wanted it to pre-select the current properties for this items, allowing the user to change at will.
I do the filling up in the update method of the widget.
I tried setting the selectedItem in the update method, but it gives me an null error.
I've searched a few places and it seems that the ListBox are only filled at the exact moment of the display. Thus pre-selecting would be impossible.
I thought about some event, that is fired when the page is displayed.
onLoad() doesnt work..
Anyone have something to help me out in here?
I really think you can set the selection before it's attached and displayed, but you have to have added the data before you can select an index. If this is a single select box you could write something like this:
void updateListContent(MyDataObject selected, List<MyDataObject> list){
for (MyDataObject anObject : list) {
theListBox.addItem(anObject.getTextToDisplay(), anObject.getKeyValueForList());
}
theListBox.setSelectedIndex(list.indexOf(selected));
}
If this is a multiple select box something like this may work:
void updateListContent(List<MyDataObject> allSelected, List<MyDataObject> list){
for (MyDataObject anObject : list) {
theMultipleListBox.addItem(anObject.getTextToDisplay(), anObject.getKeyValueForList());
}
for (MyDataObject selected : allSelected) {
theMultipleListBox.setItemSelected(list.indexOf(selected), true);
}
}
(Note I haven't actually compiled this, so there might be typos. And this assumes that the selected element(s) is really present in the list of possible values, so if you cant be sure of this you'll need to add some bounds checking.)
I've been happily setting both the values and the selection index prior to attachment so as far as I'm aware it should work. There's a bug however when setting the selected index to -1 on IE, see http://code.google.com/p/google-web-toolkit/issues/detail?id=2689.
private void setSelectedValue(ListBox lBox, String str) {
String text = str;
int indexToFind = -1;
for (int i = 0; i < lBox.getItemCount(); i++) {
if (lBox.getValue(i).equals(text)) {
indexToFind = i;
break;
}
}
lBox.setSelectedIndex(indexToFind);
}
Pre-selection should work also with setValue()-function. Thus, no complicated code is needed.