CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2011
    Posts
    1

    Adding multiple components dynamically to a nested JPanel

    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

  2. #2
    Join Date
    Mar 2010
    Posts
    16

    Re: Adding multiple components dynamically to a nested JPanel


Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured