47679

How to resize a JPanel to fit a JFrame in “docknorth” with no interference to remaining JPanels

Question:

I am doing a little test of a demo Swing GUI. In this demo, the JFrame is composed of 3 "master" JPanels. If you will, the first (jp1) is composed of JLabels, and the other two are composed of several other JPanels. I am using MigLayout.

Here is my sample code:

// All the jPanels JFrame frame = new JFrame(); frame.setLayout(new MigLayout()); JPanel jp1 = new JPanel(); jp1.setLayout(new MigLayout()); jp1.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1)); JPanel jp2 = new JPanel(); jp2.setLayout(new MigLayout()); jp2.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1)); JPanel jp3 = new JPanel(); jp3.setLayout(new MigLayout()); jp3.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1)); JPanel jp4 = new JPanel(); jp4.setLayout(new MigLayout()); jp4.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1)); JPanel jp5 = new JPanel(); jp5.setLayout(new MigLayout()); jp5.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1)); JPanel jp6 = new JPanel(); jp6.setLayout(new MigLayout()); jp6.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1)); JPanel jp7 = new JPanel(); jp7.setLayout(new MigLayout()); jp7.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1)); JPanel bigPanel1 = new JPanel(); bigPanel1.setLayout(new MigLayout()); bigPanel1.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1)); JPanel bigPanel2 = new JPanel(); bigPanel2.setLayout(new MigLayout()); bigPanel2.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1)); //All the labels to be added to JPanel jp1 JLabel label1 = new JLabel(); label1.setText("LABEL1"); JLabel label2 = new JLabel(); label2.setText("LABEL2"); JLabel label3 = new JLabel(); label3.setText("LABEL3"); JLabel label4 = new JLabel(); label4.setText("LABEL4"); jp1.add(label1); jp1.add(label2); jp1.add(label3); jp1.add(label4,"wrap"); bigPanel1.add(jp2); bigPanel1.add(jp6); bigPanel1.add(jp3,"grow,wrap"); bigPanel2.add(jp4); bigPanel2.add(jp7); bigPanel2.add(jp5,"grow,wrap"); frame.getContentPane().add(jp1,"dock north, wrap"); frame.getContentPane().add(bigPanel1,"span,grow,wrap"); frame.getContentPane().add(bigPanel2,"span,grow,wrap"); frame.pack(); frame.setVisible(true);

Which results in this output:<a href="http://i.stack.imgur.com/Wx0SJ.png" rel="nofollow">GUI OUTPUT</a>

What I want to achieve is being able to add labels into the 1st JPanel (jp1) without messing with the remainder JPanels width. Additionally, I want to make the several JPanels inside a bigPanel to occupy its full width, as well as in jp2,jp6 and jp3 to fill bigPanel1.

How should I do this? Thanks in advance.

Answer1:

I have never used MigLayout, and personally dont see the reason if it can be done using default java LayoutManager.

Okay so I used a combination FlowLayout and <a href="http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html" rel="nofollow">GridBagLayout</a> to achieve this, along with gc.fill=GridBagConstraints.NONE and gc.anchor=GridBagConstraints.WEST for those panels which we dont want to fill the <em>contentpane</em> width, also updated as per your comment to stop the JPanel/JFrame from growing larger than the given max width when more JLabels are added this was done using a JScrollPane:

<img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/QVYDs.png" data-original="https://i.stack.imgur.com/QVYDs.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

