0

I am try to learn java. I create architecture but i dont know how can i send data with two form here is my source code...

This part wich is table and buttons;

package main;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class Form1 extends JFrame implements ActionListener 
{
   JButton button = new JButton("Open form");
   public Form1(){
      String[] column = {"Name", "Surname"};
      Object[][] data ={};

      DefaultTableModel model = new DefaultTableModel(data, column);
      JTable table = new JTable(model);
      JScrollPane pane = new JScrollPane(table);
      JFrame frame = new JFrame();
      button.setBounds(150, 300, 80, 35);
      pane.setBounds(150, 200, 150, 80);
      frame.add(pane);
      frame.add(button);
      frame.setLayout(null);
      frame.setTitle("Form1");
      frame.setResizable(false);
      frame.setSize(450,380);
      frame.setLocationRelativeTo(null);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true); 
      button.addActionListener(this);

   }
   public void actionPerformed (ActionEvent action){
      if(action.getSource() == button )
      {
         Form2 f2 = new Form2();
         dispose();
      }
   } 
   public static void main(String[] args) {
      Form1 f1 = new Form1();
   }  
}

I want to get values from to this form;

package main;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

public class Form2 extends JFrame implements ActionListener{
   JTextField txt = new JTextField(10);
   JTextField txt2 = new JTextField(10);
   JButton addButton = new JButton("add");
   public Form2(){
      JFrame frame = new JFrame();
      txt.setBounds(20, 50, 80, 22);
      txt2.setBounds(20, 100, 80, 22);
      addButton.setBounds(20, 150, 80, 30);

      frame.add(txt);
      frame.add(txt2);
      frame.add(addButton);
      frame.setLayout(null);
      frame.setTitle("Form2");
      frame.setResizable(false);
      frame.setSize(450,380);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
      addButton.addActionListener(this);    
   }
   public void actionPerformed (ActionEvent action){
      if(action.getSource() == addButton){
         String Name= this.txt.getText();
         String Surname= this.txt2.getText();
         //DefaultTableModel model = (DefaultTableModel) Form1.table.getModel();
         Object[] row = {Name, Surname};
         Form1 f1 = new Form1();
         dispose();
      }
   }  
}

I check the tutorials and other youtube channels and no one doesnt like this can you give me one refrenece to learn with tutorial or examples.

  • 1
    Don't use null layout, Swing was designed to work with different screen sizes, reslutions and PLAFs, instead use a layout manager or [combinations of them](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html), also take a look at [Null layout is evil](http://www.leepoint.net/GUI/layouts/nulllayout.html) and [Why is it frowned upon to use a null layout in swing?](http://stackoverflow.com/questions/6592468/why-is-it-frowned-upon-to-use-a-null-layout-in-swing) – Frakcool Jan 03 '17 at 20:40
  • 1
    Some other things that I saw in your code: 1) You're using multiple `JFrame`s, see [The use of multiple JFrames, Good / Bad Practice?](http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-or-bad-practice) (BAD) – Frakcool Jan 03 '17 at 20:45
  • 2) You're extending `JFrame` and creating a `JFrame` object, when you extend `JFrame`, basically you're saying your class **is a** `JFrame` and it makes it less flexible, if you need to extend something extend `JPanel` then, also you're never using the `JFrame` you're extending, see [this question](http://stackoverflow.com/questions/41252329/java-swing-using-extends-jframe-vs-calling-it-inside-of-class) too – Frakcool Jan 03 '17 at 20:45
  • 1
    If this is not a duplicate, please edit your question to include a [mcve] that shows your revised approach based on the example and comments. – trashgod Jan 03 '17 at 20:59

0 Answers0