How to Show output Jlabel another Jframe - java

When i input Text in the JtextField of FrameIn, and then click button OK, the Text will display on the Jfield of FrameShow the last frame is what I want, cause I still don't know how to make it.
I am using NetBeans GUI builder.
package learn;
public class FrameIn extends javax.swing.JFrame {
private String Name = null;
public FrameIn() {
initComponents();
}
*
*
private void ButtonActionPerformed(java.awt.event.ActionEvent evt) {
FrameShow show = new FrameShow();
Name = Text.getText();
this.dispose();
show.setVisible(true);
}
public String getName(){
return this.Name;
}
and This my FrameShow
public class FrameShow extends javax.swing.JFrame {
/**
* Creates new form Frame1
*/
public FrameShow() {
FrameIn inName = new FrameIn();
initComponents();
Label.setText(inName.getName());
}
So if i input Text in the JtextField of FrameIn, then output will display on the Jfield of FrameShow second Jframe
Output form this code is null on the Jfield

You can pass your parametres between the two Frame,
so when you click a your button, make an action that call your frameShow, and you can pass your values, in the constructor of your frame or you can create a field in your second frame and use setter to put your value, here is the idea.
class A{
...
//action
String v = textField.getText();
B b = new B(v);
...
}
class B{
public B(String v){
this.label.setText(v);
}
}
Second idea :
class A{
...
//action
String v = textField.getText();
B b = new B();
b.setLabelValue(v);
...
}
Here is your code should be look like:
private void ButtonActionPerformed(java.awt.event.ActionEvent evt) {
Name = Text.getText();
FrameShow show = new FrameShow(Name);
this.dispose();
show.setVisible(true);
}
public FrameShow(String name) {
initComponents();
Label.setText(name);
}
Hope you get my point and you understand the idea.

Related

Change value of public variable in Java, used by multiple frames

I have two frames in NetBeans 9.0 as frame1.java, frame2.javaand the main class as main.java.
If I declare a public variable in frame1.java as
public String stringName;
and a function fn() which gives the value of stringName in frame1as say "abcd".
When I write this in frame2,
frame1 fm = new frame1();
String str = frame1.stringName;
System.out.print(str);
I get the output as null. But what I require is "abcd".
What am I doing wrong, and what should it be?
Thanks for help!
Edit:
I have linked frame1 and frame2 such that the GUI from frame1 leads to frame2, and so does the value.
Edit 2
The process goes like this:
GUI of frame1 is visible >> based on user's input, function fn() stores the value, say "abcd" in stringName >> a button click in frame1 leads to frame2>> variable str gets the value from stringName >> System.out.print(str) outputs the value as null .
CODE
frame1:
public class frame1 extends javax.swing.JFrame {
public String stringName;
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){
stringName = jTextField1.getText(); // gets a not null value
}
}}
frame2:
public class frame2 extends javax.swing.JFrame {
frame1 fm = new frame1();
String str = frame1.stringName;
System.out.print(str); //outputs a null value
}
The point ist that you are crating a new Instance (frame1, fm) in your class frame2. So the value from the string in this new Instance is null. You need a reference to your old Instance which you maybe have initialised in your main method?
Something like that:
String str = myOldInstance.stringName;
But you should create getter an setter and make your var private.
But to help you exactly we need more Code.
in this case the best is Listener pattern.
Create interface of listener, which will inform about change text. In class - target of this information - create instance of this listener and return that. In class - source of information - set listener and put on field.
When you want inform of change text, you fire method of listener, and on seconde frame will execute implementation of method.
Below example - I fire on button click.
Any way, field should be private, and add getter and setter. Public fields are bad.
Main class
public class App {
public static void main(String[] args) {
Frame1 f1=new Frame1();
Frame2 f2=new Frame2();
TextListener textListener = f2.getListener();
f1.setListener(textListener);
}
}
Listener
public interface TextListener {
public void onTextPropagate(String text);
}
Frame classes
public class Frame1 extends JFrame{
private TextListener listener;
JButton button;
public Frame1() {
super("Frame1");
setBounds(200, 200, 400, 600);
button=new JButton("Action");
button.setBounds(100, 200, 200, 100);
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if(listener!=null) {
String text = UUID.randomUUID().toString();
System.out.println("On Frame1:\t"+text);
listener.onTextPropagate(text);
}
}
});
this.add(button);
setVisible(true);
}
public void setListener(TextListener listener) {
this.listener=listener;
}
}
public class Frame2 extends JFrame{
public Frame2() {
super("Frame2");
setBounds(100, 100, 200, 400);
setVisible(true);
}
public TextListener getListener() {
return new TextListener() {
#Override
public void onTextPropagate(String text) {
reactOnChangeText(text);
}
};
}
private void reactOnChangeText(String text) {
System.out.println("On Frame2:\t"+text);
}
}

