java


Buttons created in a loop are all doing the same thing when clicked


I am currently trying to do some stuff in Java, using ButtonColumn from the internet, which allows me to create clickable button inside a JTable cell. But I am in need to make multiple buttons and for each button specified it´s own parameter for its action. So I have this code:
DefaultTableModel dtm = (DefaultTableModel) jTable1.getModel();
dtm.setNumRows(uziv.size());
int riadok = 0;
for (int i = 0; i < uziv.size(); i++) {
jTable1.setValueAt(uziv.get(i).getMeno(), riadok, 0);
jTable1.setValueAt(uziv.get(i).getPriezvisko(), riadok, 1);
jTable1.setValueAt(uziv.get(i).getEmail(), riadok, 2);
jTable1.setValueAt(uziv.get(i).getLogin(), riadok, 3);
int idUz = uziv.get(i).getId();
Action ban = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
DefaultTableModel dtm = (DefaultTableModel) jTable1.getModel();
int modelRow = Integer.valueOf(e.getActionCommand());
db.zabanujUzivatela(db.selectId(idUz));
}
};
ButtonColumn col = new ButtonColumn(jTable1, ban, 4);
riadok++;
}
But the problem is that my idUziv even that it´s changing during the for cyclus, at the end when I run my program and click on any button, all buttons are pointing on the same idUziv when their action is executed. And I am really out of ideas how else I can do this. I need to make it like the first button click would point to idUziv with value 1 for example, second with value 2 and so on... Do you have any idea how to id ? Or is it even possible ?
Thank you very much for every answer
ButtonColumn col = new ButtonColumn(jTable1, ban, 4);
You can't create a ButtonColumn for each row. The class name is "...Column". The editor is for the entire column. There is only one Action for all the rows. So the Action needs to be written generically so it can work for all rows.
But I am in need to make multiple buttons and for each button specified it´s own parameter for its action.
Store the ID as part of the TableModel. So you need to create a table with a column for the ID and then set the id when you load the data into the TableModel:
jTable1.setValueAt(uziv.get(i).getLogin(), riadok, 3);
//int idUz = uziv.get(i).getId();
jTable1.setValueAt(uziv.get(i).getId(), riadok, 4);
Then you can remove the TableColumn from the JTable so the column is not visible:
table.removeColumn( table.getColumn(...) );
Now in your Action you can access the ID from the TableModel for the selected row.
Or another option is to imbed the ID in the text for the button column as was demonstrated in the example from the TableButtonColumn so that you see "Login ID1". Then you just get the text from the model and parse out the last word.

Related Links

Is there a way to set a id of a view programatically with a string
Object serialization and random access in java
Plus operator for Fraction calculator
Unknown log trace in Hazelcast
Real name of “all frames in one image” images?
How to stop an activity from a non-activity-class?
How to obtain Felix Framework's properties in the config.properties file?
Detach Writer from System.out without closing
onActivityResult shows null for intent-data [duplicate]
Is multiplying by 0.0000001 the same as dividing by 10000000?
Bat File Cannot Find Symbol Warnings
SimpleDateFormat adding 7 hours?
Select for update not working
JAXB schema validation error with CXF - JAXBException occurred : cvc-elt.1: Cannot find the declaration of element
log4j not sending INFO mails
Memory leak in service android

Categories

HOME
java
client
listview
pypi
netbeans
gremlin
google-api-php-client
relative-path
sd-card
routes
bookshelf.js
google-oauth
react-redux
leon
twitter-bootstrap-4
cross-browser
mouse
gitpitch
midi
zend-framework3
flask-wtforms
lucene.net
autocad-plugin
cx-freeze
p-value
poltergeist
applozic
maquette
webkitspeechrecognition
objectanimator
nat
exuberant-ctags
titanium-mobile
typed.js
gammu
ghost4j
stacked
x11-forwarding
document.write
quadratic-programming
acoustics
taffy
revapi
promela
nstextview
knockout-components
pg-dump
google-perftools
akka-cluster
jspdf-autotable
medium.com
google-feed-api
measures
disque
react-native-listview
rvest
ipconfig
maven-tomcat-plugin
reactive-banana
ruby-2.2
wireshark-dissector
security-testing
varargs
0xdbe
ideamart
sailfish-os
generic-programming
sniffer
device-orientation
jscript.net
javafx-webengine
device-manager
ember-charts
meteor-velocity
mbr
stxxl
uv-mapping
c18
jsctypes
opcache
http-unit
sublist
dsn
errai
online-compilation
bubble-chart
gwt-ext
qt-jambi
xmlspy
ecl
fluent-interface
digest-authentication
web-application-design
icanhaz.js
perfect-hash
database-management
zend-decorators
django-notification

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App