In an attempt to tidy up my (very basic) UI code i have split the button and text field into two seperate classes, with a main to create a frame and run. The button needs to affect the contents of the text field when clicked, so i have used a mouseEvent
. However, i cannot call my textfield's method, as the textfield object is stored in main, so there is no object to use a method for. All methods are public and all attributes are private. All help is apprciated, thank you.
I have tried making the object public statiic
to no avail, i'm not sure if there's something brightly obvious that I am missing here.
Fpor context, the mouseEvent
needs to call the method rename(String)
from the text field object, known as tf, in the class gui1
edit:
(main)
public interface gui1{
public static void main(String[] args) {
textfieldobj tf = new textfieldobj("You should press button 1",100,100, 150,20);
buttonObject b = new buttonObject("new button!");
(in buttonObject class)
public class buttonObject extends JButton implements{
JButton b;
public buttonObject(String text){
JButton b=new JButton(text);
b.setBounds(100,100,60,60);
b.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
tf.setText("You Did It");//problem area
b.setEnabled(false);
(in textfield class)
public void setText(String newtext) {
text = newtext;
super.setText(newtext);
}