The Artima Developer Community
Sponsored Link

Java Answers Forum
My JSlider does not work

3 replies on 1 page. Most recent reply: Apr 11, 2005 11:09 PM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Rama

Posts: 4
Nickname: rush88
Registered: Apr, 2005

My JSlider does not work Posted: Apr 10, 2005 8:09 PM
Reply to this message Reply
Advertisement
I am to write a program that uses the paintComponent method to draw the current value of a JSlider on subclass of JPanel. in addition provide JTextField where specific value can be entered. JTextField should display the current value of the JSlider at all times. JLabel should be used to identify the JTextField. the JSlider methods setValue and getValue should be used.

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

public class Slider extends JPanel {
private JSlider diameterSlider;
private OvalPanel myPanel;
private JTextArea displayArea;
//private int diameter = 10;

public Slider() {

super("Slider");
myPanel = new Ovalpanel();
myPanel.setBackground(Color.BLUE);

JLabel AreaLabel = new JLabel("ENTER VALUE");
displayArea = new JTextArea(2,3);

//g.fillOval(10,10,diameter,diameter);

}
diameterSlider =
new JSlider(SwingConstants.HORIZONTAL,0,200,10);
diameterSlider.setMajorTickSpacing(10);
diameterSlider.setPaintTicks(true);

diameterSlider.addChangeListener(new ChangeListener() {

public void setDiameter(int newDiameter)
{
diameter = (newDiameter >= 0?newDiameter:10);
//repaint();
myPanel.setDiameter(diameterSlider.getValue());
}
public Dimension getPreferredSize() {
return new Dimension(200,200);
}

public Dimension getMinimumSize() {
return getPreferredSize();
}
}
Container container = getContentPane();
container.add(diameterSlider,BorderLayout.SOUTH);
container.a dd(myPanel,BorderLayout.CENTER);

setSize(220,270);
setVisible(true);

public static void main(String args[]) {
Slider application = new Slider();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: My JSlider does not work Posted: Apr 11, 2005 12:27 AM
Reply to this message Reply
We don't do homework here.

What is your question?

Rama

Posts: 4
Nickname: rush88
Registered: Apr, 2005

Re: My JSlider does not work Posted: Apr 11, 2005 6:50 PM
Reply to this message Reply
my question will be....is it possible to give me directions or help me correct this program to do what it should be doing..please.
Thanks

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: My JSlider does not work Posted: Apr 11, 2005 11:09 PM
Reply to this message Reply
The code as posted can't even be compiled.
In this code there are several closing brackets missing. That could be a problem of this forum.
Repost the code, but this time use the java tag as shown on the right side of the input field.

Also post the code for OvalPanel, since I don't know what myPanel.setDiameter(diameterSlider.getValue()) does. I ca nonly assume that It filles a Textfield.

Now to some points I noticed:
1. You need to override the method
void 	stateChanged(ChangeEvent e)
in your ChangeListener. If you don't it won't do anything.

2. If this IS the code as you wrote it (and it's not the fault of the forum) then first you should put eerything into the constructor that belongs into it. You put a closing bracket after the "fillOval" line. I'm sure that does not belong there.
I suppose it belongs after setVisible(true), right?
2.1 addChangelistener must be inside the constructor.

3. getPreferredSize and getMinimumSize should not be inside the changelistener.

4. I don't know if setDiameter belongs to the changelistener, but I suppose not.

All of this points except point 1 are related to missing or additional brackets. If it's not the forums fault, you should pay more attention there. Brackets are the non-plus-ultra in programming. They mark what belongs together, so they are the first thing to look at.

Hint: Instead of asking "what is wrong here?" you should give some more informations: are there compile errors (where), does a method not get called or returns the wrong value or whatever.
Also a code without documentation is difficult to read for someone else than the author.
If you just don't know what to write, at least post the output you get from the compiler or running the program.

Flat View: This topic has 3 replies on 1 page
Topic: I need help with this problem...please Previous Topic   Next Topic Topic: Isolate/remove parts of a String

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use