File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to resize a JSlider

 
xiaoyi hou
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i tried the code below:
final JSlider lengthSlider = new JSlider(amountModel);
lengthSlider.setSize(500, 10);
but it doesn't work.
any ideas?

thanks
X HOU
 
Rita Williams
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is lengthSlider final?
 
xiaoyi hou
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i removed "final", still not working.
 
Geoff Griffiths
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try setting the values as varaibles ie int a = 50 etc and using these in the setSize statement . also try printing the value from getSize before and after to see if it changes. I'll have another look at this when I get home and let you know if I find anything.
Geoff
 
John Warland
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean make the slider bigger or smaller on the screen or
change the number of positions that you can move the slider tab too.
If its the earlier then then you need to read up on layoutManagers and positioning components. I can help here as Ive just been studying them.
 
xiaoyi hou
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think i mean make the jslider longer on the screnn. because if the range of the jslider is set to 0 - 1000, it's very difficult to pick a value from it.
please show me how you did it.
thanks
X HOU
 
xiaoyi hou
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i tried:
int x = 300, y = 30;
slider.setSize(x, y);
still not working
X HOU
 
xiaoyi hou
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i noticed there is another setSize() method which takes a Dimension as parameter, i tried it as below, not working either
Dimension d = new Dimension(300, 30);
jslider.setSize(d);
X HOU
 
ken chou
Ranch Hand
Posts: 68
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

After you call the setSize() function, if you add the JSlider to another container which has a Layout Manager(could be default). That Layout Manager may control over what you were trying to set for the size.
 
xiaoyi hou
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your reply.
i tried adding the jslider to a JPanel, and using gridlayout lay this and another 3 jpanel. still not working.
X HOU
 
ken chou
Ranch Hand
Posts: 68
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Go to the following website: http://mindprod.com/gotchas.html#SETLOCATION
Look for the subject:
"broken" setLocation, setSize, setBackground, setForeground
 
John Warland
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An example using a GridBagLayout:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GridBag extends Frame {
// Constructor
public GridBag() {
super("GridBag");
this.addWindowListener(new CloseWindow());
// Create 3 panels.
// One to hold Display and Back, CE and C buttons.
PanelleftPanel, rightPanel, topPanel;
leftPanel = new Panel();
rightPanel = new Panel();
topPanel = new Panel();
leftPanel.setBackground(Color.green);
rightPanel.setBackground(Color.red);
topPanel.setBackground(Color.blue);
// Set up a GridBagConstraint for each Panel
/* Panels laid out to look like this....
|---------------------------------------------|
| |
| Top Panel |
|---------------------------------------------|
| | |
| | |
| | Right Panel |
| Left | |
| Panel | |
| | |
| | |
-----------------------------------------------
*/
GridBagLayout gbl = new GridBagLayout();
setLayout(gbl);
GridBagConstraintsgbc_leftPanel,
gbc_rightPanel,
gbc_topPanel;

gbc_leftPanel = new GridBagConstraints();
gbc_rightPanel = new GridBagConstraints();
gbc_topPanel = new GridBagConstraints();
// Constraints for topPanel
gbc_topPanel.gridx = 0; // Top row
gbc_topPanel.gridy = 0; // First cell
gbc_topPanel.gridwidth = 2; // Span two cells
gbc_topPanel.gridheight = 1;
gbc_topPanel.fill = GridBagConstraints.BOTH;
gbc_topPanel.weightx = 10.0;
gbc_topPanel.weighty = 30.0;
// Constraints for 'bottom' leftPanel
gbc_leftPanel.gridx = 0; // First cell
gbc_leftPanel.gridy = 1; // Second row down
gbc_leftPanel.gridwidth = 1;
gbc_leftPanel.gridheight = 1;
gbc_leftPanel.fill = GridBagConstraints.BOTH;
gbc_leftPanel.weightx = 20.0;// Left panel takes up less room.
gbc_leftPanel.weighty = 70.0;

// Constraints for 'bottom' rightPanel
gbc_rightPanel.gridx = 1; // Second cell
gbc_rightPanel.gridy = 1; // Second row down
gbc_rightPanel.gridwidth = GridBagConstraints.RELATIVE;
gbc_rightPanel.gridheight = 1;
gbc_rightPanel.fill = GridBagConstraints.BOTH;
gbc_rightPanel.weightx = 80.0;// Right Panel takes up most room
gbc_rightPanel.weighty = 70.0;
// Leave a small gap between bottom left panel and bottom right panel
gbc_rightPanel.insets = new Insets(0,4,0,0);

gbl.setConstraints(topPanel, gbc_topPanel);
gbl.setConstraints(leftPanel, gbc_leftPanel);
gbl.setConstraints(rightPanel, gbc_rightPanel);
// Now we have our 3 panels in place. Populate them with
// components etc...

// **** TOP PANEL FIRST ****
topPanel.add(new Button("This is in the top panel!"));
// ****** BOTTOM LEFT PANEL ******
// Add 4 buttons to the bottom left panel.
leftPanel.setLayout(new GridLayout(4,1,3,3)); // 4 rows, 1 column, spacing
leftPanel.add(new Button(" A "));
leftPanel.add(new Button(" B "));
leftPanel.add(new Button(" C "));
leftPanel.add(new Button(" D "));

// ****** BOTTOM RIGHT PANEL ******
rightPanel.setLayout(new BorderLayout());
JSliders = new JSlider();
rightPanel.add(s, BorderLayout.NORTH);
// Add the Panels to the Frame.
add(topPanel);
add(leftPanel);
add(rightPanel);
}
// Create a window listener so can exit by clicking on close button
class CloseWindow extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
};
}
public static void main(String[] args) {
GridBaggb = new GridBag();
gb.setBounds(50,50, 280,280);
gb.setResizable(true);
gb.setVisible(true);
}
}
Hope this helps.
Regards,
John.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic