I reduced my sketch down to just a few swing inputs like one slider, one button, one textfield and gave it another try. I almost have it. I can move the slider and the textfield changes and shows a double. I can also edit the textfield. Edit: Whoops, now I can do both if I use a button to do it. Press the button after entering text and the slider updates.
Edit: Forgot to update the code. And, to use it, enter the text by typing and hitting enter. Then hit the GHA button. Or, move the slider and then hit enter in the textfield or it causes a nullpointer. Also, if you hit GHA and it prints a value in radians, then hit GHA again. Its not perfect, but it is a start.
Here’s the reduced code:
//Generated by GuiGenie - Copyright (c) 2004 Mario Awad.
//Home Page http://guigenie.cjb.net - Check often for new versions!
//import java.awt.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.text.DecimalFormat;
MyPanel controlPanel;
public String text;
double GHA = Math.toRadians(-51.16667f); //(-128.83333f);
double GHA1 = Math.toRadians(51.16667f);
//double alt = Math.toRadians(62.6f);
int timer=0;
boolean Star1;
public class MyPanel extends JPanel {
private TextDemo jcomp1;
private JLabel jcomp2;
private JButton jcomp3;
//private JButton jcomp9;
private JButton jcomp4;
private JSlider jcomp5;
public MyPanel() {
//construct components
jcomp1 = new TextDemo(); //JTextArea (1, 16);
jcomp2 = new JLabel ("GHA : DDD.MMSS");
jcomp3 = new JButton ("GHA");
jcomp4 = new JButton("Star1");
jcomp5 = new JSlider(0,360,(int)(Math.toDegrees(-GHA))); //,(int)Math.toDegrees(GHA));
jcomp3.addActionListener(new Button1Click());
jcomp4.addActionListener(new Button2Click());
jcomp5.addChangeListener(new HSlider1Change());
jcomp5.setOrientation (JSlider.HORIZONTAL);
jcomp5.setMinorTickSpacing (10);
jcomp5.setMajorTickSpacing (60);
jcomp5.setPaintTicks (true);
jcomp5.setPaintLabels (true);
//adjust size and set layout
setPreferredSize (new Dimension (544, 756));
setLayout (null);
//add components
add (jcomp1);
add (jcomp2);
add (jcomp3);
add (jcomp4);
add (jcomp5);
//set component bounds (only needed by Absolute Positioning)
jcomp1.setBounds (70, 35, 205, 25);
jcomp2.setBounds (285, 35, 175, 25);
jcomp3.setBounds (70, 80, 100, 25);
jcomp4.setBounds (70,320,100,25);
jcomp5.setBounds(70,420,400,50);
}
class Button1Click implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JButton b = (JButton)e.getSource();
GHA = Double.parseDouble(text);
jcomp5.setValue((int)Double.parseDouble(text));
jcomp1.textField.setText(text);
println("GHA: " + GHA);
timer=millis()+1000;
}
}
class Button2Click implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JButton b = (JButton)e.getSource();
GHA1 = GHA;
println("GHA1: " + GHA1);
// dec1 = dec;
// alt1 = alt;
// Globe = true;
// Map = false;
Star1 = true;
timer=millis()+1000;
}
}
class HSlider1Change implements ChangeListener
{
public void stateChanged(ChangeEvent e)
{
JSlider source = (JSlider)e.getSource();
if (source.getValue() < 180){
GHA = radians( (- source.getValue()));
jcomp1.textField.setText(Double.toString(Math.toDegrees(-GHA)));
}
if (source.getValue() > 180) {
GHA = radians((360 - source.getValue()));
jcomp1.textField.setText(Double.toString(360-(Math.toDegrees(GHA))));
}
timer=millis()+1000;
println("slider: " + source.getValue() + " GHA: " + Math.toDegrees(GHA));
}
}
}
void setup()
{
JFrame frame =new JFrame("Controls");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
controlPanel = new MyPanel();
controlPanel.setOpaque(true); //content panes must be opaque
frame.setContentPane(controlPanel);
//frame.add(new TextDemo());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public class TextDemo extends JPanel implements ActionListener {
protected JTextField textField;
protected JTextArea textArea;
private final static String newline = "\n";
public TextDemo() {
textField = new JTextField(20);
textField.addActionListener(this);
textField.setHorizontalAlignment(textField.CENTER);
textArea = new JTextArea(2, 20);
textArea.setEditable(false);
add(textField);
add(textArea);
}
public void actionPerformed(ActionEvent evt) {
text = textField.getText();
textArea.append(text + newline);
textField.selectAll();
GHA = Double.parseDouble(text);
textField.setText(Double.toString(GHA));
println(GHA);
//Make sure the new text is visible, even if there
//was a selection in the text area.
}
}