java - How to distinguish between two JButtons with same name -
i developing swing application need have delete buttons. each delete button clicked perform deletion of row different file depending on button clicked. problem cant not distinguish 2 buttons names both have same name "delete". don't want name them differently may have lot of such delete buttons. there property id or can assign them?
avoid use of "switchboard" listeners , instead give each jbutton own action object, 1 created overriding abstractaction. abstractaction objects of same class, has reference file or string collection of interest, delete action performed on correct item.
another option associate button object requires action on via hashmap<jbutton, actionabletype>
.
here 2 examples combined 1 program. jpanel on right contains rows of word pairs delete jbutton, jbutton intimately tied jlabel containing word pairs, since held in common object, wordpairrow. there single actionlistener class added every delete jbutton, passed unique wordpairrow , container jpanel holds components:
wprow.adddeletelistener(new deleteaction(container, wprow));
if delete button pressed, row of words , button removed container.
perhaps better solution display word pairs within jtable using custom table model. in model there's 3rd column of booleans displayed check box. when "delete checked rows" jbutton below jtable checked, checked rows deleted jtable. later when want extract changed list, iterate through table model extracting words remaining.
import java.awt.borderlayout; import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import java.awt.insets; import java.awt.event.*; import java.io.inputstream; import java.util.scanner; import java.util.vector; import javax.swing.*; import javax.swing.table.defaulttablemodel; @suppresswarnings("serial") public class wordpairtest extends jpanel { private static final string data_file_name = "wordpair.txt"; private jpanel wordlistpanel = new jpanel(new gridbaglayout()); private wordpairtablemodel tablemodel = new wordpairtablemodel(); private jtable table = new jtable(tablemodel); public wordpairtest(scanner scanner) { int rownumber = 0; while (scanner.hasnextline()) { string line = scanner.nextline(); string[] tokens = line.split("\\s*,\\s*"); if (tokens.length == 2) { wordpairrow wprow = new wordpairrow(tokens[0], tokens[1]); addrowtolist(rownumber, wordlistpanel, wprow); rownumber++; tablemodel.addrow(tokens[0], tokens[1]); } } jpanel btnpanel = new jpanel(); btnpanel.add(new jbutton(new deletecheckedrowsaction(tablemodel))); jpanel tablepanel = new jpanel(new borderlayout()); tablepanel.add(new jscrollpane(table)); tablepanel.add(btnpanel, borderlayout.page_end); setlayout(new boxlayout(this, boxlayout.line_axis)); add(wordlistpanel); add(tablepanel); } private void addrowtolist(int row, jpanel container, wordpairrow wprow) { container.add(wprow.getfirstword(), creategbc(0, row)); container.add(wprow.getsecondword(), creategbc(1, row)); container.add(wprow.getdeletebtn(), creategbc(2, row)); wprow.adddeletelistener(new deleteaction(container, wprow)); } private gridbagconstraints creategbc(int x, int y) { gridbagconstraints gbc = new gridbagconstraints(); gbc.insets = new insets(5, 5, 5, 5); gbc.gridx = x; gbc.gridy = y; gbc.weightx = 1.0; gbc.weighty = 1.0; gbc.fill = gridbagconstraints.horizontal; return gbc; } private static void createandshowgui() { inputstream resource = wordpairtest.class .getresourceasstream(data_file_name); scanner scanner = new scanner(resource); jframe frame = new jframe("wordpairtest"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.getcontentpane().add(new wordpairtest(scanner)); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); scanner.close(); } public static void main(string[] args) { swingutilities.invokelater(new runnable() { public void run() { createandshowgui(); } }); } } class wordpairrow { private jlabel firstword = new jlabel(""); private jlabel secondword = new jlabel(""); private jbutton deletebtn = new jbutton("delete"); public wordpairrow(string word1, string word2) { firstword.settext(word1); secondword.settext(word2); } public jlabel getfirstword() { return firstword; } public jlabel getsecondword() { return secondword; } public jbutton getdeletebtn() { return deletebtn; } public void adddeletelistener(actionlistener listener) { deletebtn.addactionlistener(listener); } } class deleteaction implements actionlistener { private jpanel container; private wordpairrow wprow; public deleteaction(jpanel container, wordpairrow wprow) { this.container = container; this.wprow = wprow; } @override public void actionperformed(actionevent e) { container.remove(wprow.getfirstword()); container.remove(wprow.getsecondword()); container.remove(wprow.getdeletebtn()); container.revalidate(); container.repaint(); } } @suppresswarnings("serial") class wordpairtablemodel extends defaulttablemodel { private static final string[] columns = { "first word", "second word", "" }; public wordpairtablemodel() { super(columns, 0); } public void addrow(string word1, string word2) { vector<object> rowdata = new vector<>(); rowdata.add(word1); rowdata.add(word2); rowdata.add(boolean.false); super.addrow(rowdata); } @override public class<?> getcolumnclass(int columnindex) { if (getrowcount() > 0) { object item = getvalueat(0, columnindex); if (item != null) { return item.getclass(); } } return super.getcolumnclass(columnindex); } @override public boolean iscelleditable(int row, int column) { return column == 2; } } @suppresswarnings("serial") class deletecheckedrowsaction extends abstractaction { private wordpairtablemodel tablemodel; public deletecheckedrowsaction(wordpairtablemodel tablemodel) { super("delete checked rows"); this.tablemodel = tablemodel; } @override public void actionperformed(actionevent e) { (int row = tablemodel.getrowcount() - 1; row >= 0; row--) { boolean check = (boolean) tablemodel.getvalueat(row, 2); if (check) { tablemodel.removerow(row); } } } }
data file:
hello, goodbye hola, adiós shalom, shalom bonjour, au revoir buongiorno, arrivederci hyvaa paivaa, nakemiin tere, nagemist kuei, niaut konnichiwa, sayonara privet, dosvidanya ahoj, cau cześć, widzenia merhaba, elveda hallo, vaarwel saluton, adiaŭ
gui appearance:
Comments
Post a Comment