Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Card Layout: 2nd card is not showing text in JTextField

2993155Jul 16 2015 — edited Jul 16 2015

I have a java swing application that implements card layout and observer pattern. Basically in the first card, I key in a number in the JTextField and click "get number" button, it will run the Calculate class, which just returns the number, which then notify its observer which is the Snd class. In the 2nd card (Snd class), it's supposed to set the number passed by the Observable (Calculate class) on the JTextField. But my problem is the JTextField on the 2nd card is not showing any text. I already tried the getText method using print method and it's returning the correct value so the Observer does get updated. It's just that JTextField is not showing anything. Here's a runnable code.

CardWindow.java

import java.awt.CardLayout;

import java.awt.EventQueue;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class CardWindow {

  private JFrame f;

  private JPanel panel_2;

  private JPanel panel_1;

  private JButton btnNext, btnBack;

  Calculate watched;

  Snd watcher;

  /**

  * Launch the application.

  */

  public static void main(String[] args) {

  EventQueue.invokeLater(new Runnable() {

  public void run() {

  try {

  CardWindow window = new CardWindow();

  window.f.setVisible(true);

  } catch (Exception e) {

  e.printStackTrace();

  }

  }

  });

  }

  /**

  * Create the application.

  */

  public CardWindow() {

  initialize();

  }

  /**

  * Initialize the contents of the frame.

  */

  private void initialize() {

  f = new JFrame();

  f.setBounds(100, 100,500,300);

  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  f.getContentPane().setLayout(new CardLayout(0, 0));

  panel_1 = new Main_1();

  panel_2 = new Snd();

  f.getContentPane().add(panel_1);

  f.getContentPane().add(panel_2);

  panel_1.setVisible(true);

  panel_2.setVisible(false);

  btnNext = new JButton("Next");

  btnNext.setBounds(300, 100, 161, 29);

  panel_1.add(btnNext);

  btnNext.addActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent e) {

  panel_1.setVisible(false);

  panel_2.setVisible(true);

  System.out.println("Next card");

  }

  });

  btnBack = new JButton("Back");

  btnBack.setBounds(300, 100, 75, 29);

  panel_2.add(btnBack);

  btnBack.addActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent e) {

  panel_2.setVisible(false);

  panel_1.setVisible(true);

  System.out.println("Return previous");

  }

  });

  }

}



Main_1.java

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JPanel;

import javax.swing.JTextField;

public class Main_1 extends JPanel {

  private static final long serialVersionUID = 1L;

  private JTextField textField;

  private JButton btnCalculate;

  Calculate watched;

  public Main_1() {

  System.out.println("View1()");

setLayout(new FlowLayout());

  textField = new JTextField();

  textField.setBounds(37, 80, 134, 28);

  add(textField);

  textField.setColumns(10);

  btnCalculate = new JButton("Get number");

  btnCalculate.setBounds(47, 131, 117, 29);

  add(btnCalculate);

  btnCalculate.addActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent e) {

  watched = new Calculate();

  watched.calculate(textField.getText());

  }

  });

  }

}

Snd.java

import java.util.Observable;

import java.util.Observer;

import javax.swing.JPanel;

import javax.swing.JTextField;

import javax.swing.JLabel;

public class Snd extends JPanel implements Observer {

private static final long serialVersionUID = 1L;

  JTextField textField2;

  private Calculate cal;

  private JLabel label;

  Snd() {

  System.out.println("View2()");

setLayout(new FlowLayout());

  textField2 = new JTextField();

  textField2.setBounds(85, 121, 134, 28);

  add(textField2);

  label = new JLabel();

  label.setBounds(129, 200, 61, 16);

  add(label);

  System.out.println("Testing");

  }

  public void update(Observable obs, Object obj) {

  System.out.println ("View      : Observable is " + obs.getClass() + ", object passed is " + obj.getClass());

  cal = (Calculate) obs;

  System.out.println("Update: " + cal.getNum() + " " + obj);

  textField2.setText(Integer.toString(cal.getNum()));

  System.out.println(textField2.isDisplayable());

  System.out.println(textField2.getText());

  label.setText(Integer.toString(cal.getNum()));

  }

}

Calculate.java

import java.util.Observable;

public class Calculate extends Observable {

private int abc;

  public Calculate () {

  System.out.println("Model()");

  }

  public void setValue(int num) {

  System.out.println("Initial value: " + abc);

  this.abc = num;

  setChanged();

  System.out.println("Initial notify observer");

  notifyObservers(abc);

  }

  public void calculate(String number) {

  Snd watcher = new Snd();

  addObserver(watcher);

  System.out.println("hasChanged: " + hasChanged());

  abc = Integer.valueOf(number);

  System.out.println("Number: " + abc);

  setChanged();

  System.out.println("hasChanged: " + hasChanged());

  notifyObservers(abc);

  System.out.println("Notify observer");

  }

  public int getNum() {

  return abc;

  }

}

This post has been answered by TPD-Opitz on Jul 16 2015
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 13 2015
Added on Jul 16 2015
5 comments
1,185 views