import java.awt.Color; import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.SwingUtilities; import javax.swing.border.LineBorder; public class Test { public Test() { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.setLayout(new GridBagLayout()); final JPanel labelPanel = new JPanel(); labelPanel.setBorder(new LineBorder(Color.black)); for (int i = 0; i < 5; i++) { labelPanel.add(new JLabel("Label" + (i + 1))); } final int maxWidth = 200; final JScrollPane jsp = new JScrollPane(labelPanel) { @Override public Dimension getPreferredSize() { //we set the height by checking if we exceeed the wanted ith thus a scrollbar will appear an we must incoprate that or labels wont be shpwn nicely return new Dimension(maxWidth, labelPanel.getPreferredSize().width < maxWidth ? (labelPanel.getPreferredSize().height + 5) : ((labelPanel.getPreferredSize().height + getHorizontalScrollBar().getPreferredSize().height) + 5)); } }; JPanel otherPanel = new JPanel(); otherPanel.add(new JLabel("label")); otherPanel.setBorder(new LineBorder(Color.black)); JPanel otherPanel2 = new JPanel(); otherPanel2.add(new JLabel("label 1")); otherPanel2.add(new JLabel("label 2")); otherPanel2.setBorder(new LineBorder(Color.black)); GridBagConstraints gc = new GridBagConstraints(); gc.fill = GridBagConstraints.BOTH; gc.weightx = 1.0; gc.weighty = 1.0; gc.gridx = 0; gc.gridy = 0; frame.add(jsp, gc); gc.fill = GridBagConstraints.NONE; gc.anchor = GridBagConstraints.WEST; gc.gridy = 1; frame.add(otherPanel, gc); gc.anchor = GridBagConstraints.WEST; gc.gridy = 2; frame.add(otherPanel2, gc); frame.pack(); frame.setVisible(true); frame.revalidate(); frame.repaint(); } public static void main(String[] args) { //Create Swing components on EDT SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new Test(); } }); } }

Answer2:

I have use BorderLayout and FlowLayout to manage the layouts. The frame has two JPanel's and one JPanel in it has two more JPanel's. All the internal panels use FlowLayout to align the JLabels. To arrange these panels on the JFrame I have used BorderLayout.

<img alt="Layout Manager" class="b-lazy" data-src="https://i.stack.imgur.com/Wlk86.png" data-original="https://i.stack.imgur.com/Wlk86.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

import java.awt.BorderLayout; import java.awt.Color; import java.awt.FlowLayout; import java.awt.GridBagLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.SwingUtilities; import javax.swing.border.LineBorder; public class LayoutTest { public LayoutTest() { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.setLayout(new GridBagLayout()); JPanel motherPanel = new JPanel(new BorderLayout()); JPanel topPanel = new JPanel(new BorderLayout()); JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); motherPanel.add(topPanel, BorderLayout.NORTH); motherPanel.add(bottomPanel, BorderLayout.CENTER); JPanel topUpperPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); JPanel topBottomPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); topUpperPanel.setBorder(new LineBorder(Color.BLACK)); topBottomPanel.setBorder(new LineBorder(Color.BLACK)); bottomPanel.setBorder(new LineBorder(Color.BLACK)); topPanel.add(topUpperPanel, BorderLayout.PAGE_START); topPanel.add(topBottomPanel, BorderLayout.CENTER); for(int i = 0; i < 3; i++) { JLabel label = new JLabel("Label-" + String.valueOf(i)); label.setBorder(new LineBorder(Color.BLACK)); topUpperPanel.add(label); } for(int i = 0; i < 2; i++) { JLabel label = new JLabel("Label-" + String.valueOf(i)); label.setBorder(new LineBorder(Color.BLACK)); topBottomPanel.add(label); } for(int i = 0; i < 5; i++) { JLabel label = new JLabel("Label-" + String.valueOf(i)); label.setBorder(new LineBorder(Color.BLACK)); bottomPanel.add(label); } frame.add(motherPanel); frame.setTitle("Layout Manager"); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new LayoutTest(); } }); } }

<strong>P.S:</strong> I would suggest you to separate the panels such that there will be "whithout no interference with remaining JPanels."

Recommend

  • Angular2: Difference between Decorators
  • How can I navigate from an “external” .cs file in Silverlight
  • iTextSharp - Use Colspan with PdfPRow
  • If statement operator being ignored?
  • Is there such a thing as static jar linking in java?
  • How to resize UIViewControllerWrapperView
  • Language Scheme: find the sum of proper divisors
  • How to invalidate particular session in Laravel (with user using remember me feature)
  • Contact Usage permission request iphone
  • Using argparse arguments as keyword arguments
  • Doctrine DQL QueryBuilder Excluding Entities where relationship exists
  • Find uninitialized variables in C&C++
  • Does jQuery attr allow for non standards?
  • auto layout leading or trainling -16
  • Default arguments to Scala's main function?
  • How to find specific subgraph in Neo4j using where clause
  • Why has Haskell troubles resolving “overloaded” operators?
  • Does WITH statement execute once per query or once per row?
  • Printing nested dictionary values that may or may not exist
  • What is the difference between GetComponent ().enabled and .SetActive (false); in unity
  • Convert a 12 hour time format to 24 hour time format (keeping record of the day) in python
  • Combining many rectangles into fewer rectangles
  • Packet modification with netfilter queue?
  • Implementing Oracle.Web.dll, Oracle.DataAccess.dll 64-bit failed on Windows Server 2008 with IIS 7.5
  • PHP file_exists() anomaly
  • react split panel resize
  • How to make R's read_csv2() recognise the text characters properly
  • Django model inheritance, filtering models
  • Android Activity.onWindowFocusChanged doesn't get called from within TabHost
  • Appending Character to Character Array In C
  • C: Incompatible pointer type initializing
  • Make VS2015 use angular-cli ng at build time in a .NET project
  • Android fill_parent issue
  • Get object from AWS S3 as a stream
  • Is possible to count alias result on mysql
  • How to make Safari send if-modified-since header?
  • Compare two NSDates in iPhone
  • Benchmarking RAM performance - UWP and C#
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • python draw pie shapes with colour filled