Thread: Fire an event with a custom component in java
hi, i'm making custom component(it's jpanel squares draw @ hand), don't know how launch actionevent there.
example(my english not good):
custom component names gridcard.
if made simple gui program jpanel called panel1 , do:
panel1.add(button1) //a generic button.
panel1.add(text1) //a generic text box/label, doesn't mind.
panel1.add(grid1) //an instance of custom component gridcard.
when put next code:
public void actionperformed(actionevent e)
{
if (e.getsource() == button1)
{
text1.settext("hi");
}
else if (e.getsource() == grid1)
{
text1.settext("goodbye");
}
}
when click in button(or press space when focus in button), actionperformed launched. how do code works(that actionperformed launched when click in grid1)?, thanks.
consider defining gridcard subclass of abstractbutton.
code:import javax.swing.*; import java.awt.event.*; class gridcard extends abstractbutton { ... } public class myclass extends jframe implements actionlistener { public myclass() { jpanel panel = new jpanel(); gridcard gc = new gridcard(); gc.addactionlistener(this); panel.add(gc); // ... } public void actionperformed(actionevent event) { ... } }
Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Programming Talk Fire an event with a custom component in java
Ubuntu
Comments
Post a Comment