Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #2728 > unrolled thread

Justifying box components

Started by"H.L" <h.l@THRWHITE.remove-dii-this>
First post2011-04-27 15:40 +0000
Last post2011-04-27 15:40 +0000
Articles 4 — 4 participants

Back to article view | Back to comp.lang.java.gui


Contents

  Justifying box components "H.L" <h.l@THRWHITE.remove-dii-this> - 2011-04-27 15:40 +0000
    Re: Justifying box compon "Knute Johnson" <knute.johnson@THRWHITE.remove-dii-this> - 2011-04-27 15:40 +0000
    Re: Justifying box compon "Michael Dunn" <michael.dunn@THRWHITE.remove-dii-this> - 2011-04-27 15:40 +0000
      Re: Justifying box compon "RedGrittyBrick" <redgrittybrick@THRWHITE.remove-dii-this> - 2011-04-27 15:40 +0000

#2728 — Justifying box components

From"H.L" <h.l@THRWHITE.remove-dii-this>
Date2011-04-27 15:40 +0000
SubjectJustifying box components
Message-ID<47309CF9.4432@operamail.com>
  To: comp.lang.java.gui
I want to put a JSlider underneath a label explaining what it is there
for. I have used a Box object instantiated with the createVerticalBox
constructor. The problem is that the text and the slider has a bad
alignment. Is there any way to make them both centered or justified to
either side of the column? Thanks in advance.

Hokan Lane

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [next] | [standalone]


#2729 — Re: Justifying box compon

From"Knute Johnson" <knute.johnson@THRWHITE.remove-dii-this>
Date2011-04-27 15:40 +0000
SubjectRe: Justifying box compon
Message-ID<Wh1Yi.1271$rN1.215@newsfe18.lga>
In reply to#2728
  To: comp.lang.java.gui
H.L wrote:
> I want to put a JSlider underneath a label explaining what it is there
> for. I have used a Box object instantiated with the createVerticalBox
> constructor. The problem is that the text and the slider has a bad
> alignment. Is there any way to make them both centered or justified to
> either side of the column? Thanks in advance.
> 
> Hokan Lane

I haven't played with BoxLayout much but what you want to do is really 
easy with GridBagLayout.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class test  {
     public static void main(String[] args) {
         Runnable r = new Runnable() {
             public void run() {
                 JFrame f = new JFrame("test");
                 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                 f.setLayout(new GridBagLayout());
                 GridBagConstraints c = new GridBagConstraints();
                 c.gridy = 0;  c.insets = new Insets(2,2,2,2);
                 c.anchor = GridBagConstraints.CENTER;

                 JLabel l = new JLabel("JSlider Info");
                 f.add(l,c);

                 ++c.gridy;
                 JSlider s = new JSlider(0,100,50);
                 f.add(s,c);

                 f.setSize(400,300);
                 f.setVisible(true);
             }
         };
         EventQueue.invokeLater(r);
     }
}

-- 

Knute Johnson
email s/nospam/knute/

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#2731 — Re: Justifying box compon

From"Michael Dunn" <michael.dunn@THRWHITE.remove-dii-this>
Date2011-04-27 15:40 +0000
SubjectRe: Justifying box compon
Message-ID<4731272a@dnews.tpgi.com.au>
In reply to#2728
  To: comp.lang.java.gui
"H.L" <H.L@operamail.com> wrote in message news:47309CF9.4432@operamail.com...
>I want to put a JSlider underneath a label explaining what it is there
> for. I have used a Box object instantiated with the createVerticalBox
> constructor. The problem is that the text and the slider has a bad
> alignment. Is there any way to make them both centered or justified to
> either side of the column? Thanks in advance.


if the label is off to the right a bit, add this line
slider.setAlignmentX(0);
but it still might not suit, as the label will appear a bit left of the start of the slider track

if you want them centered, add both lines
slider.setAlignmentX(.5f);
label.setAlignmentX(.5f);

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#2733 — Re: Justifying box compon

From"RedGrittyBrick" <redgrittybrick@THRWHITE.remove-dii-this>
Date2011-04-27 15:40 +0000
SubjectRe: Justifying box compon
Message-ID<47318d02$0$13930$fa0fcedb@news.zen.co.uk>
In reply to#2731
  To: comp.lang.java.gui
Michael Dunn wrote:
> "H.L" <H.L@operamail.com> wrote in message news:47309CF9.4432@operamail.com...
> 
>> I want to put a JSlider underneath a label explaining what it is there
>> for. I have used a Box object instantiated with the createVerticalBox
>> constructor. The problem is that the text and the slider has a bad
>> alignment. Is there any way to make them both centered or justified to
>> either side of the column? Thanks in advance.
> 
> 
> 
> if the label is off to the right a bit, add this line
> slider.setAlignmentX(0);
> but it still might not suit, as the label will appear a bit left of the start of the slider track
> 
> if you want them centered, add both lines
> slider.setAlignmentX(.5f);
> label.setAlignmentX(.5f);

There are some constants that I find handy for this:
   slider.setAlignmentX(Component.CENTER_ALIGNMENT);
   label.setAlignmentX(Component.CENTER_ALIGNMENT);

If I need to justify a large number of components, I use a for loop:
   for(JComponent c: new JComponent[]
           { slider, label, this, that, other })
       c.setAlignmentX(Component.CENTER_ALIGNMENT);

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.gui


csiph-web