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

Java string: classes or packages with advanced functions?
Java memory space overlap?
Angle between 3 points signed, bad results
how can I find ram size of some mobile phones?
.jar works in Windows but not Linux (stacktrace)
Running performance test on machine with varying loads
Asterisk DTMF sometimes gets ignored (but only for some people)
MultiUserChat invitations in Smack
Some very generic code with visitor-pattern
Using org.json.JSONObject in Eclipse
Password system for Beta of Android application
Linked list implementation in java?
Are Locks AutoCloseable?
Taking website screenshots with Flash (or Java), client-side
java ant build several editions of software
Help recognizing this logger

Categories

HOME
twitter
magnific-popup
oracle11g
push-notification
path-finding
stock
hashmap
raspberry-pi
lodash
q
spring-jdbc
jgroups
constraint-programming
esper
izpack
numeral.js
quicklook
collectd
apache-cayenne
orchardcms
carthage
lombok
windows-7-x64
code-review
swingx
facebook-instant-articles
excel-2007
captiveportal
smb
react-css-modules
info.plist
pingfederate
maquette
srcset
exuberant-ctags
titanium-mobile
google-sites-2016
webtest
windows-dev-center
qwerty
space-complexity
hot-module-replacement
hockeyapp
cookiecutter-django
gulp-sourcemaps
.net-4.6.2
apple-news
logfiles
fault
revapi
python-c-api
ionicons
datastax-startup
typescript1.8
mplayer
brightcove
spring-mongodb
chord-diagram
android-fonts
url-masking
intrusion-detection
django-debug-toolbar
flash-cs5
lua-5.1
moveit
separator
wso2cloud
reactive-banana
sdhc
wireshark-dissector
spidermonkey
drawbitmap
event-bubbling
nsight
angular-local-storage
unity5.2.3
apache-commons-net
modalpopup
tween
gwidgets
oxwall
google-style-guide
tld
jquery-layout
phpthumb
listings
plasma
spring-validator
clipper
stage
gcj
self-extracting
subviews
wise
webkit.net
scripting-languages

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