how to save slider value and move to new question?

G

Geoff Cox

Hello

The code below displays an image and allows a user to answer a related
question by moving a slider. When the program is stopped (window
closed) the slider value is saved to a file.

I would like to develop this so that when the user is happy with the
slider postion he/she clicks a button which

1. saves the slider value to the file, with the question number
2. displays another image with a new question
3. the slider is used to answer this new question etc etc

I would appreciate any pointers on how to do this!

Thanks

Geoff


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

class Changes2 extends JFrame implements ChangeListener
{
JSlider scale = new JSlider(-20,+20,0);
JLabel position = new JLabel("Set Position");

public Changes2()
{

super("Slider");
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);

Container contentArea = getContentPane();
GridLayout lay = new GridLayout(3,1);
contentArea.setLayout(lay);

scale.setMajorTickSpacing(10);
scale.setMinorTickSpacing(5);
scale.setPaintTicks(true);
scale.setPaintLabels(true);

scale.addChangeListener(this);

JLabel text = new JLabel("Look at the picture and indicate
on the slider how you feel.");

ImageIcon picicon = new ImageIcon("pic1.jpg");
JButton buttonpic = new JButton(picicon);

JPanel panel1 = new JPanel();
panel1.setBackground(Color.blue);
panel1.add(buttonpic);

JPanel panel2 = new JPanel();
panel2.setBackground(Color.white);
panel2.add(text);

JPanel panel3 = new JPanel();
panel3.setBackground(Color.blue);
panel3.add(scale);

contentArea.add(panel1);
contentArea.add(panel2);
contentArea.add(panel3);

setContentPane(contentArea);
}

public void stateChanged(ChangeEvent event)
{
JSlider src = (JSlider) event.getSource();
if(!src.getValueIsAdjusting())
position.setText("Position is "+scale.getValue());

try
{
FileWriter outfile = new FileWriter ("data.txt");
PrintWriter pw = new PrintWriter(outfile);
pw.println(scale.getValue());
pw.close();
}
catch (IOException e) { System.out.println(e); }

}

public static void main(String[] args)
{
Changes2 eg = new Changes2();
}
}
 
K

Knute Johnson

Geoff said:
Hello

The code below displays an image and allows a user to answer a related
question by moving a slider. When the program is stopped (window
closed) the slider value is saved to a file.

I would like to develop this so that when the user is happy with the
slider postion he/she clicks a button which

1. saves the slider value to the file, with the question number
2. displays another image with a new question
3. the slider is used to answer this new question etc etc

I would appreciate any pointers on how to do this!

Thanks

Geoff

Geoff:

You need to add an ActionListener to your JButton. Write your data to
the file there, change your JLabel text, and display a new image. You
will probably need to extend JPanel to display your images.
 
R

Raymond DeCampo

Geoff said:
Hello

The code below displays an image and allows a user to answer a related
question by moving a slider. When the program is stopped (window
closed) the slider value is saved to a file.

I would like to develop this so that when the user is happy with the
slider postion he/she clicks a button which

1. saves the slider value to the file, with the question number
2. displays another image with a new question
3. the slider is used to answer this new question etc etc

I would appreciate any pointers on how to do this!

How did you get this far without being able to at least start
implementing this?

1. You already have code that saves the slider value, just add in the
question number.
2. Change the icon on your JButton you are using to display the image
(odd choice, I would have gone with JLabel).
3. Again, you already have the basics.

HTH,
Ray
 
G

Geoff Cox

How did you get this far without being able to at least start
implementing this?

Raymond,

By looking at other code and building up what I wanted ... plus
reading the Java in 21 days book - a slow process.

Thanks for the hints.

Cheers

Geoff
 
G

Geoff Cox

You need to add an ActionListener to your JButton. Write your data to
the file there, change your JLabel text, and display a new image. You
will probably need to extend JPanel to display your images.

Thanks Knute - will work on this.

Cheers

Geoff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,246
Members
46,840
Latest member
BrendanG78

Latest Threads

Top