Update ListView Selection on button click - java

https://gyazo.com/dbe9a706e2eaecc34028593636bcf2ea
Here is a picture of an assignment I am working on. I have the add and remove working with no problem. I even had the loan/return button working before I switched to a GUI. Problem is I can not figure out how to update the selected entry on button click. I also can't find any similar problems in google. I don't need the assignment done for me, just a point in the right direction. Thanks for any help.
public void handle(ActionEvent e) {
try {
String tempText = listView.getElementAt(listView.getSelectedIndex()).toString();
mediaCollection.set(listView.getSelectionModel().getSelectedIndex(), tempText + loanedToTextField.getText());
MediaItem.setLoanedTo(loanedToTextField.getText());
Should I not be using ListView?

Related

Vaadin unbuffered grids won't close

I am having a weird problem on my Vaadin app. I have a screen with two separate unbuffered grids.
The user is able to edit the data in those two grids and then click a "Save" button to save the changes made.
My problem is that I want to close the editors when the user clicks on "Save".
I tried the following code:
private void closeEditors() {
if (tab1.getEditor().isOpen()) {
tab1.getEditor().closeEditor();
}
if (tab2.getEditor().isOpen()) {
tab2.getEditor().closeEditor();
}
}
I don't understand why this code doesn't work, editors stay opened. I also tried calling the cancel method but in vain.
I am using Vaadin 14.
I am posting this here with not much hope of finding an answer, this problem seems really precise.
But with any luck, maybe someone has experienced a similar issue ?
Maybe there is another glitchier way of forcing my editors to close ?
Any suggestion would be of great help, thanks in advance for anything you could think of !
EDIT: a little more code
This is the grids:
private Grid<Map<String, String>> tab1;
private Grid<Map<String, List<String>>> tab2;
This is the save function
public void saveData() {
saveDataFromTab1();
saveDataFromTab2();
try {
ServicesProxyImpl.getInstance().updateInBD(someObject);
saveButton.setEnabled(false);
cancelButton.setEnabled(false);
closeEditors();
Dialog dialog = VaadinComponentUtils.generateDialog(Constantes.MSG_SAVE_OK);
dialog.open();
} catch (JAXBException e) {
e.printStackTrace();
Dialog dialog = VaadinComponentUtils.generateDialog(Constantes.MSG_SAVE_KO);
dialog.open();
}
}
And this is the save button:
public Button getSaveButton() {
Button saveButton= VaadinComponentUtils.generateButton("Save",
VaadinIcon.CHECK_CIRCLE_O, null, true);
saveButton.setEnabled(false);
saveButton.addClickListener(event -> saveData());
return saveButton;
}
EDIT 2:
I have noticed something, when I click on an element of one of my two grids, I want the editor to open for that specific element and I want to close the editor on the other grid (the one not concerned by the modification). This works ! My grids behave like I want. It seems I am only losing control over my editors after I have actually modified one of the cells and clicked on my save button.
The isOpen function returns false on both grids after I call my closeEditors function, so it seems the grid thinks its editor is closed but it is still opened on my UI.
EDIT 3: I have found a workaround
Well, I have solved my problem by adding a close event listener on both my grids and calling resetGrids when the close event is fired. This function simply removes the grids from the UI, fetches the data to be displayed and then adds the grid one again, both editors being closed. I guess it solves my problem but I would have wanted to understand what was going on...
private void closeEditors() {
tableauHoraires.getEditor().addCloseListener(e -> resetGrids());
tableauRamassagePorteAPorte.getEditor().addCloseListener(e -> resetGrids());
if (tableauRamassagePorteAPorte.getEditor().isOpen()) {
tableauRamassagePorteAPorte.getEditor().closeEditor();
}
if (tableauHoraires.getEditor().isOpen()) {
tableauHoraires.getEditor().closeEditor();
tableauHoraires.getEditor().refresh();
}
}
Make sure that the objects in your grid have proper equals and hashcode methods and that the field(s) being edited do not influence them.
I use the PK from the database.

Disabling a button in javaFX and enable it again

Hi guy I just started learn some JavaFX and I made a simple product TableView program which you can add and delete items. the items include name price and quantity.
I tried to prevent a bug so I did everytime the name\price\quantity fields are empty the add button will be disabled.
addButton = new Button("Add");
if(name.getText().isEmpty()&& price.getText().isEmpty() && quantity.getText().isEmpty()) {
addButton.setDisable(true);
} else {
addButton.setDisable(false);
addButton.setOnAction(e-> addButtonClicked());
}
The button is indeed disable but when I input some data to the field it remain disable.
I would like if someone can help me figure it out.
srry for the broken english ;)
Ok I fixed this, if someone got the same issue this is the solution :
addButton = new Button("Add");
addButton.disableProperty().bind(
name.textProperty().isEmpty()
.or(price.textProperty().isEmpty())
.or(quantity.textProperty().isEmpty())
);

How to hide and unhide box from checkbox

I'm a student of android developer.
I need to make a project and I've ran into a problem.
basically the problem is that i need to make the box of a checkBox disappear and only after pushing a specific button, the box will appear and be clickable.
from my searches i've found that when i write:
myCheckBox.setButtonDrawable(new ColorDrawable(Color.TRANSPARENT));
it will disappear and it's good but couldn't find the way to make it appear after that..
thanks a lot. :)
You can achieve that my making the view disappear either by using
myCheckBox.setVisibility(View.GONE);
(OR)
myCheckBox.setVisibility(View.INVISIBLE);
And again you can make it to appear by
myCheckBox.setVisibility(View.VISIBLE);
Hope this is helpful:)
You need to use
yourCheckBox.setVisibility(View.GONE);
to make that visible again,
yourCheckBox.setVisibility(View.VISIBLE);
You can get the currently assigned drawable with getButtonDrawable() and store it in a field, for example:
class Foo {
private Drawable oldDrawable;
private CheckBox myCheckBox;
public void hideCheckbox() {
oldDrawable = myCheckBox.getButtonDrawable();
myCheckBox.setButtonDrawable(new ColorDrawable(Color.TRANSPARENT));
}
public void showCheckbox() {
myCheckBox.setButtonDrawable(oldDrawable);
}
}

