javaswingactionlistenereventhandlerchangelistener

Java Swing: JButtons and JSliders will not work individually


I'm relatively new to making UIs with Swing in Java, and I'm having trouble getting buttons and sliders to work in my project. I was trying to test that my sliders and buttons worked with prints, but found that they wouldn't work.

I will get results if I don't check what slider/button was interacted with. I.E. I will only get results if ANY slider or button in the project is pressed. But obviously I want them all to to do different things.

Here is my code so far

Main.java

//Imports here, just saving space

public class Main
{
    public static void main(String[] args) throws IOException
    {
        ImageProcessor e = new ImageProcessor();
        e.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        e.initGUI();
    }
}

ImageProcessor.java

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

public class ImageProcessor extends JFrame
{

    JPanel          imagePane;
    JCheckBox       xView, yView, zView, mip;
    JLabel          xSliceLabel, ySliceLabel, zSliceLabel;
    JSlider         xSliceSlider, ySliceSlider, zSliceSlider;
    JLabel          xImageIcon, yImageIcon, zImageIconl;
    BufferedImage   xImage, yImage, zImage;
    short           cthead[][][];
    short           min, max;
    double          ai, aj, ak;
    JButton         test;


    public void initGUI()
    {
        // Build the window to contain everything
        setTitle("Simple Example");
        setSize(1200, 900);

        /////// MAKE THE GUI ///////


        // Container Window
        Container container = getContentPane();
        container.setLayout(null);


        // Check boxes to select view
        JCheckBox xView = new JCheckBox("X View");
        xView.setBounds(150, 45, 65, 20);
        container.add(xView);

        JCheckBox yView = new JCheckBox("Y View");
        yView.setBounds(225, 45, 65, 20);
        container.add(yView);

        JCheckBox zView = new JCheckBox("Z View");
        zView.setBounds(300, 45, 65, 20);
        container.add(zView);


        // Panel to show CT Head image
        JPanel imagePane = new JPanel();
        imagePane.setBounds(25, 75, 512, 512);
        imagePane.setBorder(BorderFactory.createTitledBorder("Image View"));
        container.add(imagePane);

        // Image labels to display BufferedImages
        JLabel xImageIcon = new JLabel();
        imagePane.add(xImageIcon);



        // Checkbox for MIP on/off
        JCheckBox mip = new JCheckBox("MIP");
        mip.setBounds(125, 630, 65, 20);
        container.add(mip);


        // Slider labels
        JLabel xSliceLabel = new JLabel("X Slice");
        xSliceLabel.setBounds(50, 675, 60, 25);
        container.add(xSliceLabel);

        JLabel ySliceLabel = new JLabel("Y Slice");
        ySliceLabel.setBounds(50, 725, 60, 25);
        container.add(ySliceLabel);

        JLabel zSliceLabel = new JLabel("Z Slice");
        zSliceLabel.setBounds(50, 775, 60, 25);
        container.add(zSliceLabel);


        // Slice sliders
        JSlider xSliceSlider = new JSlider(0, 255, 76);
        xSliceSlider.setBounds(125, 675, 256, 25);
        container.add(xSliceSlider);

        JSlider ySliceSlider = new JSlider(0, 255, 76);
        ySliceSlider.setBounds(125, 725, 256, 25);
        container.add(ySliceSlider);

        JSlider zSliceSlider = new JSlider(0, 112, 76);
        zSliceSlider.setBounds(125, 775, 256, 25);
        container.add(zSliceSlider);

        JButton test = new JButton("Test");
        test.setBounds(50, 700, 60, 25);
        container.add(test);

        // Handler class
        GUIEventHandler handler = new GUIEventHandler();

        xSliceSlider.addChangeListener(handler);
        ySliceSlider.addChangeListener(handler);
        zSliceSlider.addChangeListener(handler);

        test.addActionListener(handler);


        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);

    }

    private class GUIEventHandler implements ActionListener, ChangeListener
    {
        public void stateChanged(ChangeEvent e)
        {
            if (e.getSource() == xSliceSlider)
            {
                System.out.println("x Slider changed!");

            } else if (e.getSource() == ySliceSlider)
            {
                System.out.println("y Slice changed!");

            } else if (e.getSource() == zSliceSlider)
            {
                System.out.println("Z slice changed!");
            }

            //This will work if any of the sliders change state
            //System.out.println("Something changed");
        }

        public void actionPerformed(ActionEvent e)
        {
            if (e.getSource() == test)
            {
                System.out.println("Testing...");
            }

            //This will also work if any button is pressed
            //System.out.println("Testing again...");
        }
    }
}

I'd greatly appreciate anyone knowledgable looking at this and pointing me in the right direction.

Thanks a lot, everyone!


Solution

  • You defined:

    In the handler, you access fields, but they are never initialized!

    In initGUI, rewrite some lines:

    JSlider xSliceSlider = new JSlider(0, 255, 76); // define a local variable
    

    as:

    xSliceSlider = new JSlider(0, 255, 76); // initialize the field