java - Adding Listeners to JButtons -
i trying add actionlisteners jbuttons new
, delete
, search
. i've tried several things , ways still cannot work.
p.s i've added action button new
, seems work not add in array.
every appreciated! in advance.
package datingrun; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.util.arraylist; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.joptionpane; import javax.swing.jtextarea; import javax.swing.jtextfield; public class main extends javax.swing.jframe { public static void main(string[] args) { jframe frame = new jframe("onomata"); jlabel ll = new jlabel("Êáôá÷ùñÞóåéò"); ll.setbounds(150, 20, 300, 15); jlabel l2 = new jlabel("onoma"); l2.setbounds(155, 80, 300, 15); jlabel l3 = new jlabel("filo"); l3.setbounds(270, 80, 100, 15); jlabel l4 = new jlabel("ilikia"); l4.setbounds(280, 80, 100, 15); jbutton b1 = new jbutton(); b1.settext("new"); b1.setbounds(105, 400, 80, 40); jbutton b2 = new jbutton(); b2.settext("search"); b2.setbounds(205, 400, 80, 40); jbutton b3 = new jbutton(); b3.settext("delete"); b3.setbounds(305, 400, 80, 40); jtextfield tf1 = new jtextfield(20); tf1.setsize(15, 20); tf1.setbounds(130, 100, 100, 20); jtextfield tf2 = new jtextfield(20); tf2.setsize(15, 20); tf2.setbounds(250, 100, 100, 20); jtextarea t1 = new javax.swing.jtextarea(); t1.setcolumns(20); t1.setrows(5); t1.seteditable(false); t1.setbounds(125, 200, 120, 150); jtextarea t2 = new javax.swing.jtextarea(); t2.setcolumns(20); t2.setrows(5); t2.seteditable(false); t2.setbounds(245, 200, 120, 150); jtextarea t3 = new javax.swing.jtextarea(); t3.setcolumns(30); t3.setrows(5); t3.seteditable(false); t3.setbounds(245, 220, 100, 100); frame.add(ll); frame.add(l2); frame.add(l3); frame.add(tf1); frame.add(tf2); frame.add(t1); frame.add(t2); frame.add(t3); frame.add(b1); frame.add(b2); frame.add(b3); frame.setsize(500, 500); frame.setlayout(null); frame.setlocationrelativeto(null); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setvisible(true); final arraylist onomata = new arraylist(); onomata.add("stelios mavridis"); onomata.add("nikos tzouvelekis"); onomata.add("andreas paraskevas"); final arraylist filo = new arraylist(); filo.add("m"); filo.add("m"); filo.add("m"); final arraylist ilikia = new arraylist(); ilikia.add("24"); ilikia.add("30"); ilikia.add("40"); t1.append("onoma \n"); t2.append("filo \n"); t3.append("ilikia \n"); (int = 0; < onomata.size() && < filo.size() && < ilikia.size(); i++) { t1.append((string) onomata.get(i) + "\n"); t2.append((string) filo.get(i) + "\n"); t3.append(ilikia.get(i).tostring() + "\n"); } b1.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { string text = joptionpane.showinputdialog("insert"); string item = null; if (text != null) { item = text.trim(); } else { return; } if (!item.isempty()) { onomata.add(item); filo.add(item); ilikia.add(item); } } }); } }
start having @ how write action listeners.
since you've got actionlistener
registered jbutton
, can assume problem solved.
after testing code, item
been added arraylist
s, you're not updating ui new content, maybe like...
b1.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { string text = joptionpane.showinputdialog("insert"); string item = null; if (text != null) { item = text.trim(); } else { return; } if (!item.isempty()) { onomata.add(item); filo.add(item); ilikia.add(item); t1.settext(null); t2.settext(null); t3.settext(null); (int = 0; < onomata.size() && < filo.size() && < ilikia.size(); i++) { t1.append((string) onomata.get(i) + "\n"); t2.append((string) filo.get(i) + "\n"); t3.append(ilikia.get(i).tostring() + "\n"); } } } });
now, having run code, jtextarea
not suited task seem trying complete, instead, maybe consider using jtable
, see how use tables more details.
avoid using null
layouts, pixel perfect layouts illusion within modern ui design. there many factors affect individual size of components, none of can control. swing designed work layout managers @ core, discarding these lead no end of issues , problems spend more , more time trying rectify
see laying out components within container more details
Comments
Post a Comment