Results 1 to 6 of 6
- 05-06-2011, 09:30 AM #1
Member
- Join Date
- May 2011
- Posts
- 2
- Rep Power
- 0
JPanel not shown after setMinimumSize()
if I change this toJava Code:JPanel content = new JPanel(); content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS)); UserInfo userInfo = new UserInfo(ratio, downloaded, uploaded); Buttons buttonPannel = new Buttons(); content.add(userInfo); content.add(buttonPannel);
buttonPannel is not shown in my frame.Java Code:JPanel content = new JPanel(); content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS)); UserInfo userInfo = new UserInfo(ratio, downloaded, uploaded); [B]userInfo.setMinimumSize(new Dimension(400,65));[/B] Buttons buttonPannel = new Buttons(); content.add(userInfo); content.add(buttonPannel);
What can I do to make it show buttonPannel and userInfo have minimum size set?
Both UserInfo and Buttons classes extend JPanel.
- 05-06-2011, 09:44 AM #2
To get better help sooner, post a SSCCE that clearly demonstrates your problem.
db
- 05-06-2011, 10:21 AM #3
Member
- Join Date
- May 2011
- Posts
- 2
- Rep Power
- 0
OK, so my code looks like this if added to one place:
and somehow whenever i set minimum size for userInfo my buttonPanel is not shownJava Code:import java.awt.Container; import java.awt.Dimension; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; public class Main { public static void main(String[] args) { JFrame langas = new JFrame(); langas.setLayout(new BoxLayout(langas.getContentPane(), BoxLayout.Y_AXIS)); langas.setVisible(true); langas.setSize(500,500); langas.setMinimumSize(new Dimension(500,200)); langas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel mainPanel = new JPanel(); mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS)); JScrollPane contentContainer = new JScrollPane(mainPanel); langas.add(contentContainer); JPanel indents = new JPanel(); indents.setLayout(new BoxLayout(indents, BoxLayout.X_AXIS)); mainPanel.add(Box.createRigidArea(new Dimension(0,10))); mainPanel.add(indents); mainPanel.add(Box.createRigidArea(new Dimension(0,10))); JPanel content = new JPanel(); content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS)); JPanel userInfo = new JPanel(); JPanel ratioPanel = new JPanel(); JLabel ratioLabel1 = new JLabel("Ratio:"); JLabel ratioLabel2 = new JLabel("1.297"); ratioLabel1.setAlignmentX(Container.RIGHT_ALIGNMENT); ratioLabel2.setAlignmentX(Container.RIGHT_ALIGNMENT); ratioPanel.setLayout(new BoxLayout(ratioPanel, BoxLayout.Y_AXIS)); ratioPanel.add(ratioLabel1); ratioPanel.add(ratioLabel2); ratioPanel.setOpaque(false); JPanel downloadedPanel = new JPanel(); JLabel downloadedLabel1 = new JLabel("Downloaded:"); JLabel downloadedLabel2 = new JLabel("1 GB"); downloadedLabel1.setAlignmentX(Container.RIGHT_ALIGNMENT); downloadedLabel2.setAlignmentX(Container.RIGHT_ALIGNMENT); downloadedPanel.setLayout(new BoxLayout(downloadedPanel, BoxLayout.Y_AXIS)); downloadedPanel.add(downloadedLabel1); downloadedPanel.add(downloadedLabel2); downloadedPanel.setOpaque(false); JPanel uploadedPanel = new JPanel(); JLabel uploadedLabel1 = new JLabel("Uploaded:"); JLabel uploadedLabel2 = new JLabel("3 GB"); uploadedLabel1.setAlignmentX(Container.RIGHT_ALIGNMENT); uploadedLabel2.setAlignmentX(Container.RIGHT_ALIGNMENT); uploadedPanel.setLayout(new BoxLayout(uploadedPanel, BoxLayout.Y_AXIS)); uploadedPanel.add(uploadedLabel1); uploadedPanel.add(uploadedLabel2); uploadedPanel.setOpaque(false); userInfo.setLayout(new BoxLayout(userInfo, BoxLayout.X_AXIS)); userInfo.add(Box.createHorizontalGlue()); userInfo.add(ratioPanel); userInfo.add(Box.createRigidArea(new Dimension(60,0))); userInfo.add(downloadedPanel); userInfo.add(Box.createRigidArea(new Dimension(60,0))); userInfo.add(uploadedPanel); userInfo.add(Box.createRigidArea(new Dimension(40,0))); userInfo.setAlignmentX(Container.LEFT_ALIGNMENT); [B]userInfo.setMinimumSize(new Dimension(400,65));[/B] JPanel buttonPannel = new JPanel(); JButton newsButton = new JButton("aaa"); JButton torrentsButton = new JButton("bbb"); JButton zoneButton = new JButton("ccc"); buttonPannel.setLayout(new BoxLayout(buttonPannel,BoxLayout.X_AXIS)); buttonPannel.add(Box.createHorizontalGlue()); buttonPannel.add(newsButton); buttonPannel.add(torrentsButton); buttonPannel.add(zoneButton); buttonPannel.add(Box.createHorizontalGlue()); buttonPannel.setAlignmentX(Container.LEFT_ALIGNMENT); content.add(userInfo); content.add(buttonPannel); indents.add(Box.createRigidArea(new Dimension(10,0))); indents.add(content); indents.add(Box.createRigidArea(new Dimension(10,0))); } }
- 05-06-2011, 12:31 PM #4
Member
- Join Date
- May 2011
- Location
- Munich
- Posts
- 15
- Rep Power
- 0
try using setPrefferredSize(). I always have difficulties with setMinimumSize()
read my blog : www.blue-walrus.com
- 05-06-2011, 02:29 PM #5
1. Always construct, manipulate and query all Swing components only on the EDT.
Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)
2. When assembling a GUI, make it visible only after all components have been added. In your code the JFrame is setVisible(true) before any content is added to it.
2a. Whenever it is necessary to add/remove compoennts to/form an already visible Swing container, invoke revalidate() and repaint() on that container when done. This isn't necessary here, the components should be added before making the GUI visible.
Also, the guidelines for an SSCCE don't make it a condition that all the code should be in one almost unreadable main method. I had to refactor your code before I could even see what was wrong. Here's the result (method names could be improved on, and further refactoring is possible)And use the methods of Box that return the strut you require. Everything doesn't have to be a rigid area.Java Code:import java.awt.Container; import java.awt.Dimension; import javax.swing.*; public class BoxLayoutProblem { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new BoxLayoutProblem().makeUI(); } }); } public void makeUI() { JPanel content = new JPanel(); content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS)); content.add(getUserInfoPanel()); content.add(getButtonPanel()); JPanel indents = getIndentsPanel(content); JPanel mainPanel = new JPanel(); mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS)); mainPanel.add(Box.createVerticalStrut(10)); mainPanel.add(indents); mainPanel.add(Box.createVerticalStrut(10)); JScrollPane contentContainer = new JScrollPane(mainPanel); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS)); frame.add(contentContainer); frame.setSize(500, 500); frame.setMinimumSize(new Dimension(500, 200)); frame.setVisible(true); } private JPanel getUserInfoPanel() { JPanel userInfo = new JPanel(); userInfo.setLayout(new BoxLayout(userInfo, BoxLayout.X_AXIS)); userInfo.add(Box.createHorizontalGlue()); userInfo.add(getPanel("Ratio:", "1.297")); userInfo.add(Box.createHorizontalStrut(60)); userInfo.add( getPanel("Downloaded:", "3 GB")); userInfo.add(Box.createHorizontalStrut(60)); userInfo.add(getPanel("Uploaded:", "1 GB")); userInfo.add(Box.createHorizontalStrut(40)); userInfo.setAlignmentX(Container.LEFT_ALIGNMENT); userInfo.setMinimumSize(new Dimension(400, 65)); return userInfo; } private JPanel getPanel(String text1, String text2) { JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); JLabel label1 = new JLabel(text1); JLabel label2 = new JLabel(text2); label1.setAlignmentX(Container.RIGHT_ALIGNMENT); label2.setAlignmentX(Container.RIGHT_ALIGNMENT); panel.add(label1); panel.add(label2); panel.setOpaque(false); return panel; } private JPanel getIndentsPanel(JPanel content) { JPanel indents = new JPanel(); indents.setLayout(new BoxLayout(indents, BoxLayout.X_AXIS)); indents.add(Box.createHorizontalStrut(10)); indents.add(content); indents.add(Box.createHorizontalStrut(10)); return indents; } private JPanel getButtonPanel() { JPanel buttonPannel = new JPanel(); buttonPannel.setLayout(new BoxLayout(buttonPannel, BoxLayout.X_AXIS)); JButton newsButton = new JButton("aaa"); JButton torrentsButton = new JButton("bbb"); JButton zoneButton = new JButton("ccc"); buttonPannel.add(Box.createHorizontalGlue()); buttonPannel.add(newsButton); buttonPannel.add(torrentsButton); buttonPannel.add(zoneButton); buttonPannel.add(Box.createHorizontalGlue()); buttonPannel.setAlignmentX(Container.LEFT_ALIGNMENT); return buttonPannel; } }
db
- 05-06-2011, 02:43 PM #6
Similar Threads
-
I don’t want my pages to be shown in the history at all
By jaisingh.saini in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 11-19-2010, 08:55 AM -
File transfer to be shown in progressbar
By mohanmurali2010 in forum Java ServletReplies: 0Last Post: 07-12-2010, 10:26 AM -
eclipse 3.4 is shown errors
By ramaksilvertide in forum New To JavaReplies: 2Last Post: 02-09-2009, 11:51 PM -
uploaded image not shown
By java_srinivasan in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 11-05-2008, 12:40 PM -
No output shown
By ai_2007 in forum Advanced JavaReplies: 4Last Post: 07-10-2007, 09:26 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks