IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

AWT/Swing Java Discussion :

Image de fond sur JScrollPane


Sujet :

AWT/Swing Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé Avatar de Jose.N70
    Homme Profil pro
    Développeur Java
    Inscrit en
    Mars 2009
    Messages
    162
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : Service public

    Informations forums :
    Inscription : Mars 2009
    Messages : 162
    Par défaut Image de fond sur JScrollPane
    Bonjour, je cherche une solution à un problème que je rencontre pour rendre "transparent" un JScrollPane.

    J'ai déjà cherché ( un peu ) sur le forum mais aucune réponse n'a résolu mon problème.

    Sur un JXImagePanel j'ai ajouté un JScrollPane qui lui contient un JEditorPane.

    L'idée est d'avoir une image de fond derrière le JEditorPane.

    Comment ai-je procédé :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    JScrollPane scrollPane = new JScrollPane(jEditorPane);
    		scrollPane .setOpaque(false);
    		scrollPane .getViewport().setOpaque(false);
    		add(scrollPane , BorderLayout.CENTER);
    Cependant avec ceci je ne vois pas mon image, le fond reste blanc.

    alors que si je supprime le JScrollPane, le JEditorPane apparait avec mon image en fond.

    ps : le jEditorPane.setOpaque(false) est bien indiqué

    -------

    Si j'ai bien compris le principe c'est le JViewPort qui contient mon JEditorPane. Je lui indique bien qu'il ne doit pas être opaque mais rien n'y fait.

    Merci à ceux et celles qui pourront m'aider

  2. #2
    Rédacteur/Modérateur

    Avatar de bouye
    Homme Profil pro
    Information Technologies Specialist (Scientific Computing)
    Inscrit en
    Août 2005
    Messages
    6 911
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49
    Localisation : Nouvelle-Calédonie

    Informations professionnelles :
    Activité : Information Technologies Specialist (Scientific Computing)
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Août 2005
    Messages : 6 911
    Billets dans le blog
    54
    Par défaut
    pas de problème pour moi :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package javaapplication2;
     
    import java.awt.BorderLayout;
    import java.awt.Color;
    import javax.swing.JEditorPane;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.SwingUtilities;
     
    /**
     *
     * @author Fabrice
     */
    public class Main {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
     
                /**
                 * {@inheritDoc}
                 */
                @Override
                public void run() {
                    JEditorPane jEditorPane = new JEditorPane();
                    jEditorPane.setOpaque(false);
                    JScrollPane scrollPane = new JScrollPane(jEditorPane);
                    scrollPane.setOpaque(false);
                    scrollPane.getViewport().setOpaque(false);
                    JPanel panel = new JPanel();
                    panel.setBackground(Color.CYAN);
                    panel.setLayout(new BorderLayout());
                    panel.add(scrollPane, BorderLayout.CENTER);
                    JFrame f = new JFrame("Test");
                    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    f.setSize(1000, 400);
                    f.setLayout(new BorderLayout());
                    f.add(panel, BorderLayout.CENTER);
                    f.setLocationRelativeTo(null);
                    f.setVisible(true);
                }
            });
        }
    }
    Nom : Sans titre3.png