How to show names in Jtextfield click on JButton using Netbeans

I am fresher in Netbeans. I have a small assignment related Netbeans. I want to show some names in JTextfield(for example "abcd","ijkl","mnop" etc as next by next) and it must show one by one. I had create some code which not working properly. I am looking some helps related to it. Please mention as full code here that I can understand in future.I do not know how to create it. So i am done it some another ways. But showing error message. My code is as follows.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String s[]={"abcd","ijkl","mnop" } ;
for(int d=0; d<s.length;d++)
jTextField1.setText(""+s);
}
Both these variables should be inside of your class but outside of any method.
String s[]={"abcd","ijkl","mnop" } ;
int i=0;
In action performed method
if(i>=s.length)
i=0;
jTextField1.setText(s[i]);
i++;
For the first click, display abcd.
Next click, display ijkl.
Next click, display mnop.
But I didn't check it. Just try it. It will works.

CustomTabbedPane: stop opening of tabs that are already open

I am using CustomTabbedPane in my class to show different panels within one frame. It works fine as far as functionality is concerned. I want my program to be elegant so I don't want a tab to show up, when a user click the button, that is already open. I have searched it for like more than 3 hours but didn't find anything similar to my problem. Below is my code snippet of action-listener for adding product.
addProduct.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
Product obj=new Product();
//if(isTabPresent()){
obj.start();
mainPane.addTab("Add Product",obj);
mainPane.setSelectedComponent(obj);
//}
}
});
I have also made a method isTabPresent() to check whether the tab is available in the mainPane(CustomTabbedPane) or not. I have commented isTabPresent() because it is not working the way I want.
public boolean isTabPresent(Object tab){
for(int i=0;i<mainPane.getTabCount();i++)
if(mainPane.getComponentAt(i).equals(tab))
return true;
return false;
}
This is what I have tried to get my job done but its not working. Any help or any better suggestion would be appreciated. Thanks a lot!

Categories