Re: how to set (preferred) JSlider length?



Mark_Galeck wrote:
Thank you all for your replies, I really appreciate, but... none of
these suggestions work - any calls to setPreferredSize,
setMinimumSize, maximumSize, none of these have any effect, with most
layout managers, they just ignore them

the solution, I found out from an article written by some guru:

To set size of your component and still work with a layout manager:

write your own subclass , and override getPreferredSize, which is what
the layout manager calls.

(He chastises Sun for misleading and lack of documentation. )


Thank you again for your comments.

I'll admit Sun's docs are not that good for telling you how things really work. But setting preferred size works fine just as I showed you in the example provided. Most of us stopped overriding getPreferredSize() years ago because setting it works just fine and we didn't need the extra code. Java is changing every day and while the web has some great articles, check the date on them. If they are more than a couple of years old there may well be new thought on the subject.

Usually problems with layoutmanagers come in two varieties, you have selected the wrong layoutmanager or you are trying to control the exact size of your component with a layoutmanager. Unending grief will attach to both situations. The purpose of the layoutmanager is to help you make things fit and to compensate for differences in font sizes, native component sizes and multi-platform quirks. They work great if you let them.

I've converted the code to an applet for you, try it, I guarantee you it will work. Try playing with the width argument in the <applet> tag. If you set it to less than 400 the JSlider will be 200 pixels wide.

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

public class test9 extends JApplet {
public void init() {
EventQueue.invokeLater(new Runnable() {
public void run() {
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();

JSlider s = new JSlider(0,400);
Dimension d = s.getPreferredSize();
d.width = 400;
s.setPreferredSize(d);
s.setMinimumSize(new Dimension(200,d.height));

add(s,c);
}
});
}
}

<html>
<head>
</head>
<body>
<applet code=test9.class width=450 height=200>
</applet>
</body>
</html>

--

Knute Johnson
email s/nospam/knute2008/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
.



Relevant Pages

  • Re: easy dialog resizing
    ... The layout controls in Whidbey are a bit better, ... > "Justin Rogers" wrote in message ... >> the layout manager to ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: suitable layout manager?
    ... what layout manager is the ...
    (comp.lang.java.gui)
  • Re: GetKeyState() problem
    ... especially for the toggle keys. ... > No, no layout manager stuff. ... >> input system's understanding of these modifier flags. ...
    (microsoft.public.windowsce.platbuilder)
  • Re: GridBagLayout
    ... Is it possible to use at GridBagLayout with a fixed / predifined no. ... allow the components to expand to the available space in the layout. ... Posted via NewsDemon.com - Premium Uncensored Newsgroup Service ... Unlimited Access, Anonymous Accounts, Uncensored Broadband Access ...
    (comp.lang.java.programmer)
  • Re: [handling data](1)how to go handle panel objects for edition inside a panel container
    ... John B. Matthews wrote: ... I am wary of absolute layouts; I tend to prefer a layout that "breathes" ... @author John B. Matthews ... public void componentResized{ ...
    (comp.lang.java.help)