I want to get the button click event in each row. How to get that ?. I tried this link and its working if there is only one button in each row. But in my case, there are more than one button in each row. 10,20 and 11,21 are my buttons.
in RowManager class from the above link, i added the following code -
button = new ButtonField("1" + index, ButtonField.CONSUME_CLICK);
button.setCookie(new Integer(index));
button.setFont(textFont);
add(button);
button1 = new ButtonField("2" + index, ButtonField.CONSUME_CLICK);
button1.setCookie(new Integer(index));
button1.setFont(textFont);
add(button1);
Now on StackScreen class, public void fieldChanged(Field field, int context), How i get the name of the clicked buttons ?
Solved By My self -
public static int v=0;
button = new ButtonField("1" + index, ButtonField.CONSUME_CLICK);
button.setCookie(new Integer(v+1)); //set cookie
button.setFont(textFont);
add(button);
v=v+1; //increment the value of v
button1 = new ButtonField("2" + index, ButtonField.CONSUME_CLICK);
button1.setCookie(new Integer(v+1));
button1.setFont(textFont);
add(button1);
v=v+1;
and -
public void setChangeListener(FieldChangeListener listener) {
// only the button field supports change listeners
button.setChangeListener(listener);
button1.setChangeListener(listener);
}
Then on StackScreen class -
public void fieldChanged(Field field, int context) {
Object f=field.getCookie();
Dialog.alert("Button " +f);
}
Related
Context: In a Vaadin 23.1.3 application there's a VirtualList with items with a ClickListener that refreshes the content of the item.
What works: When there are 3 items in the VirtualList, the ClickListener works fine and after calling callingRefreshItem the item in the VirtualList is re-rendered.
This is how the VirtualList looks like after I clicked the item with id="id2" 6 times (the counter counts up (equals 6) and LocalDateTime is updated, as expected):
What does not work: When there are 7000 items in the VirtualList, (a) the ClickListener stops working after calling refreshItem and (b) the item that should get refreshed (by refreshItem) is not refreshed.
This is how the list looks like with e.g. 7000 items after clicking some items (counter is not refreshed and LocalDateTime is not refreshed):
Code:
#Route("sandbox")
public class SandboxView extends VerticalLayout {
private static class Item {
String id;
int clicked=0;
public Item(String id) {
super();
this.id = id;
}
#Override
public boolean equals(Object obj) {
return this.id.equals(((Item)obj).id);
}
}
public SandboxView() {
int numberOfItems = 7000;
VirtualList<Item> vlist = new VirtualList<>();
List<Item> items = new ArrayList<>();
for (int i=0;i<numberOfItems;i++) {
items.add(new Item("id"+i));
}
ListDataProvider<Item> dataProvider = new ListDataProvider<Item>(items);
vlist.setDataProvider(dataProvider);
vlist.setRenderer(new ComponentRenderer<Div, Item>(item -> {
Div div = new Div();
div.addClickListener(e -> {item.clicked++;System.out.println(item.id + " clicked "+item.clicked+"x");dataProvider.refreshItem(item, true);});
div.add(item.id+" "+item.clicked+" " +LocalDateTime.now());
return div;
}
));
this.add(vlist);
}
}
Further observations:
It seems that this behavior starts with 50 items in the VirtualList.
When I scroll away from a broken item and then scroll to the broken item back, the values at the item are refreshed, the ClickListener is present and I can use it a single time. Then I have to scroll away and back again.
(Update) There is a JavaScript error in the browser: The error has occurred in the JS code: '$0, $1, return $0.$connector.updateData($1)' and this message:
Question: How can I refresh an item in a large VirtualList without losing ClickListeners?
In Vaadin 23.1.6 this is fixed.
https://github.com/vaadin/flow-components/issues/3487#issuecomment-1191292376
I have the following problem:
My method opens a JDialog with a bunch of buttons (only one in example code). I want to click a button and thereby choose an ImageIcon for my method to return. But the Method does not wait for me to click a button. It opens the window and then returns an empty ImageIcon.
public class Kartenauswahl {
ImageIcon bandit;
public ImageIcon auswahlfenster() {
int bwidth = new Integer(150);
int bheight = new Integer(225);
bandit = new ImageIcon("cover/Bandit.jpe");
bandit.setImage(bandit.getImage().getScaledInstance(bwidth,bheight,Image.SCALE_DEFAULT));
final JDialog kartenwahl = new JDialog();
kartenwahl.setTitle("Kartenwahl");
kartenwahl.setSize(1500,1000);
kartenwahl.setVisible(true);
kartenwahl.setLayout(new FlowLayout());
ImageIcon returnicon= new ImageIcon();
final JButton b1 = new JButton(); //just to get the Icon out of the void loop
JButton B1 = new JButton(bandit); //this is going to be the button I want to click to choose the ImageIcon which is returned
B1.setContentAreaFilled(false);
B1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
b1.setIcon(bandit);
kartenwahl.dispose();
}
});
kartenwahl.add(B1);
returnicon = (ImageIcon) b1.getIcon();
return returnicon;
}
}
Question: can I bind the return statement to a condition? Like "only return after I clicked that Button B1"?
Hi sorry for the long wait. I have written an custom JDialog that should work for you.
public class CustomDialog extends JDialog {
JButton[] buttons;
ImageIcon selectedImageIcon;
public CustomDialog() {
setSize(500, 500);
setLayout(new GridLayout(4, 6));
ActionListener actionListener = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
selectedImageIcon = ((ImageIcon) ((JButton) e.getSource()).getIcon());
dispose();
}
};
buttons = new JButton[24];
for(int i = 0; i < 24; i++) {
buttons[i] = new JButton(new ImageIcon("path_to_your_image_file"));
buttons[i].addActionListener(actionListener);
add(buttons[i]);
}
setVisible(true);
}
public ImageIcon getSelectedImageIcon() {
return selectedImageIcon;
}
}
The initial size is not that important the GridLayout is. you mentioned that you would need 24 buttons so I created an grid with 4 rows and 6 columns.
Then I create the buttons in a loop and adding the same Listener to set the selection icon with the icon of the pressed button. Afterwards I dispose the screen triggering an windowClosed event.
You could simply create this Dialog from your main class and wait for the response like so:
public class main {
public static void main(String[] args) {
CustomDialog customDialog = new CustomDialog();
customDialog.addWindowListener(new WindowAdapter() {
#Override
public void windowClosed(WindowEvent e) {
ImageIcon icon = customDialog.getSelectedImageIcon();
//do something with your icon
}
});
}
}
Don't forget to mark this answer as correct if it fixes your problem.
Have a good one!
i am trying to add a custom window in blackberry but before this i am trying to add a custom label in that popup screen for my satisfaction that i can add or cant . so at the time when i am adding that i am facing the problem of IllegalArguementException error , so can you please tell me how can i solve that problem . i am doing like this .
see this is my MYScreen class which i am using to add pop-up . so pop-up is added when ever i clicked that press button , which is added in the Screen .
public final class MyScreen extends MainScreen implements FieldChangeListener
{
private ButtonField btn;
public MyScreen()
{
setTitle("MyTitle");
btn = new ButtonField ("press");
btn.setChangeListener(this);
add(btn) ;
}
public void fieldChanged(Field field, int context)
{
if ( field == btn )
{
Dialog.inform("hello");
pop_manager manager_object = new pop_manager(0);
UiApplication.getUiApplication().pushScreen( new up_pop_test( manager_object ) );
}
}
}
so in this i have added , new_up_pop_test class which is :
public class up_pop_test extends PopupScreen
{
public up_pop_test( pop_manager delegate)
{
super(delegate);
add(delegate);
}
}
and pop_manager is :
public class pop_manager extends Manager
{
protected pop_manager(long style)
{
super(style);
}
protected void sublayout(int w, int h)
{
Field f = getField(0);
layoutChild( f , w/3+w/3 , 50 ) ;
setPositionChild ( f , w/33 + w/33 , w/67+w/104 );
setExtent(w,h);
}
}
You don't need to do as much work as you were trying to...
All you need is a PopupScreen, which you can customise at will, just like a normal screen.
public class MyPopup extends PopupScreen {
public MyPopup() {
super(new VerticalFieldManager());
LabelField infoLabel = new LabelField("Here is a label in a popup");
add(infoLabel);
}
}
In order to call it, all you need to do is push like a normal screen.
UiApplication.getUiApplication().pushScreen(new MyPopup());
You cannot gain control over the Dialog class, these are for simple operations like informing the user or asking a question, and are standardised
In my application there is one ObjectChoiceField in MainScreen
i want to get change Listner of ObjectChoiceField after click on index 0.
i had already get click event of ObjectChoiceField after click on index grater than 1 and so on ..
so how can i get instance click event after click on ObjectChoiceField ?
I am not really sure what you are asking for.
In your FieldChangeListener.fieldChanged() method, calling ObjectChoiceField.getSelectedIndex() tells you which index is currently selected. You can look for index 0 from that.
If that is not what you need, then you need to clarify your question better.
ObjectChoiceField choiceField = new ObjectChoiceField();
public void fieldChanged(Field field, int context) {
if(field.equals(choiceField))
{
if(choiceField.getSelectedIndex==0)
{your Code}
}
If you want the value which the user have selected you can do by this way too
ObjectChoiceField choiceField = new ObjectChoiceField();
public void fieldChanged(Field field, int context) {
if(field.equals(choiceField))
{
if(choiceField.getSelectedIndex==0)
{
int index = choiceField.getSelectedIndex();
String s = (String) objectWeather.getChoice(index);
Dialog.inform("selected value is"+s);
}
}
}
I have a table with 2 columns: a checkbox and a textfield. I want to disable the textfield depending of the respective (same row) checkbox status. If the checkbox is checked then the textfield will be cleared and be read only. Is this possible ? Here is my code:
#SuppressWarnings("serial")
private Table filtersTable() {
final Table table = new Table();
table.setPageLength(10);
table.setSelectable(false);
table.setImmediate(true);
table.setSizeFull();
// table.setMultiSelectMode(MultiSelectMode.SIMPLE) ;
table.addContainerProperty("Tipo filtro", CheckBox.class, null);
table.addContainerProperty("Valor", String.class, null);
table.setEditable(true);
for (int i = 0; i < 15; i++) {
TextField t = new TextField();
t.setData(i);
t.setMaxLength(50);
t.setValue("valor " + i);
t.setImmediate(true);
t.setWidth(30, UNITS_PERCENTAGE);
CheckBox c = new CheckBox(" filtro " + i);
c.setWidth(30, UNITS_PERCENTAGE);
c.setData(i);
c.setImmediate(true);
c.addListener(new ValueChangeListener() {
#Override
public void valueChange(ValueChangeEvent event) {
// within this, could I access the respective row ID
// (i) then enable/disable TextField t on second column ?
System.out.println("event.getProperty().getValue()="
+ event.getProperty().getValue());
}
});
table.addItem(new Object[] { c, t }, i);
}
return table;
}
Thanks
Few changes to your code made it possible.
Not the finiest way, but te simpliest.
First,you have to set your second column (Valor) to TextField.class not String.class.
Here the change :
table.addContainerProperty("Valor", TextField.class, null);
Instead of keepin the variable i in the CheckBox.setData(), I suggest you to link your checkBox to the TextField of the same row, like this :
c.setData(t);
Finally I made little change to your listener :
c.addListener(new Property.ValueChangeListener() {
public void valueChange(ValueChangeEvent event) {
CheckBox checkBox = (CheckBox)event.getProperty();
if((Boolean) checkBox.getValue())
{
TextField associatedTextField = (TextField)checkBox.getData();
//Do all your stuff with the TextField
associatedTextField.setReadOnly(true);
}
}
});
Hope it's work for you!
Regards, Éric
public class MyCheckBox extends CheckBox {
private TextBox t;
public MyCheckBox(TextBox t) {
this.t = t;
attachLsnr();
}
private void attachLsnr()
{
addListener(new Property.ValueChangeListener() {
public void valueChange(ValueChangeEvent event) {
CheckBox checkBox = (CheckBox)event.getProperty();
if((Boolean) checkBox.getValue())
{
t.setReadOnly(true);
}
}
});
}
}