Results 1 to 7 of 7

Thread: Genarate JPanel

  1. #1
    Bill is offline Member
    Join Date
    Mar 2008
    Posts
    8
    Rep Power
    0

    Default Genarate JPanel

    Hi everybody,

    First, i want to excuse my english because i'm a french guy. Thank you

    I want to generate multiple JPanel inside a single JPanel. I try with a for loop, but this method don't give me any succes.

    The situation is...... i recieve an ID between 1 to unlimited and depending this ID i want to generate a JPanel with a Jtable inside

    Ex. ID=3 -> 3 JPanel
    ID=8 -> 8 JPanel

    i hope i'm clear with my poor english

    here is a sample of my code

    Java Code:
                GeneralContent = new JPanel();
                GeneralContent.setName();
                GeneralContent.setLayout(new BorderLayout());
     
               
                deviceInfoContent = new JPanel();
                deviceInfoContent.setLayout(new FlowLayout());
     
                
                JLabel lblDevice = new JLabel();
                JLabel lblDeviceName = new JLabel();
                              
                lblDevice.setText("some text");
                lblDeviceName.setText("some text");
                
                        
                deviceInfoContent.add(lblDevice);
                deviceInfoContent.add(lblDeviceName);
                
                GeneralContent.add(deviceInfoContent,BorderLayout.NORTH);
                GeneralContent.revalidate();
                GeneralContent.add(_table, BorderLayout.CENTER);
                GeneralContent.revalidate();
     
                 //This paenl is my principal container
                _panel.add(GeneralContent);
                _panel.revalidate();
    Thanks a lot guys

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    // Did you mean for this
    GeneralContent.add(_table, BorderLayout.CENTER);
    // to be like this?
    GeneralContent.add(_panel, BorderLayout.CENTER);

  3. #3
    Bill is offline Member
    Join Date
    Mar 2008
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by hardwired View Post
    Java Code:
    // Did you mean for this
    GeneralContent.add(_table, BorderLayout.CENTER);
    // to be like this?
    GeneralContent.add(_panel, BorderLayout.CENTER);
    the _panel is the container of GeneralContent and _table is a JTable in the genralContent.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.table.AbstractTableModel;
    
    public class AddingComponents {
        private JPanel getContent() {
            JPanel generalContent = new JPanel();
            generalContent.setName("generalContent");
            generalContent.setLayout(new BorderLayout());
    
            JPanel deviceInfoContent = new JPanel();
            System.out.println("default layout manager for JPanel = " +
                       deviceInfoContent.getLayout().getClass().getName());
            deviceInfoContent.setLayout(new GridLayout(1,0));
    
            JLabel lblDevice = new JLabel();
            JLabel lblDeviceName = new JLabel();
    
            lblDevice.setText("some text");
            lblDeviceName.setText("some text");
    
            deviceInfoContent.add(lblDevice);
            deviceInfoContent.add(lblDeviceName);
    
            generalContent.add(deviceInfoContent,BorderLayout.NORTH);
    //        generalContent.revalidate();
            generalContent.add(getTable(), BorderLayout.CENTER);
    //        generalContent.revalidate();
    
             //This paenl is my principal container
             JPanel _panel = new JPanel();
             _panel.add(generalContent);
    //         _panel.revalidate();
             return _panel;             // returning either of
                    //generalContent;   // these will work okay
        }
    
        private JScrollPane getTable() {
            JTable table = new JTable(getModel());
            Dimension d = table.getPreferredSize();
            d.height = 175;
            table.setPreferredScrollableViewportSize(d);
            return new JScrollPane(table);
        }
    
        private AbstractTableModel getModel() {
            return new AbstractTableModel() {
                public int getRowCount() { return 10; }
    
                public int getColumnCount() { return 4; }
    
                public Object getValueAt(int row, int col) {
                    return String.valueOf((row+1)) + (col+1);
                }
            };
        }
    
        public static void main(String[] args) {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new AddingComponents().getContent());
            f.pack();
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }

  5. #5
    Bill is offline Member
    Join Date
    Mar 2008
    Posts
    8
    Rep Power
    0

    Default

    hi,

    this work fine, but what i want is to loop into each panel and multiply them and display them one under each other.

    The number of panel can be between 1 to 10 but i don't never know how many panel i have :eek: ......it's why i use a for loop.....

    thanks a lot

  6. #6
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.awt.*;
    import java.util.Random;
    import javax.swing.*;
    import javax.swing.table.AbstractTableModel;
    
    public class AddingComponents {
        Random seed = new Random();
    
        private JPanel getGeneralContent() {
            JPanel generalContent = new JPanel();
            generalContent.setName("generalContent");
            generalContent.setLayout(new BorderLayout());
    
            JPanel deviceInfoContent = new JPanel(new GridLayout(1,0));
    
            JLabel lblDevice = new JLabel("device", JLabel.CENTER);
            JLabel lblDeviceName = new JLabel("name", JLabel.CENTER);
    
            deviceInfoContent.add(lblDevice);
            deviceInfoContent.add(lblDeviceName);
    
            generalContent.add(deviceInfoContent,BorderLayout.NORTH);
            generalContent.add(getTable(), BorderLayout.CENTER);
    
             return generalContent;
        }
    
        private JScrollPane getTable() {
            JTable table = new JTable(getModel());
            Dimension d = table.getPreferredSize();
            d.height = 160;
            table.setPreferredScrollableViewportSize(d);
            return new JScrollPane(table);
        }
    
        private AbstractTableModel getModel() {
            return new AbstractTableModel() {
                public int getRowCount() { return 10; }
    
                public int getColumnCount() { return 2; }
    
                public Object getValueAt(int row, int col) {
                    return String.valueOf((row+1)) + (col+1);
                }
            };
        }
    
        private JScrollPane getIDPanel() {
            int numPanels = 3 + seed.nextInt(8);  // [3 - 10]
            JPanel panel = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.weightx = 1.0;  // Allows horizontal expansion.
            gbc.fill = GridBagConstraints.HORIZONTAL; // Expand.
            gbc.gridwidth = GridBagConstraints.REMAINDER; // 1 column
            Dimension d = new Dimension(200,50);
            for(int i = 0; i < numPanels; i++) {
                JPanel p = new JPanel();
                p.setPreferredSize(d);
                p.setBackground(getColor());
                panel.add(p, gbc);
            }
            JScrollPane scrollPane = new JScrollPane(panel);
            scrollPane.setPreferredSize(new Dimension(200,200));
            return scrollPane;
        }
    
        private Color getColor() {
            return new Color(seed.nextInt(0x1ffffff));
        }
    
        public static void main(String[] args) {
            AddingComponents app = new AddingComponents();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(app.getGeneralContent(), BorderLayout.NORTH);
            f.add(app.getIDPanel());
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }

  7. #7
    Bill is offline Member
    Join Date
    Mar 2008
    Posts
    8
    Rep Power
    0

    Default

    Hi hardwired,

    thanks a lot for your help. I think this is what i want. I'll try this right now.

    Thx

Similar Threads

  1. JPanel won't update
    By ibanez270dx in forum New To Java
    Replies: 3
    Last Post: 01-06-2009, 09:59 PM
  2. refresh JPanel
    By olesja in forum AWT / Swing
    Replies: 1
    Last Post: 04-16-2008, 04:58 PM
  3. Problem with JPanel
    By ibanez270dx in forum New To Java
    Replies: 2
    Last Post: 11-09-2007, 06:04 PM
  4. JPanel Problems
    By Riftwalker in forum AWT / Swing
    Replies: 6
    Last Post: 10-16-2007, 12:16 AM
  5. Draw on JPanel, Help
    By carl in forum Java 2D
    Replies: 1
    Last Post: 07-31-2007, 07:56 AM

Posting Permissions

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