unable to setText in Java Frame

Hey I am creating a java project. In which I have a insert record frame, on insert frame I have a option to enter father ID and if the user did not know the father id, so I have set a button to find the father id.
when the user will click on that button, the new frame will appear and user can search for id, the result will show in a table and when user click on the particular record, the frame will dispose and should set the respective id on the previous frame.
I have written the code for it, and it is passing the value to the previous frame but it is not setting the value to the textfield that I want it to be. Where I am doing wrong? Here is the code.
FamilyInsert.java
public class FamilyInsert extends javax.swing.JFrame {
/**
* Creates new form FamilyInsert
*/
int id = DBManager.genID();
public int fid;
public FamilyInsert() {
initComponents();
txtId.setText(""+id);
txtName.requestFocus();
}
public void setFid(int fid){
txtFid.setText(""+fid);
System.out.println("setFID "+fid);
}
public void reset()
{
txtName.setText("");
txtFather.setText("");
txtFid.setText("");
txtCity.setText("");
txtState.setText("");
txtName.requestFocus();
}
private void btnSubmitActionPerformed(java.awt.event.ActionEvent evt) {
int id = Integer.parseInt(txtId.getText());
String name = txtName.getText();
String fname = txtFather.getText();
int fid = Integer.parseInt(txtFid.getText());
String city = txtCity.getText();
String state = txtState.getText();
Family family = new Family(id,name,fname,fid, city,state);
boolean flag = false;
flag = DBManager.insertMember(family);
if(flag==true){
JOptionPane.showMessageDialog(this,"Successfully Saved");
id++;
txtId.setText(""+id);
reset();
}
else
{
JOptionPane.showMessageDialog(this,"Error Occured");
}
}
private void txtFidActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {
SearchFatherFrame f = new SearchFatherFrame();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
and from the search frame:
private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {
int id;
if(evt.getClickCount()==2){
if(jTable1.getSelectedRow()!=-1)
{
int index = jTable1.getSelectedRow();
Family s = list.get(index);
id = s.getId();
System.out.println("ID from search frame "+id);
FamilyInsert f = new FamilyInsert();
f.setFid(id);
this.dispose();
//JOptionPane.showMessageDialog(this, s.getId()+"\n"+s.getName());
}
}
Your problem is that you're creating a new FamilyInsert object within the other class, and changing its state, but this leaves the state of the original FamilyInsert object unchanged. What you need to do instead is to pass a reference of the original displayed FamilyInsert into the 2nd object, and then change its state.
Change this:
SearchFatherFrame f = new SearchFatherFrame();
to something more like:
SearchFatherFrame f = new SearchFatherFrame(this);
Pass the reference into the class and use to set a field:
public class SearchFatherFrame {
private FamilyInsert familyInsert;
public SearchFatherFrame(FamilyInsert familyInsert) {
this.familyInsert = familyInsert;
// other code....
}
}
Then use that reference passed in to change the state of the original object.
if(jTable1.getSelectedRow()!=-1) {
int index = jTable1.getSelectedRow();
Family s = list.get(index);
id = s.getId();
System.out.println("ID from search frame "+id);
// FamilyInsert f = new FamilyInsert();
// f.setFid(id);
familyInsert.setFid(id); // **** add
this.dispose();
//JOptionPane.showMessageDialog(this, s.getId()+"\n"+s.getName());
}
Also you want the 2nd window to be a JDialog not a JFrame. Please see: The Use of Multiple JFrames, Good/Bad Practice?
Could you try
public void setFid(int fid){
txtFid.setText(""+fid);
System.out.println("setFID "+fid);
yourJFrame.setVisible(true); //Reloads the frame
}

How to access an Existing JFrame Form?

I'm having a little problem accessing an existing JFrame form (I'm doing this in NetBeans by the way):
public class FrameA extends javax.swing.JFrame {
FrameB b; //creates a new JFrame called b
FrameA a = this; //this JFrame is assigned to a variable a
String x;
public FrameA() {
initComponents();
}
private void btnClickMe (java.awt.event.ActionEvent evt) {
x = jTextField1.getText();
b = new FrameB(); //creates a new instance of FrameB
b.setVisible (true); //opens the new JFrame
a.setVisible(false); //hides or closes the current JFrame
}
public String getMe () {
return x; //attempts to get the value of x
}
}
public class FrameB extends javax.swing.JFrame {
FrameA a = this.FrameA(); //calls the existing JFrame FrameA
String clickie = a.getMe(); //attempts to get the x variable from FrameA
//assigns it to a variable then display on the textarea
jTextArea1.setText(clickie);
public FrameB() {
initComponents();
}
}
I am not so sure what I am doing wrong and am a little confused on how to really access the existing JFrame (FrameA here in this case) from the current JFrame (which is FrameB)
Any help is greatly appreciated! Thanks!

Text of JTextArea not being retrieved?

I want to get the text of private access JTextArea from another class in the same package and store/save the text into a String.
public class JTextAreaDemo extends javax.swing.JFrame {
public JTextAreaDemo() {
initComponents();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
TxtArea_Class d = new TxtArea_Class();
d.readJtxtAreaText();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new JTextAreaDemo().setVisible(true);
}
});
}
private javax.swing.JTextArea jTextArea1;
/**
* #return the jTextArea1
*/
public String getjTextArea1() {
return jTextArea1.getText();
}
/**
* #param jTextArea1 the jTextArea1 to set
*/
public void setjTextArea1(javax.swing.JTextArea jTextArea1) {
this.jTextArea1 = jTextArea1;
}
Now I want to save the text of JTextArea to string in below class
public class TxtArea_Class {
JTextAreaDemo demo;
String txt;
public TxtArea_Class(){
demo = new JTextAreaDemo();
txt = new String();
}
public void readJtxtAreaText(){
txt = demo.getjTextArea1();
if(txt.isEmpty()){
System.out.println("Failed To Get TextArea Contents ");
}
else{
System.out.println("Successfully Get TextArea Contents ");
}
}
Console Output :
Failed to Get TextArea Contents
Problem is in your TextArea_Calss's constructor
try the following.
public TextArea_class(TextAreaDemo demo) {
this.demo = demo;
this.str = new String();
}
and in button event. do this.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
TxtArea_Class d = new TxtArea_Class(this);
d.readJtxtAreaText();
}
In current implementation, Every-time you create an instance of TextArea-calss a new frame get created. because in TextArea_Class constructor you are creating an new instance of demo class.
and you are trying to get value from newly created demoFrame(that might be invisible for you but exist).
I'm hoping this will solve your issue.
You have two different instances of JTextAreaDemo!! One created in main and made visible, the other created in TxtArea_Class. The first one is the one on the screen, and the second is the one you read the string from. So the text you enter into the first doesn't show in the second.
I got the contents of JTxtArea from another class by updating my code by this.
TextArea_class
public TextArea_class(TextAreaDemo demo) {
this.demo = demo;
this.str = new String();
}
JTxtAreaDemo
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
TxtArea_Class d = new TxtArea_Class(this);
d.readJtxtAreaText();
}

