1

I've created two JFrames. The main JFrame contains the text area. My sub JFrame contains a drop down list. The task is to pass the value that I've selected in the drop down list and display in the text area in the main JFrame.

Code in the sub JFrame:

private void btnOKActionPerformed(java.awt.event.ActionEvent evt) {
    close();      
    room=cmbRoom.getSelectedItem().toString();
}

Code in the main JFrame:

private void btnDisplayActionPerformed(java.awt.event.ActionEvent evt) {
    roomNo r=new roomNo();
    txtArea2.append("\nRoom Number: " + r.getroom());
}                                           
mKorbel
  • 109,525
  • 20
  • 134
  • 319
AJ J
  • 101
  • 1
  • 2
  • 6
  • 1
    See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) Use a modal `JDialog` for the 2nd frame. Honestly, this comes up around every 8 hours on SO. Please search in future! – Andrew Thompson May 05 '13 at 03:07
  • *"this comes up around every 8 hours on SO."* Just look at answers to this *far **more** specific* search http://stackoverflow.com/search?q=title%3A%22another+JFrame%22 based on your question title. – Andrew Thompson May 05 '13 at 06:09

3 Answers3

0
class NextPage extends JFrame
{
  NextPage(String st)
  {
     setLayout(null);
     setDefaultCloseOperation(javax.swing. WindowConstants.DISPOSE_ON_CLOSE);
     setTitle("Welcome");
     JLabel lab=new JLabel("Welcome  "+st);
     lab.setBounds(10,10,500,20);
     add(lab);
     setSize(300, 100);
  }
}
M. Haris Azfar
  • 598
  • 6
  • 16
0

This might not be exactly correct answer but this will do the job.

Suppose you have 2 Jframes namely Home.java and Second.java

the code for Second.java as below,

public static String selection = "";//static variable to store seletced value from combobox
Home h = new Home();//instance of Home Jframe

/**
* return selected value (called from Home Jframe)
*/
public static String getSeletced() {
    return selection;
}

/**
* get selected value from comboBox event
*/
private void cmbLapActionPerformed(java.awt.event.ActionEvent evt) {                                       
    selection = cmbLap.getSelectedItem().toString();
    h.isSelected = true;//this is to control data duplication
}  

Now for the Home.java file we can use formWindowGainedFocus event to update the jTextArea. Home .java file contains following code,

public static boolean isSelected = false;//flag to check combo box is selected

private void formWindowGainedFocus(java.awt.event.WindowEvent evt) {                                       
    System.out.println(isSelected);
    if (isSelected) {
        String text = new Second().getSeletced();
        System.out.println(text);
        txaData.append("Your Laptop: " + text + "\n");//appending data
        isSelected = false;//to prevent duplication
    }
}  

This method can use to update jTextArea using data from another jFrame.

ThilinaMD
  • 365
  • 3
  • 13
-1
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class PassData extends JFrame
{
    JTextField text;
    PassData(){
    JLabel l=new JLabel("Name: ");
    text=new JTextField(20);
    JButton b=new JButton("Send");
    setLayout(null);
    l.setBounds(10,10,100,20);
    text.setBounds(120,10,150,20);
    b.setBounds(120,40,80,20);
    add(l);
    add(text);
    add(b);
    setVisible(true);
    setSize(300,100);
      b.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
        String value=text.getText();
        NextPage page=new NextPage(value);
        page.setVisible(true);
        }
    });
   }
    public static void main(String[] args) 
  {
    new PassData();
  }
}
M. Haris Azfar
  • 598
  • 6
  • 16