There are 2 frames. On one there is a table, on the other - a button. Required when you click on the button add to the table entry. They say: “use ActionListener”, but I don’t even have an idea how it should look. Can I have a code sample?


Frame1:

public class Frame1 extends javax.swing.JFrame { DefaultTableModel data = new DefaultTableModel(); public Frame1() { initComponents(); data.addColumn("A"); data.addColumn("B"); jTable1.setModel(data); } 

Frame2:

 public class Frame2 extends javax.swing.JFrame { Frame1 fr; public Frame2(final Frame1 fr) { this.fr = fr; initComponents(); jButton1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { fr.data.addRow(new Object[] {1, 2}); } }); } 

MainClass:

 public class MainClass { public static void main(String[] args) { new Frame1().setVisible(true); new Frame2(new Frame1()).setVisible(true); } } 
  • one
    read about the swing - Gorets
  • What to read, where to read? I know perfectly well about Swing, I use it, so it has nothing to do with the question at all. And the question is, how do I use an ActionListener in this case. If you have something to say - say, no - go past. It's simple. - Sergey1991
  • one
    What do you know about swing? how to handle clicks? These are the basics that go from the first pages of books, in the examples, where you need to display something by pressing a button, there is the code: button.setOnClickListener (this); There are examples in any book on java - Gorets
  • Why do you even use frames? - Sergey
  • Well kakbe course writing, but why do frames are used? - Sergey1991

2 answers 2

It means so. First, how to hang ActionListener ? Like this :

 button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // этот код будет вызван при нажатии кнопки } }); 

Then, what to do when you click a button in this ActionListener itself? You need to add data to the TableModel . You now have a TableModel stored in Frame1 . Conceptually this is not true, but God bless him. Your ActionListener code lies in Frame2 , which means Frame2 must have a reference to Frame1 . (Actually, of course, business logic should be completely removed from the UI-code, but this is for the future.)

So, to start Frame2 should have a link to an instance of Frame1 . And in this ActionListener itself, call the public function from there, which will update the TableModel .

All clear?

PS: why do you need two main functions? One of them will not work.

  • And how to get a link to a copy of Frame1? - Sergey1991
  • @ Sergey1991: whatever. For example, pass in the constructor. - VladD
  • one
    @ Sergey1991: strange, are you sure that you are not mistaken anywhere? And how do you call the Frame2 constructor? - VladD
  • one
    @ Sergey1991: well, turn on your head! You create and display one instance of Frame1, and in Frame2 you transfer another. Guess yourself what to do? - VladD
  • one
    I guess. I've done everything. Many thanks for the help - not everyone would have had the patience)) And I was stupid as I could)) - Sergey1991
 public class MainClass { public static void main(String[] args) { Frame1 frame1 = new Frame1(); frame1.setVisible(true); Frame2 frame2 = new Frame2(frame1); frame2.setVisible(true); } } 

Maybe someone else will help ...