Affichages : 118
Taille : 18,9 Ko

    Ca devrait fonctionner pareil avec un JXImagePanel à la place d'un JPanel.
    Merci de penser au tag quand une réponse a été apportée à votre question. Aucune réponse ne sera donnée à des messages privés portant sur des questions d'ordre technique. Les forums sont là pour que vous y postiez publiquement vos problèmes.

    suivez mon blog sur Développez.

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning. ~ Rich Cook

  3. #3
    Membre confirmé Avatar de Jose.N70
    Homme Profil pro
    Développeur Java
    Inscrit en
    Mars 2009
    Messages
    162
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : Service public

    Informations forums :
    Inscription : Mars 2009
    Messages : 162
    Par défaut
    Je vais essayer avec un JPanel au cas ou mais je doute ...

  4. #4
    Membre confirmé Avatar de Jose.N70
    Homme Profil pro
    Développeur Java
    Inscrit en
    Mars 2009
    Messages
    162
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : Service public

    Informations forums :
    Inscription : Mars 2009
    Messages : 162
    Par défaut
    Bon rien. Peut être une erreur grossière dans mon code ... le voici :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
     
    public class PanelInfo extends JXImagePanel {
     
    	private String texteInfo;
    	private JEditorPane jEditorPane = null;
     
    	public PanelInfo (String texte){
    		texteInfo = texte;
    		setLayout(new BorderLayout());
    		setImage(new ListeImages().donneImageNormale("image1"));
    		JScrollPane jScrollPane = new JScrollPane(getJEditorPane());
    		jScrollPane.setOpaque(false);
    		jScrollPane.getViewport().setOpaque(false);
    		add(jScrollPane, BorderLayout.CENTER);		
    	}
     
    //.....puis le Jeditorpane

  5. #5
    Rédacteur/Modérateur

    Avatar de bouye
    Homme Profil pro
    Information Technologies Specialist (Scientific Computing)
    Inscrit en
    Août 2005
    Messages
    6 911
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49
    Localisation : Nouvelle-Calédonie

    Informations professionnelles :
    Activité : Information Technologies Specialist (Scientific Computing)
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Août 2005
    Messages : 6 911
    Billets dans le blog
    54
    Par défaut
    Mise à part que je ne sais pas trop où le JXImagePanel place l'origine de son image, moi ça marche plutot bien, et ce quelque soit le mode d'affichage choisit :

    Nom : Sans titre2.jpg
Affichages : 112
Taille : 279,8 Ko

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package javaapplication2;
     
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.RenderingHints;
    import java.awt.font.LineMetrics;
    import java.awt.geom.Rectangle2D;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import javax.swing.JComponent;
    import javax.swing.JEditorPane;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JSplitPane;
    import javax.swing.SwingUtilities;
    import org.jdesktop.swingx.JXImagePanel;
     
    /**
     *
     * @author Fabrice
     */
    public class Main extends JComponent {
     
        public enum Variant {
     
            SWING,
            SWING_X;
        }
        private Variant variant;
     
        public Main(Variant variant) throws IOException {
            variant = (variant == null) ? Variant.SWING : variant;
            this.variant = variant;
            JEditorPane jEditorPane = new JEditorPane();
            jEditorPane.setForeground(Color.WHITE);
            jEditorPane.setOpaque(false);
            StringBuilder text = new StringBuilder();
            for (int i = 0; i < 30; i++) {
                text.append("BlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBla\n");
                text.append("BlaBlaBlaBlaBlaBlaBlaBlaBlaBla\n");
                text.append("\n");
            }
            jEditorPane.setText(text.toString());
            jEditorPane.setCaretPosition(0);
            JScrollPane scrollPane = new JScrollPane(jEditorPane);
            scrollPane.setOpaque(false);
            scrollPane.getViewport().setOpaque(false);
            JComponent container = null;
            final BufferedImage image = ImageIO.read(new File("C:\\Users\\Fabrice\\Pictures\\DAOrigins 2010-03-22 21-44-27-45.jpg"));
            switch (variant) {
                case SWING_X: {
                    JXImagePanel imagePanel = new JXImagePanel();
                    imagePanel.setImage(image);
                    container = imagePanel;
                }
                break;
                case SWING:
                default: {
                    JComponent panel = new JComponent() {
     
                        /**
                         * {@inheritDoc}
                         */
                        @Override
                        protected void paintComponent(Graphics g) {
                            super.paintComponent(g);
                            g.drawImage(image, 0, 0, null);
                        }
                    };
                    panel.setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));
                    container = panel;
                }
            }
            container.setLayout(new BorderLayout());
            container.add(scrollPane, BorderLayout.CENTER);
            setLayout(new BorderLayout());
            add(container, BorderLayout.CENTER);
        }
     
        /**
         * {@inheritDoc}
         */
        @Override
        public void paint(Graphics g) {
            super.paint(g);
            Graphics2D g2d = (Graphics2D) g;
            g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            Dimension size = getSize();
            String label = variant.toString();
            Font font = new Font("Dialog", Font.BOLD, 70);
            Rectangle2D stringBounds = font.getStringBounds(label, g2d.getFontRenderContext());
            LineMetrics lineMetrics = font.getLineMetrics(label, g2d.getFontRenderContext());
            g2d.setColor(Color.RED);
            g2d.setFont(font);
            g2d.drawString(label, (int) ((size.width - stringBounds.getWidth()) / 2.0), (int) (lineMetrics.getAscent() + (size.height - stringBounds.getHeight()) / 2.0));
        }
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
     
                /**
                 * {@inheritDoc}
                 */
                @Override
                public void run() {
                    try {
                        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new Main(Variant.SWING), new Main(Variant.SWING_X));
                        JFrame f = new JFrame("Test");
                        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        f.setSize(1300, 1000);
                        f.setLayout(new BorderLayout());
                        f.add(splitPane);
                        f.setLocationRelativeTo(null);
                        f.setVisible(true);
                        splitPane.setDividerLocation(0.5);
                    } catch (IOException ioe) {
                        ioe.printStackTrace();
                    }
                }
            });
        }
    }
    Merci de penser au tag quand une réponse a été apportée à votre question. Aucune réponse ne sera donnée à des messages privés portant sur des questions d'ordre technique. Les forums sont là pour que vous y postiez publiquement vos problèmes.

    suivez mon blog sur Développez.

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning. ~ Rich Cook

  6. #6
    Membre confirmé Avatar de Jose.N70
    Homme Profil pro
    Développeur Java
    Inscrit en
    Mars 2009
    Messages
    162
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : Service public

    Informations forums :
    Inscription : Mars 2009
    Messages : 162
    Par défaut
    Problème identifié, aucun rapport avec mon JScrollPane mais localisé sur le Look and Feel de mon application.

    Je vais essayer de voir cela. Merci de votre aide.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Image en fond sur une Userform
    Par potters dans le forum Macros et VBA Excel
    Réponses: 2
    Dernier message: 12/06/2007, 14h17
  2. Image de fond sur une JFrame et dans une JToolBar
    Par FredPsy dans le forum Agents de placement/Fenêtres
    Réponses: 9
    Dernier message: 01/02/2007, 18h12
  3. Probleme d'image de fond sur un <li>
    Par pierrot10 dans le forum Mise en page CSS
    Réponses: 11
    Dernier message: 30/01/2007, 15h35
  4. Affichage d'une image de fond sur un site
    Par eugiragal dans le forum Mise en page CSS
    Réponses: 1
    Dernier message: 20/12/2006, 18h43
  5. Image de fond sur MDIForm
    Par Michel Devaud dans le forum Composants VCL
    Réponses: 3
    Dernier message: 10/03/2006, 15h28

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo