Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By JosAH

Thread: Need Some Algorithm

  1. #1
    incredibleX is offline Member
    Join Date
    Nov 2013
    Posts
    30
    Rep Power
    0

    Question Need Some Algorithm

    I want some ideas on how to add a new JButton in JFrame every time a button is clicked.

    In simple words when a button is pressed in JFrame an other button with some specific location is created automatically.

    Sounds a bit weird to me. but How?

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,049
    Rep Power
    6

    Default Re: Need Some Algorithm

    You write the code to do so. Which would mean:

    - getting your requirements straight in stead of calling them "weird"; knowing the graphical layout is very important when working on a GUI.
    - learning how to actually use the API. Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)

    Good luck getting to the point where you can at least make a start yourself, it is going to take some time and effort.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    incredibleX is offline Member
    Join Date
    Nov 2013
    Posts
    30
    Rep Power
    0

    Default Re: Need Some Algorithm

    i know how to build a swing application.
    All the stuff you have provided is not telling me what to do?
    What i want from you is an algorithm or an idea which can help me understand how is it possible to create a button like this and how will i use the functionality of each button created.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,049
    Rep Power
    6

    Default Re: Need Some Algorithm

    I know what you want, but so far you show nothing that indicates that you made some effort into this problem yourself first. I'm not going to do the thinking for you when you have a perfectly functional brain yourself. So what have YOU come up with so far that is not complete enough to go ahead and implement this thing?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    incredibleX is offline Member
    Join Date
    Nov 2013
    Posts
    30
    Rep Power
    0

    Default Re: Need Some Algorithm

    show me some way for this purpose.
    i'm trying creating this stuff using simple javax.swing library but it isn't working the way i want it to.
    Definitely i'm putting effort on it.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: Need Some Algorithm

    Quote Originally Posted by incredibleX View Post
    show me some way for this purpose.
    i'm trying creating this stuff using simple javax.swing library but it isn't working the way i want it to.
    Definitely i'm putting effort on it.
    So you want us to show you working code and then what? Are you going to scrape it, copy it, run it, and that's it? We are no code mill; you have to show us your efforts and tell us what's failing and then we'll try to help you with your code.

    kind regards,

    Jos
    gimbal2 likes this.
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    incredibleX is offline Member
    Join Date
    Nov 2013
    Posts
    30
    Rep Power
    0

    Default Re: Need Some Algorithm

    no i don't want you to give me the code.
    humph.. tell me is it possible with javax.swing or i should study something else for this?

  8. #8
    incredibleX is offline Member
    Join Date
    Nov 2013
    Posts
    30
    Rep Power
    0

    Default Re: Need Some Algorithm

    Quote Originally Posted by JosAH View Post
    So you want us to show you working code and then what? Are you going to scrape it, copy it, run it, and that's it? We are no code mill; you have to show us your efforts and tell us what's failing and then we'll try to help you with your code.

    kind regards,

    Jos
    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    
    
    public class Test extends JFrame {
    	
    	private JButton b= new JButton();
    	private JButton c= new JButton();
    	private JTextField f= new JTextField();
    	int i;
    	Timer t;
    	
    	void createButton(){
    		b= new JButton();
    		b.setSize(b.getPreferredSize());
    		b.setLocation(0,0);
    		
    		add(b);
    	}
    
    	Test(){
    		
    		setSize(500,500);
    		setLocationRelativeTo(null);
    		setResizable(false);
    		setLayout(null);
    
    		f.setSize(100,20);
    		f.setLocation(100, 5);
    		
    		c= new JButton();
    		c.setSize(b.getPreferredSize());
    		c.setLocation(20,20);
    		
    		
    		
    		add(f);
    		add(c);
    		
    		setVisible(true);
    		
    		ActionListener action = new ActionListener() {
    			
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				// TODO Auto-generated method stub
    				i=Integer.parseInt(f.getText());
    				if(e.getSource()==c){
    					if(i==1){
    						createButton();
    					}
    				}
    			}
    		};
    		
    		t= new Timer(100,action);
    		
    	}
    	public static void main(String args[]) {
        	new Test();
        }
    }
    here is a code i'm working on... But it isn't working.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: Need Some Algorithm

    Adding or removing components to/from a container (such as the ContentPane of a JFrame) after the container is set to visible, needs to revalidate the container. Read the API documentation for the relevant Swing classes.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    Join Date
    Sep 2013
    Posts
    21
    Rep Power
    0

Similar Threads

  1. Mph to Kph Algorithm
    By jbing109 in forum New To Java
    Replies: 4
    Last Post: 01-21-2013, 08:26 AM
  2. Algorithm help? :)
    By Mirix in forum New To Java
    Replies: 6
    Last Post: 05-24-2010, 02:08 AM
  3. Need some help in an algorithm
    By ea09530 in forum New To Java
    Replies: 3
    Last Post: 04-04-2010, 01:13 PM
  4. Help with an Algorithm
    By Manfizy in forum New To Java
    Replies: 22
    Last Post: 07-03-2009, 07:16 AM
  5. O(log n) algorithm help !!!!!!
    By itseeker87 in forum New To Java
    Replies: 8
    Last Post: 09-09-2008, 05:12 PM

Posting Permissions

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