Home > Java > javaTutorial > body text

How do you pass data from a JTable in one JFrame to text fields in another JFrame?

Barbara Streisand
Release: 2024-10-26 19:00:30
Original
399 people have browsed it

How do you pass data from a JTable in one JFrame to text fields in another JFrame?

Passing Data Between JFrames

In this scenario, you have two JFrames: frame1, which includes text fields, and frame2, which contains a search box and a JTable containing search results. Your goal is to reflect the selected values from the JTable in the text fields of frame1.

Object References and Communication

Your question mentions passing frame1's object as a parameter, but it's crucial to understand that passing references between GUI objects follows the same principles as passing references in non-GUI Java code. The reference to the second JFrame (frame2) is typically accessible within the first JFrame (frame1), allowing you to invoke its methods directly.

Cross-Window State Access

The timing of accessing the state of one window from another depends on the window relationship. For modal dialogs, the first window can retrieve data immediately after the dialog returns, which is the code executed after setting the dialog visible.

For non-modal dialogs, you often rely on listeners to determine when to extract the information.

Example: Passing Text Between Windows

To illustrate this concept, consider the following example:

<code class="java">import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class WindowCommunication {

    public static void main(String[] args) {
        JFrame frame1 = new JFrame("Frame 1");
        frame1.getContentPane().add(new JLabel("Text: "));
        frame1.getContentPane().add(new JTextField(10));
        JButton button = new JButton("Open Frame 2");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JDialog frame2 = new JDialog(frame1, "Frame 2");
                frame2.getContentPane().add(new JTextField(10));
                frame2.pack();
                frame2.setVisible(true);
            }
        });
        frame1.getContentPane().add(button);
        frame1.pack();
        frame1.setLocationRelativeTo(null);
        frame1.setVisible(true);
    }
}</code>
Copy after login

In this example, when the button in frame1 is clicked, a JDialog frame2 is opened. The reference to frame2 is held by frame1, allowing the latter to access and manipulate the text in the JTextField within frame2.

Similarly, you can retrieve data from a JTable in your specific scenario by using listener-based techniques to determine when the desired row is selected in the table.

The above is the detailed content of How do you pass data from a JTable in one JFrame to text fields in another JFrame?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!