Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-20-2008, 07:03 PM
Member
 
Join Date: Mar 2008
Posts: 8
Bill is on a distinguished road
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

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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-21-2008, 06:29 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,266
hardwired is on a distinguished road
Code:
// Did you mean for this GeneralContent.add(_table, BorderLayout.CENTER); // to be like this? GeneralContent.add(_panel, BorderLayout.CENTER);
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-21-2008, 07:49 PM
Member
 
Join Date: Mar 2008
Posts: 8
Bill is on a distinguished road
Quote:
Originally Posted by hardwired View Post
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 03-21-2008, 09:54 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,266
hardwired is on a distinguished road
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); } }
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 03-22-2008, 08:08 PM
Member
 
Join Date: Mar 2008
Posts: 8
Bill is on a distinguished road
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 ......it's why i use a for loop.....

thanks a lot
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 03-24-2008, 05:51 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,266
hardwired is on a distinguished road
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); } }
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 03-24-2008, 09:38 PM
Member
 
Join Date: Mar 2008
Posts: 8
Bill is on a distinguished road
Hi hardwired,

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

Thx
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
JPanel won't update ibanez270dx New To Java 3 01-06-2009 10:59 PM
refresh JPanel olesja AWT / Swing 1 04-16-2008 05:58 PM
Problem with JPanel ibanez270dx New To Java 2 11-09-2007 07:04 PM
JPanel Problems Riftwalker AWT / Swing 6 10-16-2007 01:16 AM
Draw on JPanel, Help carl Java 2D 1 07-31-2007 08:56 AM


All times are GMT +3. The time now is 03:22 PM.


VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org