Logical error with the StyledEditorKit.ForegroundAction class in Java

1

I'm doing a text editor, it turns out that I'm using the class StyledEditorKit.ForegroundAction to be able to set the color to the selected text, the question is that when I call the class from the method actionPerformed the text is not manipulated, but if I do it from the outside if it is manipulated.

Then I'll leave the code and put a comment in the part where the logical error is occurring:

public class LaminaTexto extends JPanel implements ActionListener{

    public LaminaTexto(){

        setLayout(new BorderLayout());

        JMenuBar miBar=new JMenuBar();

        JMenu archivo=new JMenu("Archivo");

        JMenu fuentes=new JMenu("Fuentes");

        JMenu estilo=new JMenu("Estilo");

        JMenu tamaño=new JMenu("Tamaño");

        JMenu colores=new JMenu("Colores");

        azul=new JMenuItem("Azul");

        rojo=new JMenuItem("Rojo");

        amarillo=new JMenuItem("Amarillo");

        mas=new JMenuItem("Mas...");

        nuevo=new JMenuItem("Nuevo");

        abrir=new JMenuItem("Abrir");

        guardar=new JMenuItem("Guardar");

        guardarC=new JMenuItem("Guardar como...");

        salir=new JMenuItem("Salir");

         arial=new JMenuItem("Arial");

         courier=new JMenuItem("Currier");

         verdana=new JMenuItem("Verdana");

         negrita=new JMenuItem("Negrita");

         cursiva=new JMenuItem("Cursiva");

        archivo.add(nuevo).addActionListener(this);

        archivo.add(abrir).addActionListener(this);

        archivo.add(guardar).addActionListener(this);

        archivo.add(guardarC).addActionListener(this);

        archivo.add(salir);

        fuentes.add(arial).addActionListener(new StyledEditorKit.FontFamilyAction("Hola", fuente="Arial"));

        fuentes.add(courier).addActionListener(new StyledEditorKit.FontFamilyAction("Hola", fuente="Courier"));

        fuentes.add(verdana).addActionListener(new StyledEditorKit.FontFamilyAction("Hola", fuente="Verdana"));

        colores.add(azul).addActionListener(new StyledEditorKit.ForegroundAction("hola", c=Color.blue));

        colores.add(rojo).addActionListener(new StyledEditorKit.ForegroundAction("hola", c=Color.red));

        colores.add(amarillo).addActionListener(new StyledEditorKit.ForegroundAction("hola", c=Color.yellow));




        //************************************AQUI ES DONDE AGREGO EL JMenuItem Y EL EVENTO**********


        colores.add(mas).addActionListener(this);



        //***************************************************************************



        miBar.add(archivo);

        miBar.add(fuentes);

        miBar.add(estilo);

        miBar.add(tamaño);

        miBar.add(colores);

        add(miBar, BorderLayout.NORTH);

        JPanel lamina2_texto=new JPanel();

        JScrollPane p=new JScrollPane(lamina2_texto);

        lamina2_texto.setLayout(new BorderLayout());

        add(p);

        texto=new JTextPane();

        lamina2_texto.add(texto, BorderLayout.CENTER);

        JCheckBoxMenuItem negrita=new JCheckBoxMenuItem("Negrita");

        JCheckBoxMenuItem cursiva=new JCheckBoxMenuItem("Cursiva");

        JRadioButtonMenuItem doce=new  JRadioButtonMenuItem("12");

        JRadioButtonMenuItem diez_seis=new  JRadioButtonMenuItem("16");

        JRadioButtonMenuItem diez_ocho=new  JRadioButtonMenuItem("18");

        JRadioButtonMenuItem veinte=new  JRadioButtonMenuItem("20");

        JRadioButtonMenuItem veinte_cuatro=new  JRadioButtonMenuItem("24");

        ButtonGroup miGrupo=new ButtonGroup();

        miGrupo.add(doce);

        miGrupo.add(diez_seis);

        miGrupo.add(diez_ocho);

        miGrupo.add(veinte);

        miGrupo.add(veinte_cuatro);

        tamaño.add(doce);

        tamaño.add(diez_seis);

        tamaño.add(diez_ocho);

        tamaño.add(veinte);

        tamaño.add(veinte_cuatro);

        estilo.add(negrita);

        estilo.add(cursiva);

        //----------------------------

        negrita.addActionListener(a);

       cursiva.addActionListener(a2);

        doce.addActionListener(new StyledEditorKit.FontSizeAction("Hola", tamagno=12));

        diez_seis.addActionListener(new StyledEditorKit.FontSizeAction("Hola", tamagno=16));

        diez_ocho.addActionListener(new StyledEditorKit.FontSizeAction("Hola", tamagno=18));

        veinte.addActionListener(new StyledEditorKit.FontSizeAction("Hola", tamagno=20));

       veinte_cuatro.addActionListener(new StyledEditorKit.FontSizeAction("Hola", tamagno=24));



        salir.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }   
        });


    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if(e.getActionCommand().equals("Abrir")){
            select.showOpenDialog(select);
            if(select.getSelectedFile()!=null){
            archivo=select.getSelectedFile();    
            abrirArchivo(archivo);
            }
        }else if(e.getActionCommand().equals("Guardar como...")){

            select.showOpenDialog(select);
            if(select.getSelectedFile()!=null){
            archivo=select.getSelectedFile();    
            guardarArchivo(archivo);

            }
        }else if(e.getActionCommand().equals("Guardar")){

            if(archivo!=null){
                guardarArchivo(archivo);

            }else{
                JOptionPane.showMessageDialog(this, "Ha ocurrido un error", "Error", 0);
            }

            }else if(e.getActionCommand().equals("Nuevo")){
                archivo=null;
                texto.setText("");
                documento="";

                //**********************************************AQUI ES DONDE ESTA OCURRIENDO EL ERROR LOGICO
            }else if(e.getActionCommand().equals("Mas...")){
              /*-**********aQUI PRETENDO USAR UN JColorChooser para poder selecionar cualquier
              color y poder agregarlo a texto selecionado*/


                c=JColorChooser.showDialog(this, "Elegir color", c);

                //*********AQUI ES DONDE LLAMO AL CONTRUCTOR, PERO NO LO MANIPULA EL TEXTO SELECIONADO
                new StyledEditorKit.ForegroundAction("hola", c);
            }  


        //********************************************************************
    }

    public void abrirArchivo(File archivo){

        try {
            entrada=new FileInputStream(archivo);
            int a;
            while((a=entrada.read())!=-1){
               char c=(char)a;
               documento+=c;

            }
        } catch (Exception e) {
        }
        texto.setText(documento);
    }



    public void guardarArchivo(File archivo){ 

        try {
            documento=texto.getText();
            salida=new FileOutputStream(archivo);
            byte[] b=documento.getBytes();
            salida.write(b);
        } catch (Exception e) {
        }
    }


    private String documento="";

    private static String fuente="";

    private static Color c;

    private static int tamagno=12;

    private static final StyledEditorKit.StyledTextAction a=new StyledEditorKit.BoldAction();

    private static final StyledEditorKit.StyledTextAction a2=new StyledEditorKit.ItalicAction();

    private JTextPane texto;

    private JMenuItem arial, courier, verdana, negrita, cursiva, doce, diez_seis, diez_ocho, veinte, veinte_cuatro, abrir, guardar, guardarC, salir, nuevo, azul, rojo, amarillo,mas;

    private JFileChooser select=new JFileChooser();
  private  File archivo;
  private FileInputStream entrada; 
  private FileOutputStream salida;

}
    
asked by edward1499 26.07.2018 в 17:23
source

0 answers