Passing Values ​​Between JFrames

I have two Jframes where frame1 has some text fields and when the button on frame1 is pressed, I open another JFrame that contains the search field and JTable containing the search results.

When I click on the result line in JTable, I want certain values ​​to be reflected in the text fields of frame1.

I tried passing the JFrame1 object as a parameter, but I don't have a clear idea of ​​how to achieve this. Any help would be greatly appreciated. Thanks

+6
source share
1 answer

First of all, your program design seems a bit off, as if you were using JFrame for one of your windows, where you really should use JDialog, as it sounds as if one window depended on another.

But regardless of whether you pass references to GUI objects in the same way as standard Java code without a GUI. If one window opens another (the second is often a dialog), then the first window usually already contains a link to the second window and can call methods from it. The key is often when the first window calls the second method to get its state. If the second is a modal dialog, then when it's easy - right after returning the dialog that will be in the code right after you install the second dialog. If this is not a modal dialog, then you probably want to use some kind of listener to find out when to retrieve the information.

Having said that, the details will depend on your program structure, and you will need to tell us more about this if you want more specific help.

For a simple example in which one window opens another, allows the user to enter text in the JTextField dialogs, and then puts the text in the first JTextField, please look at this:

import java.awt.Window; import java.awt.Dialog.ModalityType; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class WindowCommunication { private static void createAndShowUI() { JFrame frame = new JFrame("WindowCommunication"); frame.getContentPane().add(new MyFramePanel()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } // let be sure to start Swing on the Swing event thread public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } } class MyFramePanel extends JPanel { private JTextField field = new JTextField(10); private JButton openDialogeBtn = new JButton("Open Dialog"); // here my main gui has a reference to the JDialog and to the // MyDialogPanel which is displayed in the JDialog private MyDialogPanel dialogPanel = new MyDialogPanel(); private JDialog dialog; public MyFramePanel() { openDialogeBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { openTableAction(); } }); field.setEditable(false); field.setFocusable(false); add(field); add(openDialogeBtn); } private void openTableAction() { // lazy creation of the JDialog if (dialog == null) { Window win = SwingUtilities.getWindowAncestor(this); if (win != null) { dialog = new JDialog(win, "My Dialog", ModalityType.APPLICATION_MODAL); dialog.getContentPane().add(dialogPanel); dialog.pack(); dialog.setLocationRelativeTo(null); } } dialog.setVisible(true); // here the modal dialog takes over // this line starts *after* the modal dialog has been disposed // **** here the key where I get the String from JTextField in the GUI held // by the JDialog and put it into this GUI JTextField. field.setText(dialogPanel.getFieldText()); } } class MyDialogPanel extends JPanel { private JTextField field = new JTextField(10); private JButton okButton = new JButton("OK"); public MyDialogPanel() { okButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { okButtonAction(); } }); add(field); add(okButton); } // to allow outside classes to get the text held by the JTextField public String getFieldText() { return field.getText(); } // This button action is simply to dispose of the JDialog. private void okButtonAction() { // win is here the JDialog that holds this JPanel, but it could be a JFrame or // any other top-level container that is holding this JPanel Window win = SwingUtilities.getWindowAncestor(this); if (win != null) { win.dispose(); } } } 

You would make a very similar method to get information from JTable.

And again, if this information does not help you, please tell us more about your program, including showing us some of your code. The best code to show is a small compiled example, SSCCE , similar to what I posted above.

+28
source

Source: https://habr.com/ru/post/894755/


All Articles