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

MouseListener is “halfway working” in my Applet
CodeIgniter Encrypt class to java
how to hide\close a webView programatically
Parsing byte array that contains different data types and able to get each value correctly using java
Java - Building calculator app and getting error when I press one operation then √
update loggedIn status on session timed out
React to NoSuchBeanDefinitionException inside Spring Application Context
download many images from server into android
Java print certain characters from an array
Report all long common subseqence(LCS) and its responsive location in the two strings
in eclipse plug-in, programmatically save new .java files to an existing jar file in the workspace
What is a programmers reference book? [closed]
How to dynamically generate Labels in jsf2.0 based on the data in the backing bean?
The difference between && and || [duplicate]
Java Textbook: “the size of an array must be known at compile time”
Why only String[] args in java instead of Object[] args?

Categories

HOME
lodash
fme
datastax-java-driver
convolution
android-youtube-api
pheatmap
iggrid
spring-xd
u-sql
qt-creator
postgres-xl
windows-server-2012
transformation
serilog
jasonette
jquery-ajaxq
orleans
functional-testing
java-7
elasticsearch-ruby
opentype
msys2
libuv
preg-match
phonegap
google-sites-2016
jspresso
copying
catch-all
vsts-build-task
wallpaper
atl
dartium
space-complexity
espeak
temporary-files
http-live-streaming
node-gyp
segment
jedis
git-diff
forever
diagnostics
sybase-asa
bind9
setuptools
impresspages
react-native-listview
bluegiga
ipconfig
uid
unity-networking
r-tree
google-places
t4mvc
pundit
emailrelay
splash
method-parameters
streambase
jsapi
sorl-thumbnail
clicktag
typekit
jscript.net
asp.net-dynamic-data
file-copying
braille
java.nio.file
coverflow
quartz-graphics
mqx
sharp-repository
flashvars
sublist
ticoredatasync
online-compilation
pendrive
gnustep
django-tagging
appender
android-sdk-2.1
xfbml
datareader
yslow
putchar
spec#
private-members
geneva-server
commodore
ajax-forms

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