Page 1 of 3 123 LastLast
Results 1 to 20 of 41
  1. #1
    alibm's Avatar
    alibm is offline Member
    Join Date
    Mar 2011
    Posts
    28
    Rep Power
    0

    Default Can I add elements to a Java GUI?

    I want to know how I can make a Java program where an unknown amount of objects can be added to a GUI depending on user input. I can program in objects one at a time within the program, but I haven't seen a more dynamic program.

    Can I do that with Java? If not, what can I do it with?

    For more information, here's a picture.




    There can be more than one block of buttons per block.


  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Yes, absolutely this can be done in Java with the Swing GUI library. For more on how to program with Swing, please check out the tutorials: Using Swing Components

  3. #3
    alibm's Avatar
    alibm is offline Member
    Join Date
    Mar 2011
    Posts
    28
    Rep Power
    0

    Default

    Here's my code.

    There's nothing when I execute.

    I declared a parent JPanel that will conatin son JPanels .
    son JPanel contains an add button.
    with add button, I can add a son JPanel in the parent Jpanel ...


    Java Code:
    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import java.awt.GridLayout;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import javax.swing.JButton;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.Color;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import java.awt.Font;
    
    
    public class DynamicGui extends JFrame {
    
    	private JPanel contentPane;
    
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					DynamicGui frame = new DynamicGui();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
    
    	/**
    	 * Create the frame.
    	 */
    	public DynamicGui() {
    		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);
    		
    		 
    		
    		////////////////////////////////////////////////
    		
    		
    		
    		////////////////////////////////////////////////
    		final JButton btnAdd = new JButton("Add");
    		btnAdd.setFont(new Font("Tahoma", Font.PLAIN, 9));
    		btnAdd.setBounds(80, 11, 59, 23);
    		
    		btnAdd.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				JPanel parent = new JPanel();
    				parent.setBackground(Color.GRAY);
    				parent.setBounds(10, 11, 171, 240);
    				contentPane.add(parent);
    				parent.setLayout(new GridLayout(0,1));
    				
    				JPanel son = new JPanel();
    			        son.setBounds(2, 2, 171, 59);
    				contentPane.add(son);
    				son.setBackground(Color.RED);
    				son.setLayout(null);
    				
    				son.add(btnAdd);
    				
    				parent.add(son);
    				 
    			}
    		});
    		
    		 
    		
    	}
    }
    Please help me! :eek:
    Last edited by alibm; 03-05-2011 at 11:01 PM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You never add anything to a container that is eventually held by a top level window. For instance, where do you add your JButton to the GUI?

    You will want to go through the Swing tutorials, in particular the sections on adding components to containers, and the section on the layout managers because you definitely don't want to use null layouts for your application, especially if you want to add components dynamically.

  5. #5
    alibm's Avatar
    alibm is offline Member
    Join Date
    Mar 2011
    Posts
    28
    Rep Power
    0

    Default

    The Add button is added in son Jpanel.
    When I click on the add button, a new son jpanel will be created and added in just under the last added one.
    Please look at the picture up.
    I used to code Java but I'm testing this dynamic GUI for the first time.
    I have a project and I have to progress in quickly.

    Thanks in advance.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by alibm View Post
    The Add button is added in son Jpanel.
    When I click on the add button, a new son jpanel will be created and added in just under the last added one.
    Yes the JButton is added to the son JPanel, but only in the ActionListener's code, which is never called because no JButton has been added yet. Do you see how this will never work?


    Please look at the picture up.
    I used to code Java but I'm testing this dynamic GUI for the first time.
    I have a project and I have to progress in quickly.
    Your progress will depend on how quickly and well you can learn Swing coding. Again, you must study from the tutorials if you hope to progress.

    Best of luck!

  7. #7
    alibm's Avatar
    alibm is offline Member
    Join Date
    Mar 2011
    Posts
    28
    Rep Power
    0

    Default

    I thought that I must declare the parent JPanel first then the Add button and in the PerformedAction:
    I will create a new son Jpanel, add the Add button to the new son Jpanel, and the last one son Jpanel to the parent Jpanel...

    Now I get the parent Jpanel visible but the first son jpanel is not present.
    So I can't add ...

    Here's the code.

    Java Code:
    ////////////////////////////////////////////////
    		final JPanel parent = new JPanel();
    				parent.setBackground(Color.GRAY);
    				parent.setBounds(10, 11, 171, 240);
    				contentPane.add(parent);
    				parent.setLayout(new GridLayout(0,1));
    		
    		
    		////////////////////////////////////////////////
    		final JButton btnAdd = new JButton("Add");
    		btnAdd.setFont(new Font("Tahoma", Font.PLAIN, 9));
    		btnAdd.setBounds(80, 11, 59, 23);
    		
    		btnAdd.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				
    				
    				JPanel son = new JPanel();
    			     
    				 
    				son.setBackground(Color.RED);
    				son.setLayout(null);
    				
    				son.add(btnAdd);
    				
    				parent.add(son);
    				 
    			}
    		});
    Last edited by alibm; 03-05-2011 at 11:28 PM.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Again, you need to learn how to use layouts because your project is going to need to make heavy use of them. If you can't use a JTable or a JList, then you'll need to stack JPanels one on top of the other, held in another JPanel that uses GridLayout set for 1 column and a variable number of columns (new GridLayout(0, 1, 0, vgap) -- where vgap is the vertical distance between components)), and this will likely be held in a JScrollPane that is held in another JPanel, perhaps one that uses BorderLayout. The layout tutorials start here: Layout Tutorials
    Last edited by Fubarable; 03-05-2011 at 11:44 PM. Reason: corrected GridLayout parameters

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    For example, this one uses the layouts mentioned above. To run the code, extract the jar file from the zip file and double click on it.
    Attached Files Attached Files

  10. #10
    alibm's Avatar
    alibm is offline Member
    Join Date
    Mar 2011
    Posts
    28
    Rep Power
    0

    Default

    wawwwwwwwwww
    You are the best.
    But When I click on add button, the new son jpanel will be added just after not in the end of list.

    Please please, post the code. :rolleyes: :) :D

  11. #11
    alibm's Avatar
    alibm is offline Member
    Join Date
    Mar 2011
    Posts
    28
    Rep Power
    0

    Unhappy

    Hello,

    Dear Fubarable,

    Please post the code. I will die here now.
    Why you do like this for me?? :confused: :confused: :confused: :confused:

    Please the code !!!! :( :( :( :( :(

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I won't post the code because this is your academic project, and you need to create it yourself. I will tell you what I did though if you're interested.

    Create a JPanel called columnPanel that uses GridLayout(0, 1, 0, 5).
    Add this in the BorderLayout.NORTH postion to another JPanel called borderLayoutPanel that uses a BorderLayout.
    Add this to a JScrollPane

    Call a method called addRow.

    In addRow I create a JPanel called rowPanel that uses a BoxLayout and has components added to it including the JButton.
    add the addRow JPanel to the columnPanel above and then call revalidate on columnPanel so it will re-layout the JPanels it holds.
    Give the JButton an ActionListener that simply calls the addRow method.

    That's it.

    Oh, nice try getting someone else to write this for you on stackoverflow.com. Please don't do that again.

  13. #13
    alibm's Avatar
    alibm is offline Member
    Join Date
    Mar 2011
    Posts
    28
    Rep Power
    0

    Default

    I'm working on now!

  14. #14
    alibm's Avatar
    alibm is offline Member
    Join Date
    Mar 2011
    Posts
    28
    Rep Power
    0

    Default

    Hello,
    The last sentence affected on me, influenced positively ....
    Thanks for making my mind come back...
    Wasting the time in learning is better then posting in other forums without nothing......

    Pleas look at this code.
    I get close but columnPanel ........

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.EventQueue;
    
    import javax.swing.BorderFactory;
    import javax.swing.Box;
    import javax.swing.BoxLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import java.awt.Color;
    import java.awt.GridLayout;
    import javax.swing.JScrollPane;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    
    
    public class Test_DG extends JFrame {
    
    	private JPanel contentPane;
    	static int nbrows;
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		nbrows=0;
    		Test_DG frame = new Test_DG();
    		frame.setVisible(true);
    	}
    
    	/**
    	 * Create the frame.
    	 */
    	public Test_DG() {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 379, 362);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(contentPane);
    		contentPane.setLayout(null);
    		
    		[COLOR="Red"]JPanel columnPanel = new JPanel();[/COLOR]
                     //////////////// ISNT RIGHT???????
    		columnPanel.setBackground(Color.ORANGE);
    		columnPanel.setBounds(10, 11, 210, 302);
    		contentPane.add(columnPanel);
    		columnPanel.setLayout(new GridLayout(0, 1, 0, 5));
    		
    		JScrollPane scrollPane = new JScrollPane();
    		columnPanel.add(scrollPane);
    		
    	}
    	
    	  void addRow() {
    		  
    		    JPanel rowPanel = new JPanel();
    		    rowPanel.setLayout(null);
    		    
    		    JLabel lbl = new JLabel("");
    		    lbl.setText("N"+nbrows);
    		    ///////////////////
    		    nbrows++;
    		    ////////////////////
    		    lbl.setBounds(10, 18, 77, 14);
    		    rowPanel.add(lbl);
    
    	    
    		    JButton btnAdd = new JButton("Add");
    			btnAdd.setBounds(124, 14, 66, 23);
    			rowPanel.add(btnAdd);
    			btnAdd.addActionListener(new ActionListener() {
    				public void actionPerformed(ActionEvent arg0) {
    					addRow(); //////// ISNT RECURSION HERE ??
    				}
    			});
    			 
    		    rowPanel.add(btnAdd);
    		   [B] [COLOR="Red"]/////////////columnPanel is not visible
    		    ///////It wouldn't add the rowPanel
    		     ////////////////////////////////:
    			columnPanel.add(rowPanel);
    		    columnPanel.validate();
    		    columnPanel.paint();
    		    ///////////////////////////////////[/COLOR]/[/B]
    		    
    		  }
    }

  15. #15
    alibm's Avatar
    alibm is offline Member
    Join Date
    Mar 2011
    Posts
    28
    Rep Power
    0

    Default

    Next stage I will change the code to add rowPanel just after the clicked button of rowPanel.
    Like in MS Excel, when I add a new cell, it will be after, not in the end.....

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    The variable that is declared within the class constructor is only visible within the constructor (same for a method or any block). To have a variable visible throughout the class, it must be declared on the class level. It can be initialized within the constructor if desired, but not declared there.

  17. #17
    alibm's Avatar
    alibm is offline Member
    Join Date
    Mar 2011
    Posts
    28
    Rep Power
    0

    Default

    I placed it uuuup

    Java Code:
    private JPanel contentPane;
    	static int nbrows;
    	JPanel columnPanel = new JPanel();
    Last edited by alibm; 03-06-2011 at 03:35 AM.

  18. #18
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  19. #19
    alibm's Avatar
    alibm is offline Member
    Join Date
    Mar 2011
    Posts
    28
    Rep Power
    0

    Default

    I didn't understand your question,
    I placed the declaration of columnPanel up, in class level.
    There's no error now.
    but the columnPanel still empty.

    I think it's addRow() because it is invoked in btnAdd actionPerformed, so there's a recursion here ... am not right ??

  20. #20
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Page 1 of 3 123 LastLast

Similar Threads

  1. Replies: 3
    Last Post: 03-05-2011, 04:33 AM
  2. Replies: 9
    Last Post: 02-14-2011, 05:22 AM
  3. how do i get elements from java beans from list?
    By anthrax in forum New To Java
    Replies: 5
    Last Post: 12-24-2009, 10:56 AM
  4. what does <Elements> mean
    By JordashTalon in forum New To Java
    Replies: 1
    Last Post: 01-31-2009, 11:19 PM
  5. Negative elements
    By swikar.java in forum New To Java
    Replies: 6
    Last Post: 12-15-2008, 05:48 AM

Tags for this Thread

Posting Permissions

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