Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: not so simple, simple swing question box

  1. #1
    Member wolfgar's Avatar
    Join Date
    Oct 2009
    Location
    the middle of the woods
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default not so simple, simple swing question box

    what i'm trying to do is create a basic question box that can be reused and already its looking imposable ( the reused part and the return info to the main program

    i have it set so that i call the class
     QBox q = new QBox();
    then on the next line instead of the 6 0ther things u'd normal use u'd just tell it to make the QBox
     q.ask("the question");
    but when i try to get the answer
    System.out.println(q.awnser);
    it come out blank . and even after it shows it doesn't show the next question
     q.ask("new question");

    heres the 2 codes i have ( 1 was mostly generated with visual swing for eclipse )

    test/main
    package userInterface;
     
    public class Test {
    	public static void main(String[] args){
    		QBox q = new QBox();
    		q.ask("Will this work?");
    		System.out.println(q.awnser);
    		q.ask("this never even runs :( ");
    	}
     
    }

    package userInterface;
     
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.UIManager;
    import org.dyno.visual.swing.layouts.Bilateral;
    import org.dyno.visual.swing.layouts.Constraints;
    import org.dyno.visual.swing.layouts.GroupLayout;
    import org.dyno.visual.swing.layouts.Leading;
    import org.dyno.visual.swing.layouts.Trailing;
     
    //VS4E -- DO NOT REMOVE THIS LINE!
    public class QBox extends JFrame {
    	private String question = "";
    	public String awnser ="";
    	private static final long serialVersionUID = 1L;
    	private JButton jButton0;
    	private JTextField jTextField0;
    	private JLabel jLabel0;
    	private static final String PREFERRED_LOOK_AND_FEEL = "com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel";
    	public QBox() {
     
    	}
     
     
    // this is where i started editing 
    	public void ask(String q) {
    		installLnF();  // ----- moved this from the main 
    		this.question = q;
    		setLayout(new GroupLayout());
    		add(getJButton0(), new Constraints(new Trailing(12, 30, 250), new Trailing(9, 64, 64)));
    		add(getJTextField0(), new Constraints(new Bilateral(12, 83, 6), new Trailing(12, 38, 38)));
    		add(getJLabel0(), new Constraints(new Leading(12, 307, 12, 12), new Leading(12, 40, 44, 44)));
    		setSize(331, 92);
    		this.setDefaultCloseOperation(QBox.EXIT_ON_CLOSE);      // ------ as well as all of these below here 
    		this.setTitle("Question");
    		this.getContentPane().setPreferredSize(this.getSize());
    		this.pack();
    		this.setLocationRelativeTo(null);
    		this.setVisible(true);
    	}
     
    	private JLabel getJLabel0() {
    		if (jLabel0 == null) {
    			jLabel0 = new JLabel();
    			jLabel0.setText(question);
    		}
    		return jLabel0;
    	}
     
    	private JTextField getJTextField0() {
    		if (jTextField0 == null) {
    			jTextField0 = new JTextField();
    		}
    		return jTextField0;
    	}
     
    	private JButton getJButton0() {
    		if (jButton0 == null) {
    			jButton0 = new JButton();
    			jButton0.setText("Send");
    			jButton0.addMouseListener(new MouseAdapter() {
     
    				public void mouseReleased(MouseEvent event) {
    					jButton0MouseMouseReleased(event);
    				}
    			});
    		}
    		return jButton0;
    	}
     
    	private static void installLnF() {
    		try {
    			String lnfClassname = PREFERRED_LOOK_AND_FEEL;
    			if (lnfClassname == null)
    				lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
    			UIManager.setLookAndFeel(lnfClassname);
    		} catch (Exception e) {
    			System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL
    					+ " on this platform:" + e.getMessage());
    		}
    	}
     
    // -------- and this should close the window and set the awnser var to what was in the text box
    	private String jButton0MouseMouseReleased(MouseEvent event) {   
    		this.setVisible(false);
    		this.removeAll();
    		return jTextField0.getText();
     
    	}
     
    }
    Last edited by wolfgar; November 16th, 2009 at 11:14 PM.
    Programming: the art that fights back


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: not so simple, simple swing question box

    The JFrame you are making is not modal, so when you call 'q.ask()' the method returns immediately without a chance to set q.answer. Ways around this would be to create a modal dialog yourself, use listeners to listen to an ActionEvent based upon the button in the window, or the easiest option: use JOptionPane.showInputDialog (see How to Make Dialogs (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components) )

  3. #3
    Member wolfgar's Avatar
    Join Date
    Oct 2009
    Location
    the middle of the woods
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: not so simple, simple swing question box

    ok .... i think i'll stay away from swing for a bit since i understood maybe 1/8 of that >.< * switches to databases *
    Programming: the art that fights back

Similar Threads

  1. Simple Form Help
    By dsen in forum Web Frameworks
    Replies: 1
    Last Post: September 8th, 2009, 11:32 PM
  2. Error of data types and type casting in java program
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: September 2nd, 2009, 10:22 AM
  3. PLEASE HELP!!!! simple java program...
    By parvez07 in forum Object Oriented Programming
    Replies: 5
    Last Post: August 26th, 2009, 06:38 AM
  4. help with simple java program
    By parvez07 in forum Java Theory & Questions
    Replies: 4
    Last Post: August 25th, 2009, 07:19 AM
  5. Simple graphics practice on previous Java code
    By amrawad_85 in forum AWT / Java Swing
    Replies: 5
    Last Post: June 19th, 2009, 10:30 AM