69415

MigLayout align center won't center JLabel component

Question:

I am using <a href="http://www.miglayout.com/" rel="nofollow">MigLayout</a> I find it flexible etc,but I am having a problem with centring stuff with it. I tried using gapleft 50% but it seems like the percent number needs to change on different frame sizes, because it's also depending on component's size. so if the component is centred using gapleft 25%, it will be on a different location if i resize the width of my frame.

I've tried using just align center and it doesn't nothing at all.

I've also tried new CC().alignX("center").spanX() and same thing:

<a href="http://gyazo.com/8b562b6dd319d0af1d85cba930af0328.png" rel="nofollow">img http://gyazo.com/8b562b6dd319d0af1d85cba930af0328.png</a>

It's sticks to left, however it does work when I use gapleft, why?

super.setLayout(new MigLayout()); this.loginPane = new LoginPanel(); BufferedImage logo = ImageIO.read(new File("assets/logo.png")); JLabel logoLabel = new JLabel(new ImageIcon(logo)); super.add(logoLabel, new CC().alignX("center").spanX());

Answer1:

<blockquote>

<em>It's sticks to left, however it does work when I use gapleft, why?</em>

</blockquote>

Based on this single line:

super.setLayout(new MigLayout()); // why super? Did you override setLayout() method?

By default <a href="http://www.miglayout.com/" rel="nofollow">MigLayout</a> rows doesn't fill all available width but only the necessary to display the longest row (based on components width). Having said this your JLabel fits the logo image width and nothing more and it looks like stick to left side. You have to tell the layout manager that it has to fill all available width when you instantiate it:

super.setLayout(new MigLayout("fillx"));

Or

LC layoutConstraints = new LC(); layoutConstraints.setFillX(true); super.setLayout(new MigLayout(layoutConstraints);

Then, your component constraints will work as expexted.

<hr /><h3>Picture</h3>

Based on this code snippet:

MigLayout layout = new MigLayout("fillx, debug"); JPanel content = new JPanel(layout); JLabel label = new JLabel("Warehouse"); label.setFont(label.getFont().deriveFont(Font.BOLD | Font.ITALIC, 18)); CC componentConstraints = new CC(); componentConstraints.alignX("center").spanX(); content.add(label, componentConstraints);

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

<hr />

<strong><em>Note:</em></strong> you can enable debug feature by doing this:

super.setLayout(new MigLayout("fillx, debug"));

Or

LC layoutConstraints = new LC(); layoutConstraints.setFillX(true); layoutConstraints.setDebugMillis(500); super.setLayout(new MigLayout(layoutConstraints);

Recommend

  • MigLayout align center won't center JLabel component
  • Caffe GoogleNet classification.cpp gives random outputs
  • Java switch statements outputting the same numbers
  • Getting a video's publish date - not upload date
  • Simple SQL Query in access
  • How do I embed a batch file in a vb program? [closed]
  • JavaScript Regex not detecting
  • AppletViewer bugged and trying to involve a timer
  • change text to everytime I click
  • Google sheets: Compare cell value in sheet 1 to cell values in a column of sheet 2
  • How to generate a certain amount of numbers and spread them randomly across a grid?
  • move_uploaded_file() returns false
  • how can i add a function to json object which has __type attribute?
  • Get Browser to send both If-None-Match and If-Modified-Since
  • statusbar frame sticks to portrait orientation and occludes window.view
  • How to make jQuery `bind` or `on` event handlers idempotent
  • GridBagLayout padding
  • setSelected() with JRadioButton r[]=new JRadioButton[3] not working [duplicate]
  • Outputting SharePoint Hyperlink Column as URL
  • Why the SequenceFile is truncated?
  • Disable Kendo Autocomplete
  • How to view images from protected folder with php?
  • Creating Java object from class name with constructor, which contains parameters [duplicate]
  • how to avoid repetitive constructor in children
  • Listbox within Listbox and scrolling trouble in Windows Phone 7 Silverlight
  • Disable Enter in editText android
  • Deleting and Updating values from a cusrsor adapter
  • Possible to stop flickering java tooltip in heavyweight mode?
  • How to check if every primary key value is being referenced as foreign key in another table
  • R: gsub and capture
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • Comma separated Values
  • How to get icons for entities from eclipse?
  • python regex in pyparsing
  • JTable with a ScrollPane misbehaving
  • Turn off referential integrity in Derby? is it possible?
  • JaxB to read class hierarchy
  • How to load view controller without button in storyboard?