-1

In these codes I get information of how many Job is available (JobCount) and in the next JFrame I want to put JobCount times JToggleButtton. but the JobCount doesn't pass correctly. How can I make it so it can pass information from frames?

public class NewJFrame extends javax.swing.JFrame {

public NewJFrame() {
    initComponents();
}
  public int PeopleCount;
public int JobCount;
public  JFrame2 jf2;

private void PJCountjButtonActionPerformed(java.awt.event.ActionEvent evt) {                                               
    PeopleCount=(int)(PeopleCountjSpinner.getValue());
    JOptionPane.showMessageDialog(null, "peoplecount="+PeopleCount);
    JobCount=(int)(JobCountjSpinner.getValue());
    jf2=new JFrame2();
    jf2.setVisible(true);
    jf2.c1=this;
    this.setVisible(false);  
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new NewJFrame().setVisible(true);
        }
    });
}
public class JFrame2 extends javax.swing.JFrame {

int j;
public NewJFrame c1 = new NewJFrame();

JPanel contentpane;
JButton show;
public JButton[] jbarray = new JButton[c1.JobCount];
public int array[][] = new int[c1.PeopleCount][c1.JobCount];

public JFrame2() {
    initComponents();
    JOptionPane.showMessageDialog(null, "peoplecount="+c1.PeopleCount);
    this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
    this.setBounds(10, 01, 500, 400);
    this.contentpane = new JPanel();
    this.show = new JButton("Show array");
    contentpane.add(show);

    for (j = 0; j < c1.JobCount; j++) {
        this.jbarray[j] = new JButton("job" + (j + 1));
        contentpane.add(jbarray[j]);
    }

    this.setContentPane(contentpane);
    this.setVisible(true);
}}
    // Variables declaration - do not modify                     
private javax.swing.JSpinner JobCountjSpinner;
private javax.swing.JButton PJCountjButton;
private javax.swing.JSpinner PeopleCountjSpinner;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
// End of variables declaration          }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 4
    See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) Commonly the fix to this type of problem is to use a modal component such as a `JDialog` or a `JOptionPane`.. – Andrew Thompson Jan 21 '14 at 09:58
  • 2
    _"How can I make it so it can pass information from frames?"_ use a setter or pass the value through a constructor – Paul Samsotha Jan 21 '14 at 10:05

3 Answers3

0

Don't use Multible JFrame beccause its a bad practice, you can use one JFrame and many JDialog like :

private void PJCountjButtonActionPerformed(java.awt.event.ActionEvent evt) {                                               
PeopleCount=(int)(PeopleCountjSpinner.getValue());
JOptionPane.showMessageDialog(null, "peoplecount="+PeopleCount);
JobCount=(int)(JobCountjSpinner.getValue());
JDialog dialog = new CustomConstructor(params.) // here you can creaea your own constructor and pass the needed info.
dialog.setVisible(true);
}
Salah
  • 8,567
  • 3
  • 26
  • 43
0

What peeskillet said is correct.

private int jobCount;

public int getJobCount() {
    return jobCount;
}

public void setJobCount(int jobCount) {
    this.jobCount = jobCount;
}

Followed by:

public class JFrame2() {
    int passedJobCount = c1.getJobCount();
}

That is how you use private variables correctly. For further reading, check out Oracle's Tutorial on Member Declarations.

Gorbles
  • 1,169
  • 12
  • 27
0

Besides i think, sharing values between frames is not a good practice, i would suggest if really needed that way, to just store the jobCount as an Integer Object and store its reference where needed. That way it is always up to date.

If i understood your intention right, that you just wanted to make a control panel of buttons for each job and person, you can do this all in one view. Just take frame1 to get the count via the slider, then dynamically add the buttons for slider.value .you could use default borderlayout and put slider north and buttons to the center.

This small ssce should help you to get on track:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class NewJFrame extends javax.swing.JFrame {

private static final long serialVersionUID = 1L;
private JSlider slider;
private JButton btnNewButton;
private JDialog d;
private JTextField text;

public NewJFrame() {

    btnNewButton = new JButton("Do");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            d.setVisible(true);
        }
    });
    getContentPane().add(btnNewButton, BorderLayout.CENTER);

    slider = new JSlider();
    slider.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            text.setText(Integer.toString(slider.getValue()));
        }
    });

    getContentPane().add(slider, BorderLayout.NORTH);

    d = new JDialog();
    text = new JTextField();
    d.getContentPane().add(text);
    d.pack();

    pack();
    setVisible(true);
}

public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new NewJFrame().setVisible(true);
        }
    });
}
}
gantners
  • 471
  • 4
  • 16