Results 21 to 40 of 41
- 03-06-2011, 02:58 AM #21
- 03-06-2011, 03:31 AM #22
It's the scrooooooollpane
I changed the scrollpane and I added a new button, out of columnPanel and invoking addRow() the code finally worked.

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; JPanel columnPanel = new JPanel(); /** * 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, 532, 362); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); [COLOR="Red"]JScrollPane scrollPane = new JScrollPane(); scrollPane.setBounds(10, 11, 210, 302); contentPane.add(scrollPane); scrollPane.setViewportView(columnPanel);[/COLOR] columnPanel.setBackground(Color.ORANGE); columnPanel.setLayout(new GridLayout(0, 1, 0, 5)); [COLOR="RoyalBlue"]JButton button = new JButton("New button"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { addRow(); } }); button.setBounds(288, 44, 89, 23); contentPane.add(button);[/COLOR] } 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); columnPanel.add(rowPanel); columnPanel.validate(); columnPanel.repaint(); } }
- 03-06-2011, 03:57 AM #23
The next level is create something like this in the picture:

And after, finishing by editing informations for each row like this picture.

I found these:
myPanel.add(Elt);
myPanel.add(Elt,index);
myPanel.getComponent(index);
myPanel.remove(Elt);
myPanel.setComponentZOrder(Elt,index);
What's index?
is it a variable to put the value in??
I'll try it in the example now ....
- 03-06-2011, 04:04 AM #24
I added this code to use for reordernig rows:
I added it in addRow()
The add button always print 0....Java Code:int index = 0; rowPanel.getComponent(index); System.out.println(index);
why?????????
- 03-06-2011, 01:11 PM #25
Hello,
I changed the code.
This is the ouput:

add button but itsn't woks like I want.
It adds rowPanel in columnPanel. For example when I click add buttton on row N°0 60, the next rowPanel will be in the end of list not just after.
I want the new rowPanel appear between N°0 60 and N°0 13 .
I added a random number with number of row to differentiate between rowPanels.
Someone have an idea.
I tryed to print the index of rowpanel using getComponent() but it always gives 0.
Here's my full code:Java Code:int index = 0; rowPanel.getComponent(index);
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; import javax.swing.JTextField; import javax.swing.JTextArea; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.Random; public class Test_DG extends JFrame { private JPanel contentPane; static int nbrows; JPanel columnPanel = new JPanel(); JButton btnStart = new JButton("Start"); /** * 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, 637, 441); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); btnStart.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { addRow(); } }); btnStart.setBounds(349, 21, 78, 23); contentPane.add(btnStart); columnPanel.setBounds(29, 24, 310, 368); contentPane.add(columnPanel); columnPanel.setBackground(Color.ORANGE); columnPanel.setLayout(new GridLayout(13, 1, 0, 2)); } void addRow() { JPanel rowPanel = new JPanel(); rowPanel.setLayout(null); JButton lbl = new JButton(""); Random randGen = new Random(); lbl.setText("N°"+nbrows +" "+randGen.nextInt(8) +randGen.nextInt(8)); /////////////////// nbrows++; //////////////////// lbl.setBounds(0, 3, 95, 18); rowPanel.add(lbl); ////////////////////////////////// JButton btnUp = new JButton("^"); btnUp.setBounds(95, 3, 45, 18); rowPanel.add(btnUp); JButton btnDown = new JButton("V"); btnDown.setBounds(139, 3, 45, 18); rowPanel.add(btnDown); JButton btnDel = new JButton("Del"); btnDel.setBounds(185, 3, 55, 18); rowPanel.add(btnDel); /////////////////////////////////////// JButton btnAdd = new JButton("Add"); btnAdd.setBounds(241, 3, 65, 18); rowPanel.add(btnAdd); int index = 0; rowPanel.getComponent(index); System.out.println(index); btnAdd.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { addRow(); //////// ISNT RECURSION HERE ?? } }); //int index = 0; rowPanel.getComponent(index); System.out.println(index); btnDel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { //columnPanel.remove(this.rowPanel); } }); columnPanel.add(rowPanel); columnPanel.validate(); columnPanel.repaint(); //////////////////////////////////// } }
-
To add a row below a current row, you will need to remove all rows from the container that holds them (I think that the method is removeAll(), but the API will say for sure what it is), then re-add them to the same container in the order desired, then call revalidate then repaint on the container. It would help to have an ArrayList of rows, so you can place them in the order desired in the ArrayList<JPanel>, then use this ArrayList to repopulate the container by iterating through it with a for loop.
- 03-06-2011, 02:48 PM #27
Hello,

ArrayList<JPanel> listRowPanel= new ArrayList<JPanel>();
for(int i = 0;i<listRowPanel.size();i++)
{
}
- 03-06-2011, 02:49 PM #28
-
Thanks for the heads up Darryl. To the OP, please read what JavaRanch says about cross-posting without notification as the same considerations apply here. Darryl gives you the link for this in your cross-post. I'll await your reply and your list of links before doing anything else in this thread.
- 03-06-2011, 03:39 PM #30
How can I know the index of the current rwoPanel??
Look at the picture up.
Java Code://how to get the index of current clicked rowpanel rowPanel.getComponent(index); System.out.println(index); int i; //for to add rowPanel before the current clicked one for( i = 0;i<index;i++) { // listRowPanel[i]=rowPanel; listRowPanel.add(rowPanel); // how can I add rows to the jtable } //add the new row to the list listRowPanel.add(rowPanel); //add the rest of rowpanels for(int j = i;i<nbrows;j++) { listRowPanel.add(rowPanel); } // removeall columnPanel.removeAll(); nbrows++; for(int k = 0;k<nbrows;k++) { columnPanel.add(rowPanel); columnPanel.validate(); columnPanel.repaint(); }
How can I do????????
- 03-06-2011, 03:59 PM #31
Sorry,
I posted this question the other day in Java dynamic gui help (Swing / AWT / SWT / JFace forum at JavaRanch) and Java dynamic gui help - Java, but wasn't able to get a good answer, so now I'm asking here...
- 03-06-2011, 04:22 PM #32
Here the full code
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; import javax.swing.JTextField; import javax.swing.JTextArea; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.ArrayList; import java.util.Random; public class Test_DG extends JFrame { private JPanel contentPane; static int nbrows; JPanel columnPanel = new JPanel(); JButton btnStart = new JButton("Start"); ArrayList<JPanel> listRowPanel= new ArrayList<JPanel>();; /** * 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, 637, 441); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); btnStart.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { addRow(); } }); btnStart.setBounds(349, 21, 78, 23); contentPane.add(btnStart); columnPanel.setBounds(29, 24, 310, 368); contentPane.add(columnPanel); columnPanel.setBackground(Color.ORANGE); columnPanel.setLayout(new GridLayout(13, 1, 0, 2)); } void addRow() { JPanel rowPanel = new JPanel(); rowPanel.setLayout(null); JButton lbl = new JButton(""); Random randGen = new Random(); lbl.setText("N°"+nbrows +" "+randGen.nextInt(8) +randGen.nextInt(8)); /////////////////// //////////////////// lbl.setBounds(0, 3, 95, 18); rowPanel.add(lbl); ////////////////////////////////// JButton btnUp = new JButton("^"); btnUp.setBounds(95, 3, 45, 18); rowPanel.add(btnUp); JButton btnDown = new JButton("V"); btnDown.setBounds(139, 3, 45, 18); rowPanel.add(btnDown); JButton btnDel = new JButton("Del"); btnDel.setBounds(185, 3, 55, 18); rowPanel.add(btnDel); /////////////////////////////////////// JButton btnAdd = new JButton("Add"); btnAdd.setBounds(241, 3, 65, 18); rowPanel.add(btnAdd); int index = 0; rowPanel.getComponent(index); System.out.println(index); btnAdd.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { addRow(); //////// ISNT RECURSION HERE ?? } }); //int index = 0; btnDel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { //columnPanel.remove(this.rowPanel); } }); //how to get the index of current clicked rowpanel rowPanel.getComponent(index); System.out.println(index); int i; //for to add rowPanel before the current clicked one for( i = 0;i<index;i++) { // listRowPanel[i]=rowPanel; listRowPanel.add(rowPanel); // how can I add rows to the jtable } //add the new row to the list listRowPanel.add(rowPanel); //add the rest of rowpanels for(int j = i;i<nbrows;j++) { listRowPanel.add(rowPanel); } // removeall columnPanel.removeAll(); nbrows++; for(int k = 0;k<nbrows;k++) { columnPanel.add(rowPanel); columnPanel.validate(); columnPanel.repaint(); } //////////////////////////////////// } void deletRow(/*the index*/) { } }
-
You don't have recursion issues but rather an infinite for loop. In this segment:
Your stop condition is i < nbrows, but where in the loop do you change i and so when will it ever be < nbrows? So you'll end up trying to add rowPanel to listRowPanel an infinite number of times.Java Code:for(int j = i;i<nbrows;j++) { listRowPanel.add(rowPanel); }
-
To know which row panel you are dealing with, first you can get the JButton that was pressed via the ActionEvent's getSource() method, and then you can the "parent" component which will be the row JPanel via getParent().
- 03-06-2011, 09:05 PM #35
Hello,
I tryed to add this line after addRow()
Clicking on each add button I get these lines in console:Java Code:System.out.println( ( (JButton) ev.getSource()).getParent());
javax.swing.JPanel[,0,0,310x26,alignmentX=0.0,alignmentY=0.0,border=,flag s=9,maximumSize=,minimumSize=,preferredSize=]
javax.swing.JPanel[,0,28,310x26,alignmentX=0.0,alignmentY=0.0,border=,flag s=9,maximumSize=,minimumSize=,preferredSize=]
javax.swing.JPanel[,0,56,310x26,alignmentX=0.0,alignmentY=0.0,border=,flag s=9,maximumSize=,minimumSize=,preferredSize=]
...
javax.swing.JPanel[,0,168,310x26,alignmentX=0.0,alignmentY=0.0,border=,flag s=9,maximumSize=,minimumSize=,preferredSize=]
Here, I remarked that this number from 0, the first button to 168.
0 28 56 84 112 140 168
that's mean x=0 and y change with position of the jpanel.
Should I manipulate this lines to know the places of rowpanels ??Last edited by alibm; 03-06-2011 at 09:25 PM.
-
You would iterate through the ArrayList checking if the JPanel extracted equals the JPanel in the list. If so, you've found your position and the index of the JPanel. Then insert the new JPanel just after or before depending on your needs.
- 03-07-2011, 10:07 AM #37
Hello,
I get the index of the wanted rowPanel under which I will add a new one.
Now, the next step is to add rowpanels from the first to the clicked one in the arraylist, then insert the new one in the list and finish with the rest of rowpanels just sill in columnpanel.
So I will put this code in addRow() method.
just using the rowpanel var in the addRow() method, I get an error and Eclipse ask me to trabsform it to final.
Doing as Eclipse want and Testing the code, I get all buttons equal.
And clicking all buttons when I add rowpanel, always write in console 0.
Your suggestions Pleaaaaaase
- 03-07-2011, 10:42 AM #38
Hello,
I tryed columnPanel.removeAll(); in a separated button.
After clicking this button, all rowpanels still and Add button don't work even the buton with the removeAll() method.
??
- 03-07-2011, 10:19 PM #39
Hello,
I think you're in work now!
But I want to say that I progressed more.
I strated understandinig things in Java, that methods like getSource, getParentComponent... etc.
I never used them before but it's a time because it's my academic project and I must finish this months to assist for course and exams of 2nd semester.
Also, I have the end of study report to write ...
I writted the code of Delete row button but it doesn't work correctly.
I iterated the arraylist and I get the index of the rowPanel in which I clicked Delete button.
After I used
Here I think that the first button had been deleted not the current where I clicked and trying to delete an other one will cause error.Java Code:Component[]allpanel=columnPanel.getComponents(); columnPanel.remove(allpanel.length-index); columnPanel.revalidate();
Should I update the content of listRowPanel here ...??
Returning to add button:
I get the index of clicked Add button.
I get too the number of rowPanel too.
I remove all rowPanels from columPanel and I started adding rowPanel from the listRowPanel by iteration.
if the index equal to counter I add the new row else I continue adding rowPanels from the listRowPanel ...
here I get an error :
Eclipse asked me to change btnAdd to final !
How acan I said that the button add him self?? this doesn't worked....
This is the full code.
I hilighted the errors with red.
Java Code:import java.awt.BorderLayout; public class Test_DG extends JFrame { private JPanel contentPane; static int nbrows; JPanel columnPanel = new JPanel(); JButton btnStart = new JButton("Start"); ArrayList<JPanel> listRowPanel= new ArrayList<JPanel>(); private final JButton button_1 = new JButton("New button");; Dimension dimRowPanel = new Dimension(310, 26); /** * 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, 637, 441); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); btnStart.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { addRow(); } }); btnStart.setBounds(349, 21, 78, 23); contentPane.add(btnStart); ///////////////////////////////// /*JScrollPane scrollPane = new JScrollPane(); scrollPane.setBounds(29, 24, 310, 368); contentPane.add(scrollPane); scrollPane.setViewportView(columnPanel); columnPanel.setLayout(new GridLayout(0, 1, 0, 0));*/ ///////////////////////////////// columnPanel.setBounds(29, 24, 310, 368); contentPane.add(columnPanel); columnPanel.setBackground(Color.ORANGE); columnPanel.setLayout(new GridLayout(13, 1, 0, 2)); JButton button = new JButton("New button"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { } }); button.setBounds(349, 71, 89, 23); contentPane.add(button); } void addRow() { JPanel rowPanel = new JPanel(); rowPanel.setLayout(null); rowPanel.setPreferredSize(dimRowPanel); JButton lbl = new JButton(""); Random randGen = new Random(); lbl.setText("N°"+nbrows +" "+randGen.nextInt(8) +randGen.nextInt(8)); lbl.setBounds(0, 3, 95, 18); rowPanel.add(lbl); JButton btnUp = new JButton("^"); btnUp.setBounds(95, 3, 45, 18); rowPanel.add(btnUp); JButton btnDown = new JButton("V"); btnDown.setBounds(139, 3, 45, 18); rowPanel.add(btnDown); ////////////////////////////////////////////////////////////////////////////////////////////////////// JButton btnDel = new JButton("Del"); btnDel.setBounds(185, 3, 55, 18); rowPanel.add(btnDel); btnDel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ev) { int index=0; Container d=( (JButton) ev.getSource()).getParent(); Iterator<JPanel> itr = listRowPanel.iterator(); [COLOR="Red"]while (itr.hasNext()) { JPanel element = itr.next(); if (element.equals(d)) { //System.out.println(element+" true" ); System.out.println("index of del : "+ index ); } index++; } Component[]allpanel=columnPanel.getComponents(); columnPanel.remove(allpanel.length-index); columnPanel.revalidate(); validate(); repaint(); }[/COLOR] }); ///////////////////////////////////////// btnDel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { /////////columnPanel.removeAll(); } }); //////////////////////////////////////////////////////////////////////////////////////////////// JButton btnAdd = new JButton("Add"); btnAdd.setBounds(241, 3, 65, 18); btnAdd.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ev) { addRow(); Container d=( (JButton) ev.getSource()).getParent(); int index=0; Iterator<JPanel> itr = listRowPanel.iterator(); while (itr.hasNext()) { JPanel element = itr.next(); if (element.equals(d)) { //System.out.println(element+" true" ); System.out.println("index off Add : "+ index ); } index++; } int nbAllPanel=columnPanel.getComponentCount(); Component[]allpanel=columnPanel.getComponents(); columnPanel.removeAll(); int index2=0; [COLOR="Red"]while(index2<nbAllPanel) { if(index2==index) { [B] columnPanel.add(btnAdd);[/B] } else { columnPanel.add(allpanel[index2]); } }[/COLOR] } }); nbrows++; [COLOR="Blue"]listRowPanel.add(rowPanel); columnPanel.add(rowPanel); columnPanel.validate(); columnPanel.repaint();[/COLOR] } /////////////////////////////////////////////////////////////////////////////////////////// }
- 03-07-2011, 10:21 PM #40
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