Hi,

I am a new Java Programmer.could you help me in resolving this.

I am trying to created a JPanel withing a main JPanel. For the nested one, i have included Grid Layout and i am trying to add components to it dynamically.
i.e i have added a combo box to the nested JPanel and based on the selection it makes, i want to add few more components to the same JPanel.

For this i created some code, but the problem is irrespective of the size, i mention for the combo box, it takes up the whole size of the JPanel and hence, the other components which needs to get added to the panel, based upon the selection of the combo box, are not getting added to the JPanel.

I searched through the net and understood that this has something to do with the layout manager only. But i am unable to debug this out..

Please help me with this

Note - while running the code, please enter the number of rows to be added as 1, since ive not yet handled multiple rows

import java.io.*;
import javax.swing.*;
import java.awt.*;

import java.awt.event.*;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import javax.swing.JLabel;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.IOException;
import java.net.*;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class tmp extends JFrame implements MouseListener, MouseMotionListener {
	

	public static JFrame frame;
	public static JTextField nor;
	public static JPanel panel;
	public static JPanel[] panels = new JPanel[500];
	public static int ctr = 0,inc=0;
	public static JScrollBar jb1,jb2;
	public static JScrollPane jp;
	public static JFileChooser jf1,jf2;
	public static JCheckBox[] jr = new JCheckBox[600];
	public static JTextField[] at = new JTextField[600];
	public static String svc_name,svc_vers;
	public static JComboBox[] combo = new JComboBox[500];
	public void Front_end()
	{
		
		frame = new JFrame("abc");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setBounds(0,0,1000,5000);
		panel = new JPanel();
		GUI();
		
		jp = new JScrollPane(panel);		
		jp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS );
		frame.add(jp);
		
		
		frame.setVisible(true);
		
	}
	public void fillPanel(int tmp)
	 {
		
		panels[tmp].setLayout(new GridLayout(2,7,8,8));
		panels[tmp].setBounds(5,150,100,200);
		
		panel.add(panels[tmp]);
		
		String choices[] = {" ","Offer Name change","Offer Price change","New Offer","Offer Code Change"};
		inc = tmp;
		
		combo[inc] = new JComboBox(choices);
		combo[inc].setSelectedIndex(0);
		combo[inc].setSize(50, 50);
		combo[inc].setBounds(20,160,70,100);
		panels[tmp].add(combo[inc]);
		combo[inc].addItemListener(new ItemListener(){
			  public void itemStateChanged(ItemEvent ie){
			  String str = (String)combo[inc].getSelectedItem();
		
				if(str.equals("Offer Name change"))
				{
					System.out.println("We have a match");
					offerNameChange(inc);
					
				}
				else if(str.equals("Offer Price change"))
				{
					System.out.println("Second match");
				}
			}
		});
		
	}
	public void offerNameChange(int countr)
	{
		JLabel name = new JLabel("Name of the offer");
		name.setBounds(120, 300, 100, 100);
		panels[countr].add(name);
		panel.add(panels[countr]);
		System.out.println("Success");
	}
	
	public void mouseEntered(MouseEvent e) { }
	public void mouseExited   ( MouseEvent e ){}
	public void mousePressed  ( MouseEvent e ) {}
	public void mouseDragged(MouseEvent e) {}
	public void mouseMoved(MouseEvent e) { }
	public void mouseReleased ( MouseEvent e ) {}
	public void mouseClicked(java.awt.event.MouseEvent e) { }
	public void GUI()
	{
		panel.setLayout(null);
		panel.setSize(3000,5000);
		Dimension dim = new Dimension(3000,5000);
		panel.setPreferredSize(dim);
		
		JLabel head = new JLabel("ghg");
		Dimension dd = new Dimension();
		dd.height = 200;
		dd.width = 100;
		head.setSize(dd);
		head.setBounds(300, 10, 200, 50);
		panel.add(head);
		
		JLabel norw = new JLabel("No. of rows more to be added");
		norw.setBounds(600,45,200,50);
		panel.add(norw);
		
		nor = new JTextField();
		nor.setBounds(770,55,25,25);
		panel.add(nor);
		
		JButton nrw = new JButton("Add");
		nrw.setBounds(800,55,60,25);
		panel.add(nrw);
	
	 nrw.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent ae)
        {
        	int i=Integer.parseInt(nor.getText());
        	int temp = ctr;
        	ctr = ctr + i;
        	for (int ii=0;ii<i;ii++)
        	{
        		panels[temp] = new JPanel();
        		fillPanel(temp);
         
        		temp++;
        	}
        	System.out.println("Out of the loop");
    		ctr++;
        	panel.revalidate();
        	panel.repaint();
        	
}
});
	 }
	public static void main(String[] args){
		System.out.println("Hi");
		tmp har = new tmp();
		har.Front_end();
	}
}

Also, i would like to include multiple such panels arranged vertically growing downwards. Could someone suggest some idea for that.
The constraint in this case is, at first ill go and add 10 rows and then later depending on my requirement, ill go and delete a particular row in the middle (say 5th) using the delete button, which would be provided at last of every row, so the rows must get rearranged in order to fill the gap made by the removal on the middle row. Please help me with this too..

Thanks in advance

Member Avatar for hfx642

For that scenario, I wrap the JComboBox in a JPanel (FlowLayout) of its own.
Then, add THAT JPanel to the containing JPanel.
The wrapper JPanel will shrink your JComboBox down to its appropriate size.

For that scenario, I wrap the JComboBox in a JPanel (FlowLayout) of its own.
Then, add THAT JPanel to the containing JPanel.
The wrapper JPanel will shrink your JComboBox down to its appropriate size.

Ohh okk..So does this mean that i need to include panels within that nested panel...Since i am planning to add multiple components to that main nested panel, wouldnt this complicate the process..
Isn't there a way, wherein i can add multiple components of some fixed size to a same nested Panel?

Member Avatar for hfx642

I do wrap components and panels all the time to "shrink" things down. It's up to you to decide if you need to do that.
Yes, but if you use an appropriate naming convention, it will help you keep things straight.
Yes.

Thank you so much..
I actually tried using flow layout instead of grid layout.Now i am seeing all my components getting added linearly. But my doubt is when i use a grid layout, i am supposed to see all the components arranged in a grid wise manner based on the number of columns and rows specified by me, so why is that always the first component that i add takes up the whole size of the JPanel as specified by the GridLayout..

Member Avatar for hfx642

JTextField and JComboBox, I always wrap them in their own JPanel (Flow).
I then place those JPanels in my formatting JPanel (Grid, Border, etc.),
and wrap THAT JPanel in another JPanel (Flow).
I also found that if I have a JPanel (Grid) defined as 3 x 2,
I'd better have 6 .add()s, or the layout will not be formatted correctly.
I don't know what the limit is for nesting (or even if there IS a limit).
If there is, I don't think that I'LL ever reach it.

Thank you so much..
Actually what i did was, i had a separate JPanel for that JcomboBox and then on selection of a value from it, i created another JPanel which had elements arranged in it in a grid wise manner since i had specified the number of rows and columns an d had added all of them simultaneously...
Thanks for helping me through this

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.