-1

What kind of frames/panes are the best practice to accommodate goal below?

  • main frame has jtable1
  • pressing ENTER on jtable1 will call sub frame
  • sub frame has jtable2 with 2 rows data
  • both jtable1 and jtable2 has exact same column names

By pressing ENTER on row 2 of jtable2, these below will happen:

  • value of current row of jtable1 = value of row 2 of jtable2
  • jtable1 will create new row
  • dispose sub frame
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Hendra
  • 43
  • 9
  • 1
    Why have two tables? Why not just have one and swap the models? – MadProgrammer Jun 29 '18 at 04:39
  • 1
    *"..should probably appear in a (modal) `JDialog`"* .. or what @MadProgrammer suggested (change the model in one table), which sounds better. – Andrew Thompson Jun 29 '18 at 04:41
  • @MadProgrammer, jtable1 is the main table, when need looking up for desired item (ex. select productname from master where productid like '%pencil%'), user need to find it in frame2 n put the desired items on jtable2, then user can choose which item is needed from jtable2, then transfer its value to jtable1 and jtable2 is disposed and back to jtable1. – Hendra Jun 29 '18 at 08:40
  • @andrew, There is actually a textfield in frame2 as input (pencil as input) for (select productname from master where productid= like '%pencil%'). there will b several results which is put in jtable2. then user will choose which pencil is needed from jtable2 n transfer its value to jtable1 by pressing ENTER b4 disposing frame2. then go back to jtable1 in frame1 n continue for other product. I'll try using jdialog / joptionpane as you suggest, hoping that it'll work. – Hendra Jun 29 '18 at 08:55
  • @andrew, thank you, (modal) Jdialog is the best option because it prohibit access to other frame till its closed. It took me a week to solve it at last. Components' variables is kind of complicated with multi frame. Is it allowed to share the code as answer to my own post here? – Hendra Jul 06 '18 at 08:47

1 Answers1

1

Here is sample which could be an answer to question above:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.KeyStroke;
import javax.swing.table.DefaultTableModel;

public class Dialog3 {

    public static void main(String[] args) {     
        //f1=JFrame 1, f1cp=ContentPane in f1, f1table=JTable in f1, f1sp=ScrollPane in f1
        JFrame f1=new JFrame("Frame");
        f1.setBounds(0, 0, 300, 400);
        f1.getContentPane().setLayout(new BorderLayout());
        f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f1.setLocationRelativeTo(null);

        JPanel f1cp=new JPanel();
        f1.getContentPane().add(f1cp, BorderLayout.CENTER);

        String[] columnNames = {"CODE", "NAME"};
            Object[][] data = {{"code 8","name 8"}, {null,null}};
            DefaultTableModel f1model = new DefaultTableModel(data, columnNames);
            JTable f1table=new JTable(f1model);
        JScrollPane f1sp=new JScrollPane(f1table);
        f1cp.add(f1sp);

        f1.pack();
        f1.setVisible(true);        

        f1table.getInputMap().put(KeyStroke.getKeyStroke("pressed ENTER"), "f1enter");
        f1table.getActionMap().put("f1enter", new AbstractAction(){
            @Override
            public void actionPerformed(ActionEvent e){showd1(f1,f1table, f1model);}
        }); 

    } //end of main(string[])

    private static void showd1(JFrame f1, JTable f1table, DefaultTableModel f1model){
        //d1=JDialog1, d1cp=ContentPane in d1, d1table=JTable in d1, d1sp=ScrollPane in d1
        JDialog d1=new JDialog(f1, "Dialog", true);
        d1.setBounds(0, 0, 300, 400);
        d1.setLocationRelativeTo(f1);

        JPanel d1cp=new JPanel();
        d1.getContentPane().add(d1cp, BorderLayout.CENTER);

        String[] columnNames = {"CODE", "NAME"};
            Object[][] data = {{"code 1","name 1"}, {"code 2","name 2"}};
            DefaultTableModel model = new DefaultTableModel(data, columnNames);
            JTable d1table=new JTable(model);
        JScrollPane d1sp=new JScrollPane(d1table);
        d1cp.add(d1sp);

        d1table.getInputMap().put(KeyStroke.getKeyStroke("pressed ENTER"), "d1tf");
        d1table.getActionMap().put("d1tf", new AbstractAction(){
            @Override
            public void actionPerformed(ActionEvent e){
                f1table.setValueAt(d1table.getValueAt(d1table.getSelectedRow(), 0),f1table.getSelectedRow(), 0);
                f1table.setValueAt(d1table.getValueAt(d1table.getSelectedRow(), 1),f1table.getSelectedRow(), 1);
                f1model.addRow(data);   
                f1table.setValueAt(null, f1model.getRowCount()-1, 0);
                f1table.setValueAt(null, f1model.getRowCount()-1, 1);
                d1.dispose();
            }
        });

        d1.pack();
        d1.setVisible(true);        
    } //end of showd1
}
Hendra
  • 43
  • 9