Java, changing active card for another class

Looking at other answers, i have followed exactly what they say, but i just keep getting the nullPointerException error. I have 4 classes, the 2 below, a GUI class and main menu class. Main manages the card layout and i would like a button in the Insert class to change the "Active" card to main menu class.
Main:
public class Main extends JPanel implements ChooserListener{
MainMenu mm;
Insert InsertCustomer;
public JPanel mPanel;
CardLayout cl;
private String c;
public Main(){
super();
//add mPanel, set to CardLayout and add the Main
mPanel = new JPanel();
this.add(mPanel);
cl = new CardLayout();
mPanel.setLayout(cl);
//add classes
mm = new MainMenu(this);
InsertCustomer = new Insert();
//add classes to mPanel
mPanel.add(mm, "mm");
mPanel.add(InsertCustomer, "InsertCustomer");
}
public void tell(Object o) {
c = o.toString();
cl.show(mPanel, c);
}
public void swapView(String key) {
CardLayout cl = (CardLayout)(mPanel.getLayout());
cl.show(mPanel, key);
}
}
Insert:
public class Insert extends JPanel{
private JButton logoutbutton;
private LogoutListener lListener;
public Insert() {
super();
//BUTTONS
//logout button
JButton logoutbutton = new JButton("Main Menu");
this.add(logoutbutton);
lListener = new LogoutListener(null);
logoutbutton.addActionListener(lListener);
}
private class LogoutListener implements ActionListener{
private Main main;
public LogoutListener(Main main){
this.main = main;
}
public void actionPerformed(ActionEvent e) {
main.swapView("mm");
}
}
}
lListener = new LogoutListener(null);
Your LogoutListener takes your Main-class, but you give him null. Of course you will get a NullPointerException (at least on your logoutButton-click).
Your problem in next lines :
lListener = new LogoutListener(null);
main.swapView("mm");
You need to put reference to your Main class, not null as you done. Because of your main in LogoutListener is null and you catch NPE.
Simple solution is to transfer reference of your Main to Insert with help of constructor and then transfer that to LogoutListener.

Categories