List of List of Buttons is empty - java

I'm working around with GWT.
Since I have a SQL db and my queries result set size is unknown I thought it would make sense to use Lists.
Actually I have one list of buttons and a second list of list of buttons.
The reason is because I have one table which stores groups and one which stores the actual data. Both of them in result should be buttons.
By clicking on a group button my layout is filled with the data buttons between the group buttons.
Now my db connection isn't ready to use so I wrote a function that fills my lists with fake data. Same for the groups.
public void fakeGroupData () {
// Group index 0
btnGroupList.add(new Button("Group a"));
// Group index 1
btnGroupList.add(new Button("Group b"));
...
}
public void fakeData () {
// Group index 0
btnDataList.add(new Button("Data 1.1"));
btnDataList.add(new Button("Data 1.2"));
btnDataList.add(new Button("Data 1.3"));
btnDataListList.add(btnDataList);
btnDataList.clear();
// Group index 1
btnDataList.add(...
}
Declaration looks like this
List<Button> btnGroupList = new ArrayList<Button>();
List<List<Button>> btnDataListList = new ArrayList<List<Button>>();
List<Button> btnDataList = new ArrayList<Button>();
When trying to get the ButtonList of the ListList the error appears.
int grpIndex = Panel.getWidgetIndex(grpBtn);
// grpBtn is equal to (Button)event.getSource() called by btnGroup ClickHandler
btnDataList.clear()
btnDataList = btnDataListList.get(grpIndex);
int loopEnd = btnDataList.size() - 1;
for (int i = 0; i<=loopEnd; i++) {...
"loopEnd" contains "-1" and nothing happens :(. I tried to debug here, everything seems ok.
"grpIndex" has the right index so the the right List is loaded.
But why is it empty? When debuggin the fakeData function eclipse shows the right size in the ButtonList.
Hope you can help me :)

btnDataList.clear(); will empty your referenced list objects.
for each Group index you need a new list. do like this
btnDataList = new ArrayList<Button>(); instead of btnDataList.clear();
// Group index 0
btnDataList.add(new Button("Data 1.1"));
btnDataList.add(new Button("Data 1.2"));
btnDataListList.add(btnDataList);
// Group index 1
btnDataList = new ArrayList<Button>();
btnDataList.add(new Button("Data 2.1"));
btnDataList.add(new Button("Data 2.2"));
btnDataListList.add(btnDataList);

Related

Is there any other way to implement setOnMouseClicked on JavaFX

I had to create a matrix in javaFX. I created it without any problem with GridPane. The next thing is to create like "buttons" on the right side of the matrix, these buttons will move +1 element of the matrix to the right. Like this:
110 <-
101 <- //ie: I clicked this button
100 <-
The result:
110 <-
110 <-
100 <-
The way I handle this bit-shifting-moving was with circular linked list. I don't have any problem with that I think you can ommit that part. I use this method:
private void moveRowRight(int index){
//recives a index row and moves +1 the elements of that row in the matrix.
}
cells is the matrix
The problem is that, first the matrix can be modified by the user input i.e. 5x5 6x6 7x7, so the number of buttons will also change. I tried using a BorderPane(center: gridpane(matrix), right: VBox()) and this is the part of the code how I added some HBox inside the Vbox (right part of the border pane) and using the setOnMouseClicked.
private void loadButtonsRight(){
for(int i = 0; i < cells[0].length ; i++){
HBox newBox = new HBox();
newBox.getChildren().add(new Text("MOVE"));
newBox.prefHeight(50);
newBox.prefWidth(50);
newBox.setOnMouseClicked(e -> {
moveRowRight(i);
});
VBRightButtons.getChildren().add(newBox); //where I add the HBox to the VBox (right part of the Border Pane)
}
}
}
But then there's this problem.
Local variables referenced from lambda expression must be final or effectively final
It seems that I cannot implement lambda with a value that will change. Is there any way to help me to put "buttons" that depends of the matrix size and that uses the method I've created?
The message tells you all you need to know to fix the problem:
Local variables referenced from lambda expression must be final or effectively final
Assign your changing variable to a final constant and use the constant value in the lambda instead of the variable:
final int idx = i;
newBox.setOnMouseClicked(e ->
moveRowRight(idx);
);
If you wish to understand this more, see the baeldung tutorial
https://www.baeldung.com/java-lambda-effectively-final-local-variables

Randomly place a text on some buttons

I am trying to move, say a text or number in setText(Integer.toString()) format randomly on 4 different buttons at each click. I am able to obtain the object form of it, but it is worthless since I can't get that information out of it.
This is my code:
public void clickButton(View view){
List mo = new ArrayList();
mo.add(Button);
mo.add(Button1);
mo.add(Button2);
mo.add(Button3); // these are defined in OnCreate method
Random rko = new Random();
int koo = rko.nextInt(mo.size());
Object a = mo.get(koo);
Log.i("here", String.valueOf(mo.get(koo)));
I get this output in the logs up on each click
android.support.v7.widget.AppCompatButton{71c6fe0 VFED..C.. ...PH... 697,708-928,933 #7f0c0052 app:id/Button2}
android.support.v7.widget.AppCompatButton{71c6fe0 VFED..C.. ...PH... 697,708-928,933 #7f0c0052 app:id/Button3}
android.support.v7.widget.AppCompatButton{71c6fe0 VFED..C.. ...PH... 697,708-928,933 #7f0c0052 app:id/Button}
I am new to this. Please help
Thanks.
hi i believe that u should use tags on your buttons p = new Random().nextInt(21);
button.setTag(Integer.toString(alpha));
//here alpha is the text that you wanted to put on the button,get the text from button by using appropriate code
cbutton.setTag(Integer.toString(" "));
You could try writing AppCompatButton instead of Object and then you should be able to get the text of a button with a.getText(); and change the text of a button with a.setText();.
#Rushikesh You did not restrict the random generated number within the range of 0-4.
The size of array list of button is 4 so the the index must be 0-4 to avoid the IndexOutOfBoundsException
List mo = new ArrayList();
mo.add(Button);
mo.add(Button1);
mo.add(Button2);
mo.add(Button3);
int koo = (int)(Math.random() * (10-(mo.size()+1))); // generated number range will be 0-4
Object a = mo.get(koo);

Android ListAdapter start # top

Currently the list when populated is starting with the view # the bottom of the list. Is there a way using listAdapters to force it to the top of the list?
Currently the orientation scrolls to the bottom on create. Is there a way to pin the screen to the top when it creates? http://imgur.com/wGTEy in this example you see that entry 1 on create is shoved upwards to make room for six... Instead I want it to populate like this. http://imgur.com/6Lg6e... entry 1 is the top of the list and 6 is pushed off to the bottom for the scroll.
If you look at the picture above you will notice it starts at the bottom of the list instead of at the top. Any Ideas?
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mStrings);
setListAdapter(mAdapter);
registerForContextMenu(getListView());
populateFields();
private void populateFields() {
if (mRowId != null) {
Cursor note = mDbHelper.fetchDaily(mRowId);
startManagingCursor(note);
String body = note.getString(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_DBODY));
mAdapter.clear();
if (!(body.trim().equals(""))){
String bodysplit[] = body.split(",");
for (int i = 0; i < bodysplit.length; i++) {
mAdapter.add(bodysplit[i].trim());
}
}
}
}
**edited to fix != string error.
You want the items later in the list to be at the top of the ListView? If so, check out this questions: Is it possible to make a ListView populate from the bottom?
You are completely changing the adapter, so the scroll position is lost in the process... You can use:
ListView listView = getListView();
int position = listView.getFirstVisiblePosition();
if (!(body.trim().equals(""))){
String bodysplit[] = body.split(",");
for (int i = 0; i < bodysplit.length; i++) {
mAdapter.add(bodysplit[i].trim());
}
}
listView.setSelection(position);
But this is not perfect as it is, if a row is added before position the index will be off. If your list contains unique values you can use ArrayAdapter#getPosition(), to find the new index.
While I still recommend using a CursorAdapter, because it handles large table data better, I want to address a point on efficiency with your ArrayAdapter code.
By using adapter.clear() and adapter.add() you are asking the ListView to redraw itself on every step... potentially dozens or hundreds of times. Instead you should work with the ArrayList directly and then ask the ListView to redraw once itself with ArrayAdapter#notifyDataSetChanged() after the loop completes.

How to order Vaadin Table Rows?

I have a table phases that has values that reference to a repository table. In the repository each value has an order number.
Normally in SQL, I would join them and then order by the order column. But if I want to do that in Vaadin I have to use the FreeFormQuery to make this join.
The Problem is that I want to allow users to change values in the table phases. So I would need to implement the FreeFormQueryDelegate. Is there a way to make this work by using a standard TableQuery instead of a FreeFormQuery ?
Maybe manually move the rows in a table ?
Vaadin offers some sort options for tables.
You can sort one specific propertyId or you can sort a set of properties.
// sort one container property Id
table.setSortContainerPropertyId(propertyId);
table.setSortAscending(ascending);
table.sort();
// sort several property Ids
table.sort(propertyId[], ascending[]);
NOTE: the code above is no proper code, it rather shows the method bodies.
I used follow code to rearrange the vaadin table rows, when "Move Up" button click, while selecting a table row.
Object selectedItemId = fieldTable.getValue();
if(selectedItemId != null){
List<Object> items = new ArrayList<>(fieldTable.getItemIds());
int index = items.indexOf(selectedItemId);
if(index < 1){
LOGGER.info("Selected Filed is at the top, cannot move up further");
return;
}
//Rearrange itemids for new order
Object upperRow= items.set(index - 1, selectedItemId);
items.set(index, upperRow);
items.forEach(itemId -> {
//Gets the properties of old table item
Item item = fieldTable.getItem(itemId);
String name = (String) item.getItemProperty(Constants.PROPERTYID_FIELD_NAME).getValue();
Button toggleBtn = (Button) item.getItemProperty(Constants.PROPERTYID_SORT_BUTTON).getValue();
//Remove the old table item
fieldTable.removeItem(itemId);
//Insert old table item into the new position index
Item newItem = fieldTable.addItem(itemId);
newItem.getItemProperty(Constants.PROPERTYID_FIELD_NAME).setValue(name);
newItem.getItemProperty(Constants.PROPERTYID_SORT_BUTTON).setValue(toggleBtn);
int position = items.indexOf(itemId);
if(position == index - 1) {
fieldTable.select(itemId);
}
});
}

java array of arraylists

I have four columns of buttons in my program. Buttons move between columns when assigned new ones. Instead of declaring 4 separate arraylists to store the buttons, is there a way to create 1 array of arraylists so I can simply itterate through the array?
I've tried List<JButton>[] lists = new ArrayList<JButton>[5];
But that won't work. What am I missing?
EDIT:
for(int i = 0; i < 5; i++){
if(choiceList.getSelectedIndex() == i){
if(btnPersons[nameList.getSelectedIndex()].getX() == column[i]){
JOptionPane.showMessageDialog(null, "Error - Name already present in the column.","", 1);
}else{
for(int j = 0; j < 5; j++){
if(lists[j].get(i) != null){
lists[j].remove(btnPersons[nameList.getSelectedIndex()]);
}
}
lists[i].add(btnPersons[nameList.getSelectedIndex()]);
lists[i].get(i).setBounds(column[i], ROWS[i], 125, 20);
//reloadLocations();
}
}
}
This is my code currently.Once a new column is selected it checks to see which listthe button was in and removes it, then adds it to the new list. But my new problem is that using lists[i] will no longer work. Idk how to properly loop through my list of arraylists using this declaration:
List<ArrayList<JButton>> lists = new ArrayList<ArrayList<JButton>>();
You have to keep a list of lists of JButton objects:
List<List<JButton>> lists = new ArrayList<List<JButton>>();
// populate (replace with your code)
lists.add(Arrays.asList(new JButton("list 1, button 1"), new JButton("list 1, button 2")));
lists.add(Arrays.asList(new JButton("list 2, button 3"), new JButton("list 2, button 4")));
lists.add(Arrays.asList(new JButton("list 3, button 5"), new JButton("list 3, button 6")));
lists.add(Arrays.asList(new JButton("list 4, button 7"), new JButton("list 4, button 8")));
// iterate
for(List<JButton> subList : lists) {
for(JButton button : subList) {
System.out.println(button.getText());
}
}
Giving an example of what worked for me / talked above.
List []oArrayOfArrayList = new ArrayList[2];
List<String> oStringList = new ArrayList<String>();
List<Integer> oIntegerList = new ArrayList<Integer>();
oArrayOfArrayList[0] = oStringList ;
oArrayOfArrayList[1] = oIntegerList ;
You cannot create arrays from classes with generic parameters.
You'll either want to make a list of a list, or forgo generic parameters.
You are not able to call the new operator for a generic object because at run time the type has been erased.
create a class for your ArrayList:
private class xxx extends ArrayList<JButton> {
// need serial to keep IDE happy
private static final long serialVersionUID = -2392107594926751233L;
};
private xxx[] lists = new xxx[5];

Categories

Resources