Results 1 to 20 of 41
- 03-05-2011, 09:36 PM #1
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.
-
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
- 03-05-2011, 09:58 PM #3
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 ...
Please help me! :eek: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); } }); } }Last edited by alibm; 03-05-2011 at 10:01 PM.
-
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.
- 03-05-2011, 10:14 PM #5
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.
-
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?
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.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.
Best of luck!
- 03-05-2011, 10:23 PM #7
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 10:28 PM.
-
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 10:44 PM. Reason: corrected GridLayout parameters
-
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.
- 03-05-2011, 11:23 PM #10
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
- 03-06-2011, 12:21 AM #11
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 !!!! :( :( :( :( :(
-
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.
- 03-06-2011, 01:28 AM #13
I'm working on now!
- 03-06-2011, 02:19 AM #14
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] } }
- 03-06-2011, 02:25 AM #15
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.....
-
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.
- 03-06-2011, 02:32 AM #17
I placed it uuuup
Java Code:private JPanel contentPane; static int nbrows; JPanel columnPanel = new JPanel();
Last edited by alibm; 03-06-2011 at 02:35 AM.
-
? Where do you show that you are declaring this variable in the class?
- 03-06-2011, 02:41 AM #19
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 ??
-
No, it's not really recursive as it's called in a different class (an inner class), but even if it were, it doesn't matter.
Similar Threads
-
Java App - Add, Delete, Reorder elements of the buttons
By alibm in forum AWT / SwingReplies: 3Last Post: 03-05-2011, 03:33 AM -
Manipulating array elements to zero using for-loops and if-statements in Java
By paulmmj in forum New To JavaReplies: 9Last Post: 02-14-2011, 04:22 AM -
how do i get elements from java beans from list?
By anthrax in forum New To JavaReplies: 5Last Post: 12-24-2009, 09:56 AM -
what does <Elements> mean
By JordashTalon in forum New To JavaReplies: 1Last Post: 01-31-2009, 10:19 PM -
Negative elements
By swikar.java in forum New To JavaReplies: 6Last Post: 12-15-2008, 04:48 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks