¿Cómo puedo obtener un jPanel que está dentro de un JtabbedPane?

I'm developing a Image Editor in java, i inserted a JPanel inside a JTabbedPane, to create a JTabbedPane with tabs that rapresent the Image Filter i want to apply to the image, i don't know how to obtain a The inner JPanel of a TabbedPane to decide witch filter is selected, because when i do " jTabbedPane1.getSelectedComponent(); " i can obtain only a component not a JComponent..

anyone knows something, sorry for my english thank's a lot..

Esto es parte de mi código:

package javaapplication22;

import javax.swing.JPanel;

/**
 *
 * @author iDoc
 */
public abstract class FilterTab extends JPanel {

    public FilterTab() {

    }

    protected void ApplyFilter() {

    }

    protected void ResetFilter() {

    }

}

public class InvertFilterTab extends FilterTab {

    private InvertFilter filter;

    public InvertFilterTab ()
    {
        filter = new InvertFilter();
    }

    /**
     *
     * @param originalImage
     * @param modifiedImage
     */
    public void ApplyFilter(BufferedImage originalImage, BufferedImage modifiedImage) {
        modifiedImage = filter.filter(originalImage, modifiedImage);
    }

    @Override
    public void ResetFilter() {

    }

}

private void button1ActionPerformed(java.awt.event.ActionEvent evt) {                                        

    BufferedImage originalImage = jOriginalPanel.getImage();
  //  BufferedImage modifiedImage = new BufferedImage(
    //        originalImage.getWidth(), originalImage.getHeight(), 
      //      BufferedImage.TYPE_INT_ARGB);
    //modifiedImage = 

    FilterTab ft = jTabbedPane1.getSelectedComponent();   <--- the problem is here

            filter.filter(originalImage, modifiedImage);

    jModifiedPanel.changeImage(modifiedImage);
    jModifiedPanel.repaint();

preguntado el 12 de junio de 14 a las 09:06

If you know that you've only added JPanels to your JTabbedPane, you can cast the component to a JPanel. Like: JPanel selected = (JPanel)jTabbedPane1.getSelectedComponent(); -

The easiest way is to keep fields holding instances of your components at the class level, so you can get them when necessary. -

1 Respuestas

Simply cast the result ...

FilterTab tab = (FilterTab)jTabbedPane1.getSelectedComponent();

Recuerda, JPanel se extiende desde JComponent, que se extiende desde Container, que se extiende desde Component

Respondido el 12 de junio de 14 a las 09:06

OK first of all thanks for reply, with casting i don't have the message : incompatible types: Component cannot be converted to FIlterTab but with the next call to filter.filter(originalImage, modifiedImage); i obtain the error: cannot find variable: filter have you got some ideas? - IDOC

No debería ser ft.filter(...) - Programador loco

i'm seeing now the the cast don't work, i get this: Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JPanel cannot be cast to javaapplication22.FilterTab at javaapplication22.GUI.button1ActionPerformed(GUI.java:350) at javaapplication22.GUI.access$000(GUI.java:23) - IDOC

Then what ever you added as the tab component wasn't a FilterTab, without more context, you'll going to have to go back through your code and find what your actually adding to the JTabbedPane - Programador loco

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.