Results 1 to 18 of 18

Thread: [RESOLVED] Java question

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2005
    Posts
    355

    Resolved [RESOLVED] Java question

    Hi, is there any website have a lots of sample code and eg. to download. I'm quite new to JAVA cos no sticky threads for this.

    & also how can I do a simple buttongroup with captions?

    TIA
    Last edited by abcat; Jul 23rd, 2007 at 11:46 PM.

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Java question

    Quote Originally Posted by abcat
    Hi, is there any website have a lots of sample code and eg. to download. I'm quite new to JAVA cos no sticky threads for this.

    & also how can I do a simple buttongroup with captions?

    TIA
    This is Java not VB6 (i.e. Object oriented) You can create an Array of JButton objects and give each a text (using setText method), maybe use a loop for that
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2005
    Posts
    355

    Re: Java question

    Hi, Thanks for replying So u mean no place eg. Tutorials to download and learn Java easily with some samples. I remember I go Java website and download all the 4 packs which are quite normal.. I wonder is there a better 1?

    Anyway for buttongroup caption I think I use TitledBorder.

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Java question

    I remember I go Java website and download all the 4 packs which are quite normal.. I wonder is there a better 1?
    What 4 packs are you talking about??
    There are no packs in java. there are 3 main technologies (JSE, JEE, JME)
    if you want some tutorials, you can visit the java.sun.com. They have some useful tutorials and how to start articles there
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2005
    Posts
    355

    Re: Java question

    I think I d.l this 4 http://java.sun.com/docs/books/tutor.../download.html . Haven't really read it. I wonder is there more sample coding with comments to download?

  6. #6
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Re: Java question

    Quote Originally Posted by ComputerJy
    if you want some tutorials, you can visit the java.sun.com. They have some useful tutorials and how to start articles there
    May be this is the best place in Java world.

    Check more on about.com site.
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

  7. #7
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Java question

    Well, I rarely look at other people's code unless they need help
    But I've visited Planet source code and I think it's a good place.

    P.S. Read the articles in the main page they are actually useful most of the time
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2005
    Posts
    355

    Re: Java question

    Thanks for all the website I think its gonna be a good reference for me Btw, I encountered 1 simple problem while coding my programs.

    Its about JTextField, by default for my program, the JTextField had a default text to call user to input text. How can I validate that the JTextField had been altered with a new string and then prompt for an error message if the text remain the same? I try using boolean expression like if txtField.getText() == txtField.getText() and also txtField.getText() == "default text I have put" but it doesn't seems to work.

    Is JTextField cannot use ActionListener for events?

    I wanna make it like when user once click over the JTextField, the default text will be gone and then user can start to type and my program will validate it if its blank string then error msg prompted..

    Thanks

  9. #9
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Java question

    I think the FocusListener should do it for you:
    Code:
    	txtField.addFocusListener(new FocusListener(){
    
    	    @Override
    	    public void focusGained(FocusEvent e) {
    		
    	    }
    
    	    @Override
    	    public void focusLost(FocusEvent e) {
    		
    	    }
    	    
    	});
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  10. #10
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Java question

    To compare strings use:
    Code:
    String str="This is a string";
    if(str.equals("This is a string"));
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2005
    Posts
    355

    Re: Java question

    Hi ComputerJY, thanks again for your help The equal works well as previously I using == and it doesn't seems to work, don't know why.

    Regarding the FocusListener, how can I use it? I think I use it the wrong way resulting in program startup, textfield straight away change instead of mouse over and click it.

    Code:
    public class TextField implements FocusListener
        {
            public void focusGained(FocusEvent e) {
               if (e.getSource() == txtField) {
                     txtField.setText("");
                  }
      
                  
                  public void focusLost(FocusEvent e) {
                     //Try on putting here also wrong, don't really know how it works though :(
                   
                               }
    
                  }
    
        }

  12. #12
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Java question

    The equal works well as previously I using == and it doesn't seems to work, don't know why.
    The "==" compares Object references (something like a pointers) and they'll be equal only if it was the exact same object.

    Regarding the FocusListener, how can I use it? I think I use it the wrong way resulting in program startup, textfield straight away change instead of mouse over and click it.
    It might be that the form has nothing but the JTextField or maybe it's the number 0 in the tab index map so it gains focus on startup
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2005
    Posts
    355

    Re: Java question

    Thanks for the explanation but somehow I'm still not understand on the FocusListener. I can't seems to make it work like those ActionListener. Actually I just want to make it like those website with forms with textbox with default text, and user click the textbox, the default text will be gone? Any idea?

  14. #14
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Java question

    Check this out and tell me if it's what you are looking for:
    Code:
    import java.awt.BorderLayout;
    import java.awt.HeadlessException;
    import java.awt.event.FocusEvent;
    import java.awt.event.FocusListener;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    
    public class TestFrame extends JFrame {
    
        private static final long serialVersionUID = -7356866315584500438L;
    
        /**
         * @throws HeadlessException
         */
        public TestFrame() throws HeadlessException {
    	super("Test Project");
    
    	JTextField normalTextField = new JTextField();
    	normalTextField.setText("This text is here to stay");
    	this.add(normalTextField,BorderLayout.NORTH);
    	
    	final JTextField textLostOnFocus = new JTextField();
    	textLostOnFocus.setText("This text will be lost on focus");
    	
    	textLostOnFocus.addFocusListener(new FocusListener(){
    
    	    @Override
    	    public void focusGained(FocusEvent e) {
    		textLostOnFocus.setText("");
    	    }
    
    	    @Override
    	    public void focusLost(FocusEvent e) {
    	    }
    	});
    	this.add(textLostOnFocus,BorderLayout.SOUTH);
        }
    
        /**
         * @param args
         */
        public static void main(String[] args) {
    	TestFrame frm = new TestFrame();
    	frm.setSize(400, 400);
    	frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frm.setVisible(true);
        }
    
    }
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2005
    Posts
    355

    Re: Java question

    Hi, thx for your effort again. Thats what I want I think i might need to take sometime for me to understand and migrate ur code to my program.

    But somehow I tested ur code again and find out that if I remove 1 textfield, the other 1 won't work >
    Code:
    textLostOnFocus.setText("This text will be lost on focus");
    Wonder is this the case that need to textfield? Can I have one instead? As previously I've tested with the code u give me earlier on also have the same problem with 1 textfield.
    Last edited by abcat; Jul 23rd, 2007 at 09:23 AM.

  16. #16
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Java question

    I told you, if the Frame has only 1 component. this component will be selected by default. so you gotta have at least 2 components on the Fram to have an inactive one
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2005
    Posts
    355

    Resolved Re: Java question

    Quote Originally Posted by ComputerJy
    I told you, if the Frame has only 1 component. this component will be selected by default. so you gotta have at least 2 components on the Fram to have an inactive one
    Oic, thanks alot, you've been a great help

  18. #18
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Java question

    You are very welcome
    Please don't forget to mark the thread resolved from the "Thread Tools" menu
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width