0

So... I have like 1 month since I started programming, so please don´t get mad if my mistake is in something basic. Thanks

I only have to copy an already written code, but the line I marked has some errors.

Error:

Multiple markers at this line - The serializable class SliderCbx does not declare a static final serialVersionUID field of type long

Code:

package cnad.edu.mx;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSlider;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class SliderCbx extends JFrame {

        private JSlider bgColorSlider;
        private JLabel lblValSlider;
        private JLabel lblCnad;
        private JPanel panLbl;
        private Font font;
        private JRadioButton rbopFont;
        private JRadioButton rbopFondo;
        private Boolean selectorColor = true;
        private ButtonGroup gpoColor;

        public SliderCbx () {

            super("Slider");
            setLayout(new FlowLayout());
            lblValSlider = new JLabel ("10");
            lblCnad = new JLabel ("CNAD");
            panLbl = new JPanel();
            bgColorSlider = new JSlider(SwingConstants.HORIZONTAL, 10, 255, 20);
            rbopFont = new JRadioButton("Tamaño de letra", false);
            rbopFondo = new JRadioButton("Color de fondo", false);
            rbopFondo.addItemListener(new radioButtonHandler());
            rbopFont.addItemListener(new radioButtonHandler());
            gpoColor = new ButtonGroup();
            font = new Font("Serif", Font.ITALIC, 40);
            bgColorSlider.setMajorTickSpacing(10); // Crea marcas o tick cada 20
            bgColorSlider.setPaintTicks(true); // Por defecto se encuentran en false
            lblCnad.setFont(font);

            bgColorSlider.addChangeListener(new ChangeListener() { // Clase anónima

                    public void stateChanged(ChangeEvent e) {
                        String val = Integer.toString(bgColorSlider.getValue());
                        lblValSlider.setText(val);

                        // lblCnad.setFont(new Font("Serif", Font BOLD
                        // bgColorSlider.getValue()));
                        if (selectorColor == true) {
                            lblCnad.setForeground(new Color(200, 255, bgColorSlider.getValue()));
                        }
                    }
            });
        add(bgColorSlider);
        add(lblValSlider);
        add(rbopFont);
        add(rbopFondo);
        gpoColor.add(rbopFont); // Para que sean muamente excluyentes, se
                    // colocan en un grupo
        gpoColor.add(rbopFondo);
        add(panLbl);
        panLbl.add(lblCnad);
        }

        private class radioButtonHandler implements ItemListener { // Clase interna 
            @Override
            public void itemStateChanged(ItemEvent e) {
                if (e.getItem() == rbopFont)
                    selectorColor = true;
                else if (e.getItem()==rbopFondo)
                    selectorColor = false;      
        }
    }
}
Cebence
  • 2,406
  • 2
  • 19
  • 20
  • http://stackoverflow.com/questions/15045865/how-to-remove-the-warning-the-serializable-class-classname-does-not-declare-a-st could maybe help you – Felix Gerber Nov 28 '16 at 09:07

1 Answers1

0

This error is most likely brought up by your IDE, Eclipse I guess. It points to the fact, that every Serializable class should provide a serialVersionUID which is used to check version compatibility between serialized content and deserializing class.

This often happens in Swing project because most Swing classes are realized as serializable, but serialization is not used at all in most cases.

If your case does not include serializing/deserializing, the solution is changing the Error/Warning settings in your Eclipse Project for serialVersionUID issue to Warning.

Another way is using the QuickFix of Eclipse or the @SuppressWarnings("serial")annotation. Both is available from the context-menu.

By the way, try compiling your project from command line using javac. You will not get an error there because this issue is insisted by the IDE. It wants to prevent you from getting runtime errors when really adding some serialization/deserialization stuff to your project. In most Swing cases it is most pragmatic to just downgrade the severity of the problem category.

Alexander
  • 1,356
  • 9
  • 16