Results 1 to 15 of 15
- 08-20-2011, 03:20 AM #1
Can't I add containers into containers? What's wrong here?
Hey again. I am trying to add 3 JPanels inside another, which one is added to another one, which is added to the Applet. The code is the next one:
Java Code:setLayout(null); container = new JPanel(); add(container); container.setLocation(xContainer, yContainer); container1 = new JPanel(new GridLayout(0,3,5,0)); container2 = new JPanel(new GridLayout(5,2,0,0)); container3 = new JPanel(new GridLayout(6,2,0,0)); container4 = new JPanel(new GridLayout(3,2,0,0)); container.add(container1); container1.add(container2, 0); container1.add(container3, 1); container1.add(container4, 2);
Then, I add components to container2, 3 and 4. The problem is that I can't see anything on the screen related to the JPanels. Is there anything ill-posed ? Thanks.
-
Get rid of your use of null layout as it's messing you up and for the most part should almost never be used. Better to use the flexible layout mangers as you're using for some of your JPanels.
- 08-20-2011, 03:55 PM #3
Yes, that's a great idea. But now I have another problem. When I try to add a new Button or JButton to the Applet, outside the "container" JPanel, as it uses the BorderLayout, and that layout specifies that if a component does not fill all the Applet site, another one must do (in brief that the layout must fill all the Applet), it gets expended till it covers all the Applet, without respect whatever you can sey him with .setBounds or .setLocation . I'd just like a method from Component or JComponent that could perform the Button to avoid the BorderLayout. Thanks.
-
You'll likely want to still use the BorderLayout, but just create a JPanel/container that uses another layout and place that JPanel in the contentPane's BorderLayout.CENTER position. Then you can add more containers or components into that JPanel. You can continue nesting JPanels/containers each using whatever layout works best of its contents to your heart's content.
- 08-20-2011, 04:18 PM #5
Thank you. Btw, another question that has nothing to do with the other ones. Is there any reason why I can't see the JLabels inside the JPanel (I just see the ones without anything wrote on them , (""), )? The JLabels are made like this:
Some other don't use a specific font, just some ones.Java Code:JLabel labelEsfera; init { labelEsfera = new JLabel("Esfera"); labelEsfera.setFont(fuenteTitulos); container3.add(labelEsfera,0); }
Thanks.
-
No idea without the context. Since this is a new question, perhaps it would be best if you create a new question / thread here. If you do, your best bet is to create a small compilable program, an SSCCE, that demonstrates your problem, that we can all run and modify and better help you solve.
- 08-21-2011, 02:02 AM #7
What I am focus here in your problem is that you can't see anything on the screen related to your JPanels. You can't really see a JPanel unless you put a border on it. . To add border on your JPanel so you can see it, here is the code:
Java Code://Constructing JPanel JPanel panel = new JPanel(); panel.setLayout(new GridLayout(3,1)); //Setting layout on JPanel with 3 rows and 1 column panel.setBorder(BorderFactory.createTitledBorder("Select an Image")); //Create a titled border on JPanel
- 08-21-2011, 03:18 AM #8
Thanks for the help, but the answer on in the deleted thread was correct. This problem happens basically when you add components to the applet or the component, when it was alredy visible (painted). So, the solution is just to add the components at the start of the init() method, nowhere else.
-
- 08-21-2011, 04:17 PM #10
For god sake, it was as simple as change the JPanels for Panels. Don't know where's the difference, but the problem is solved...
-
- 08-21-2011, 04:27 PM #12
I'll pass you the example that I used to solve it, that simply represents the problem in a shorter way; it adds some swing components inside containers and then paints something.
With this code I have problems with swing components (using JPanels):
With this one, the problem is solved (with Panels):Java Code:/** * Applet Prueba * * <APPLET CODE="Prueba.class" WIDTH="500" HEIGHT="500"></APPLET> */ import java.applet.*; import java.awt.*; import javax.swing.*; import javax.swing.JLabel; import javax.swing.JPanel; public class Prueba extends JApplet{ JButton button; JButton button2; JButton button3; JPanel panel; JPanel panel2; JPanel panel3; JComboBox combo; public void init() { setLayout(null); button = new JButton("HelloWorld"); button2 = new JButton("HolaMundo"); button3 = new JButton("HolaMundo"); String[] fluidoArray = {"Aire", "Aigua", "Glicerina"}; combo = new JComboBox(fluidoArray); panel = new JPanel(new GridLayout(1,2,5,5)); panel2 = new JPanel(new GridLayout(2,1)); panel3 = new JPanel(new GridLayout(2,1)); add(panel); panel.setBounds(100, 100, 100, 100); panel.add(panel2,0); panel.add(panel3,1); panel2.add(button3,0); panel2.add(button2,1); panel3.add(button,0); panel3.add(combo,1); } public void paint(Graphics g) { g.drawString("Hello", 50, 30); } }
Java Code:/** * Applet Prueba * * <APPLET CODE="Prueba.class" WIDTH="500" HEIGHT="500"></APPLET> */ import java.applet.*; import java.awt.*; import javax.swing.*; import javax.swing.JLabel; import javax.swing.JPanel; public class Prueba extends JApplet{ JButton button; JButton button2; JButton button3; Panel panel; Panel panel2; Panel panel3; JComboBox combo; public void init() { setLayout(null); button = new JButton("HelloWorld"); button2 = new JButton("HolaMundo"); button3 = new JButton("HolaMundo"); String[] fluidoArray = {"Aire", "Aigua", "Glicerina"}; combo = new JComboBox(fluidoArray); panel = new Panel(new GridLayout(1,2,5,5)); panel2 = new Panel(new GridLayout(2,1)); panel3 = new Panel(new GridLayout(2,1)); add(panel); panel.setBounds(100, 100, 100, 100); panel.add(panel2,0); panel.add(panel3,1); panel2.add(button3,0); panel2.add(button2,1); panel3.add(button,0); panel3.add(combo,1); } public void paint(Graphics g) { g.drawString("Hello", 50, 30); } }
-
No, this is a Swing application, and you most definitely do not want to use Panel in place of JPanel. What behavior is the second bit of code showing that the first bit is not? And again, why are you still using null layout when this is likely contributing to your problem (as noted in my first post above)?
Edit: Also consider uploading an image of just what you want your GUI to look like.Last edited by Fubarable; 08-21-2011 at 04:50 PM.
-
Edit: one major problem I see is that you're drawing directly on the JApplet and that won't work. Better to draw in a paintComponent method of a JPanel and then add that JPanel to the applet.
For example:
Java Code:import java.awt.*; import javax.swing.*; public class Prueba extends JApplet { JButton button; JButton button2; JButton button3; JPanel panel; JPanel panel2; JPanel panel3; JComboBox combo; public void init() { JPanel mainPanel = new JPanel() { @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.drawString("Hello", 50, 30); } }; getContentPane().add(mainPanel); button = new JButton("HelloWorld"); button2 = new JButton("HolaMundo"); button3 = new JButton("HolaMundo"); String[] fluidoArray = {"Aire", "Aigua", "Glicerina"}; combo = new JComboBox(fluidoArray); panel = new JPanel(new GridLayout(1, 2, 5, 5)); panel2 = new JPanel(new GridLayout(2, 1)); panel3 = new JPanel(new GridLayout(2, 1)); panel.add(panel2, 0); panel.add(panel3, 1); panel2.add(button3, 0); panel2.add(button2, 1); panel3.add(button, 0); panel3.add(combo, 1); mainPanel.setLayout(new BorderLayout()); JPanel bottomPanel = new JPanel(); bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.LINE_AXIS)); bottomPanel.add(Box.createHorizontalGlue()); bottomPanel.add(panel); mainPanel.add(bottomPanel, BorderLayout.SOUTH); } }
- 08-22-2011, 12:14 AM #15
Similar Threads
-
transparant containers
By Dipke in forum New To JavaReplies: 5Last Post: 03-27-2011, 02:41 PM -
Help using JPanel as containers + being able to move them
By Skye in forum AWT / SwingReplies: 2Last Post: 12-19-2010, 02:24 PM -
Handle Resizing of Swing Containers
By simlei in forum AWT / SwingReplies: 5Last Post: 02-23-2010, 02:20 AM -
First Java-game: Containers
By eastviolence in forum New To JavaReplies: 0Last Post: 04-04-2008, 06:09 PM -
Swing containers
By Java Tip in forum Java TipReplies: 0Last Post: 12-07-2007, 12:06 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks