-2

How do I make the next button go to the next Frame in this GUI? I need to have it where I can click next to display 20 more details:

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class FilledFrameViewer
{
   public static void main(String[] args)
   {
      JFrame frame = new JFrame();

      /*JButton button = new JButton*/
      JButton nextButton = new JButton("NEXT");
      JLabel label = new JLabel("Frame 1.");

      JPanel panel = new JPanel();
      panel.add(nextButton);
      panel.add(label);
      frame.add(panel);

      final int FRAME_WIDTH = 300;
      final int FRAME_HEIGHT = 100;
      frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
      frame.setTitle("A frame with two components");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      frame.setVisible(true);
   }
}
Franz Kafka
  • 10,623
  • 20
  • 93
  • 149
WB4991
  • 11
  • 1
  • 1
  • 3
  • 2
    Well, you could insert some code in your question to see what is the problem. – miller Apr 10 '13 at 09:24
  • I havnt much thats why im stuck – WB4991 Apr 10 '13 at 09:26
  • , import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class FilledFrameViewer { public static void main(String[] args) { JFrame frame = new JFrame(); /*JButton button = new JButton*/ JButton nextButton = new JButton("NEXT"); JLabel label = new JLabel("Frame 1."); JPanel panel = new JPanel(); panel.add(nextButton); panel.add(label); frame.add(panel); – WB4991 Apr 10 '13 at 09:27
  • 1
    [What have you tried?](http://mattgemmell.com/2008/12/08/what-have-you-tried/) – Daanvn Apr 10 '13 at 09:27
  • final int FRAME_WIDTH = 300; final int FRAME_HEIGHT = 100; frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); frame.setTitle("A frame with two components"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } – WB4991 Apr 10 '13 at 09:27
  • 1
    Wait, insert this in a question. Not in a comment. You have edit button below a question – miller Apr 10 '13 at 09:27
  • 1
    The Use of Multiple JFrames, Good/Bad Practice? http://stackoverflow.com/a/9554657/300257 – Gilbert Le Blanc Apr 10 '13 at 09:28

4 Answers4

0

If you want to hide or show the frame, you can use like this

JFrame f1 = new JFrame("Frame 1"); JFrame f2 = new JFrame("Frame 2");

to hide f1, call f1.setVisible(false); to show f2, call f2.setVisible(true);

Annu
  • 532
  • 4
  • 8
  • 22
0

A bit unclear. Do you want to move a JButton to another JFrame? I don't think you can manage that without dynamic programming or such (I guess?).

You should look through a tutorial of the Swing components from Oracle http://docs.oracle.com/javase/tutorial/uiswing/

And see your comments. I agree having multiple JFrame is a bad habit.

Edit

Use the eventlistner (see other answer) and make it follow a switch case? Then there you can make it display like a dialog or change a JLable inside your JFrame?

Dialog tutorial here: http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html. I think you should go with a lable and change it's text.

JButton nextButton = new JButton("Next");
nextButton.addActionListener(new ActionListner(

private int counter = 0;

public void actionPerformed(ActionEvent e) { 

    counter++;

    switch(counter){

        case 1: somelable.setText("Your text here");

    };

}));

I wrote this on free hand but I guess something like this would work?

Lurvas777
  • 35
  • 6
  • No what I want is when I click next it will go to the next jframe and display info on a different person – WB4991 Apr 10 '13 at 09:43
  • you should not have many jframes! Gilbert Le Blanc already pointet out that [here](http://stackoverflow.com/a/9554657/300257)! – Lurvas777 Apr 10 '13 at 09:49
  • Yes but it's the question I have is to use a next frame. – WB4991 Apr 10 '13 at 09:50
  • Then I guess you have to custom make 20 JFrames each with a next button added a actionlistener which creates a new JFrame (the next one in line) and hides the current one showing – Lurvas777 Apr 11 '13 at 11:21
0

You have to edit the codes in your next frame... Example for your NextFrame,

public class NextFrame
   public static void main(String[] args)
   {
      private static FilledFrameViewer parentFrame; //ADD THIS FOR CONNECTION TO FIRST FRAME

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    NextFrame frame = new NextFrame(null); //CHANGES MADE HERE
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }

      public NextFrame(FilledFrameViewer f) { //CHANGES MADE HERE
      this.parentFrame = f; //CHANGES MADE HERE
        setTitle("Personal Assistant");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
NH Narumi
  • 9
  • 10
0

Ah, Yes...and also you'll have to add some things in the Next button...

Example:

 btnNext = new JButton();
    btnNext.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent e) {
            goNext();
            {
                private void goNext(){
                                    NextFrame nextframe= new NextFrame(null);
                                    nextframe.setVisible(true);
            }
            }

    });
NH Narumi
  • 9
  • 10