Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 132,646 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,109 people online right now. Registration is fast and FREE... Join Now!




got gui. now how to loop it?

 
Reply to this topicStart new topic

got gui. now how to loop it?

rwilliamj
post 15 Aug, 2008 - 04:14 PM
Post #1


New D.I.C Head

*
Joined: 14 Aug, 2008
Posts: 15

hello.

i, a neophyte, have completed a gui using netbeans. how do i apply loops (i.e. while statements) within, or to the entire gui? i am trying to program it to go through fifty cycles, then print the elapsed time.the interface wont accept any application of while statements that encompass what i see as the critical parts of code. here is the source. thank you.

CODE

import java.util.InputMismatchException;
import java.util.Random;
import java.util.Scanner;



        
public class CelsiusConverterGUI extends javax.swing.JFrame {
    
   long start = System.currentTimeMillis();
  
   int problemCount = 1;

      
   int rndma = new Random().nextInt(12) + 1;
   int rndmb = new Random().nextInt(12) + 1;
    
  
  
  
  
    
    public CelsiusConverterGUI() {
        initComponents();
        problemField.setText(" " + rndma + "x" + rndmb);
        answerField.requestFocusInWindow();
    }
    
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        convertButton = new javax.swing.JButton();
        fahrenheitLabel = new javax.swing.JLabel();
        equalSign = new javax.swing.JLabel();
        problemField = new javax.swing.JTextField();
        answerField = new javax.swing.JFormattedTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Converter");

        convertButton.setText("Submit");
        convertButton.setBorder(null);
        convertButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                convertButtonActionPerformed(evt);
            }
        });

        equalSign.setText("=");

        problemField.setEditable(false);
        problemField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                problemFieldActionPerformed(evt);
            }
        });

        answerField.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        answerField.setAutoscrolls(false);
        answerField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                ENTER(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(fahrenheitLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addComponent(problemField, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(equalSign)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(answerField, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(54, 54, 54)
                        .addComponent(convertButton)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {answerField, convertButton, problemField});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(problemField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(equalSign)
                    .addComponent(answerField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(convertButton)
                .addGap(15, 15, 15)
                .addComponent(fahrenheitLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {answerField, convertButton});

        pack();
    }// </editor-fold>



private void problemFieldActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void convertButtonActionPerformed(java.awt.event.ActionEvent evt) {
//Parse degrees Celsius as a double and convert to Fahrenheit.
  
       // TODO add your handling code here:
}

private void ENTER(java.awt.event.KeyEvent evt) {
int answerFieldinterger2 = (int)((Double.parseDouble(answerField.getText())));
int solution = rndma * rndmb;
boolean correctAns2 = solution == answerFieldinterger2;
    
      
        
    if(correctAns2){fahrenheitLabel.setText("Correct!");
                        answerField.setText("");
                        problemCount ++;
       } else { long elapsed = System.currentTimeMillis() - start;
                fahrenheitLabel.setText(" " + elapsed/1000f + "seconds");}
}

    /**
    * @param args the command line arguments
    */
  
public static void main(String args[]) {
      
    java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CelsiusConverterGUI().setVisible(true);
            }
        });
        
        
    }

    // Variables declaration - do not modify
    private javax.swing.JFormattedTextField answerField;
    private javax.swing.JButton convertButton;
    private javax.swing.JLabel equalSign;
    private javax.swing.JLabel fahrenheitLabel;
    private javax.swing.JTextField problemField;
    // End of variables declaration

}
User is offlineProfile CardPM

Go to the top of the page

thenovices
post 16 Aug, 2008 - 06:31 PM
Post #2


D.I.C Head

**
Joined: 18 Jan, 2008
Posts: 73



Thanked 7 times
My Contributions


what do you mean by "loop the GUI"? What are you trying to accomplish?

This post has been edited by thenovices: 16 Aug, 2008 - 06:33 PM
User is offlineProfile CardPM

Go to the top of the page

pbl
post 16 Aug, 2008 - 06:52 PM
Post #3


D.I.C Lover

Group Icon
Joined: 6 Mar, 2008
Posts: 2,982



Thanked 190 times

Dream Kudos: 75
My Contributions


QUOTE(rwilliamj @ 15 Aug, 2008 - 05:14 PM) *

hello.

i, a neophyte, have completed a gui using netbeans. how do i apply loops (i.e. while statements) within, or to the entire gui? i am trying to program it to go through fifty cycles, then print the elapsed time.the interface wont accept any application of while statements that encompass what i see as the critical parts of code. here is the source. thank you.



fifty cycles of what ?
User is offlineProfile CardPM

Go to the top of the page

rwilliamj
post 17 Aug, 2008 - 10:30 AM
Post #4


New D.I.C Head

*
Joined: 14 Aug, 2008
Posts: 15

i want to give the student 50 questions--something that would be like

int count = 1
while count <51 {blah blah gui}
count ++


textFeld.setText ("all done")
textFild.setText (elapsed time + "")
User is offlineProfile CardPM

Go to the top of the page

pbl
post 17 Aug, 2008 - 12:06 PM
Post #5


D.I.C Lover

Group Icon
Joined: 6 Mar, 2008
Posts: 2,982



Thanked 190 times

Dream Kudos: 75
My Contributions


You don't loop though the GUI
You have the GUI to react to and event (like a button push)

Here a quick example

CODE

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

import javax.swing.*;


public class SimpleQuestionAnswer extends JFrame implements ActionListener {

    String[] question = {"Question 1", "Question 2", "Question 3"};
    JLabel qLabel;
    JTextField answer;
    JButton ok;
    int count = 0;
    
    SimpleQuestionAnswer() {
        super("FirstGui");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        
        JPanel p = new JPanel(new GridLayout(1,2));
        p.add(new JLabel(" The question is: "));
        qLabel = new JLabel(question[count]);
        p.add(qLabel);
        add(p, BorderLayout.NORTH);
        
        p = new JPanel(new GridLayout(1,2));
        p.add(new JLabel("Enter your answer here: "));
        answer = new JTextField("");
        p.add(answer);
        add(p, BorderLayout.CENTER);
        
        ok = new JButton("OK");
        ok.addActionListener(this);
        add(ok, BorderLayout.SOUTH);
        
        setSize(300, 100);
        setVisible(true);
        
    }

    // invoked when the user click the button
    public void actionPerformed(ActionEvent arg0) {
        // check the answer
        // ....
        
        count++;
        if(count >= question.length) {
            qLabel.setText(" you're done");
            ok.setEnabled(false);
            return;
        }
        qLabel.setText(question[count]);
        answer.setText("");
    }
    
    
    public static void main(String[] arg) {
        new SimpleQuestionAnswer();
    }
    
}
User is offlineProfile CardPM

Go to the top of the page

rwilliamj
post 20 Aug, 2008 - 04:55 PM
Post #6


New D.I.C Head

*
Joined: 14 Aug, 2008
Posts: 15

thank you so much!!
arrays were the way to go.

User is offlineProfile CardPM

Go to the top of the page

pbl
post 20 Aug, 2008 - 06:15 PM
Post #7


D.I.C Lover

Group Icon
Joined: 6 Mar, 2008
Posts: 2,982



Thanked 190 times

Dream Kudos: 75
My Contributions


QUOTE(rwilliamj @ 20 Aug, 2008 - 05:55 PM) *

thank you so much!!
arrays were the way to go.

Array or not... the important point is:
You don't drive a GUI !
GUI are event driven
the User must make your GUI react
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 04:44AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month