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

Interfaces Graphiques en Java Discussion :

Liens sur une zone d'un JPanel


Sujet :

Interfaces Graphiques en Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    201
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 201
    Par défaut Liens sur une zone d'un JPanel
    Bonjour,

    Je souhaite réaliser en java un lien (hypertext?) lié à un libellé placé dans un JPanel à scroll bar vertical qui lors d'un click dessus me permettrait d'afficher directement la zone désirée sans avoir à manipuler la scroll Bar.

    Un peu comme les sommaires dans Word. Je ne suis pas sur que cela s'appelle un hyperlien..

    Merci d'avance.

  2. #2
    Membre très actif
    Avatar de william44290
    Homme Profil pro
    Responsable de service informatique
    Inscrit en
    Juin 2009
    Messages
    400
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 61
    Localisation : France

    Informations professionnelles :
    Activité : Responsable de service informatique

    Informations forums :
    Inscription : Juin 2009
    Messages : 400
    Par défaut
    Attention ce code n'est pas fait pour être partagé mais il traite le cas sur un JTextField :

    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
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    package wComposantControlSaisie;
     
    import java.awt.Color;
    import java.awt.Desktop;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.FocusEvent;
    import java.awt.event.FocusListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.font.TextAttribute;
    import java.io.IOException;
    import java.net.URI;
    import java.net.URISyntaxException;
    import java.util.Map;
     
    import javax.swing.JTextField;
    import javax.swing.ToolTipManager;
     
    import wFiche.FicheIFastEdit;
    import wListe.ListeIFastListe;
    import wOutils.Field;
    import wOutils.Gp;
     
    public class JTextDb extends JTextField implements FicheIFastEdit, FocusListener, MouseListener, KeyListener {
    	private static final long serialVersionUID = 1L;
     
    	int rotation=-1;
    	int x=0;
    	int y=0;
    	Color colorBorderHeader=Gp.LABEL_FG_BLACK;
    	Color color=Gp.LABEL_FG_BLACK;
    	String superText="";
    	String libelleHelpText="";
     
    	FicheIFastEdit fe;
    	ListeIFastListe fl;
    	public boolean selectAllOnFocus=true;
     
    	public boolean borderHeader=false;
    	public boolean libelleFiche=false;
    	boolean web=false;
    	boolean mailTo=false;
    	String urlsiteWeb;
    	boolean debugSetText=false;
    	boolean debugSetTextTrace=false;
    	boolean debugDisposeTrace=false;
    	public JTextDb(){
    		adapte();
    	}
    	public JTextDb(String txt){
    		super(txt);
    		adapte();
    	}
    	private void adapte(){
    		putClientProperty(Field.TYPE_FIELD,Field.TEXT);
    		this.setDisabledTextColor(Gp.VERTFONCE_FG);
    		this.setFont(Gp.FONTTEXT12);
    		this.addFocusListener(this);
    		this.addKeyListener(this);
    	}
    	public void setText(String string){
    		super.setText(string);
    		if (debugSetText){
    			Gp.af(getName()+":"+string);
    		}
    		if (debugSetTextTrace){
    			Gp.af();
    		}
    	}
    	public void setDebugSetText(boolean enable){
    		debugSetText=enable;
    	}
    	public void setDebugSetTextTrace(boolean enable){
    		debugSetTextTrace=enable;
    	}
    	public void setDebugDisposeTrace(boolean enable){
    		debugDisposeTrace=enable;
    		if (debugDisposeTrace){
    			Gp.ad("initialize : "+this.getClass().getSimpleName());
    		}
    	}
    	public void setSuperText(String txt, int rotation,int x,int y,Color color){
    		this.superText=txt;
    		this.rotation=rotation;
    		this.color=color;
    		this.y=y;
    		this.x=x;
    		this.repaint();
    	}
    	public void setLibelleHelpText(String libelleHelpText) {
    		putClientProperty(Field.TYPE_FIELD, Field.LIBE);
     
    		if (libelleHelpText.length()>0)
    			this.libelleFiche=true;
    		this.libelleHelpText=libelleHelpText;
     
    		if(libelleFiche){
    			if (Gp.TOOLTIP){
    				ToolTipManager.sharedInstance().setInitialDelay(Gp.TOOLTIPINITIALDELAY); 
    				ToolTipManager.sharedInstance().setDismissDelay(Gp.TOOLTIPDELAY);
    				ToolTipManager.sharedInstance().setReshowDelay( Gp.TOOLTIPINITIALDELAY/10 );
    				ToolTipManager.sharedInstance().registerComponent( this );
    				String s="*";
    				this.setToolTipText("<html><b>" +
    						"<br>" +
    						"<FONT  color=#000000 size=3>" +
    						s+s+s+s+libelleHelpText+s+s+s+s+
    						"<FONT/>"+
    						"<br>" +
    						"<FONT  color=#C0C0C0 size=2>Aide, exemple<FONT/>" +
    				"<html/>");
    			}
    		}
    	}	
    	public String getLibelleHelpText(){
    		return this.libelleHelpText;
    	}
    	@SuppressWarnings("unchecked")
    	public void setUnderline(boolean enable){
    		Font font = getFont();
    		Map map = font.getAttributes();
    		if (enable) {
    			map.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
    		} else {
    			web=false;
    			mailTo=false;
    			map.put(TextAttribute.UNDERLINE,null);
    		}
    		setFont(font.deriveFont(map));
    	}
    	public void setWidthChar(int width){
    		Dimension dim;
    		dim=new Dimension(((width+2)*Gp.WIDTHCHAR),Gp.INSETCHAR+Gp.HEIGHTCHAR+Gp.INSETCHAR);
    		setPreferredSize(dim);
    		setMaximumSize  (dim);
    		setMinimumSize  (dim);
    		validate();
    	}
    	public void reduceHeightDim(int height){
    		Dimension dim;
    		dim=new Dimension(this.getPreferredSize().width,height);
    		setPreferredSize(dim);
    		setMaximumSize  (dim);
    		setMinimumSize  (dim);
    		validate();
    	}
    	public void setEnabled(boolean enable){
    		if (!this.isEditable())
    			enable=false;
    		super.setEnabled(enable);
    	}
    	public void setEditable(boolean enable){
    		super.setEditable(enable);
    		if (!enable)
    			this.setEnabled(enable);
    	}
    	// FICHEFASTEDIT //
    	public void validateFastEditAdd(FicheIFastEdit fe,ListeIFastListe fl){
    		this.fe=fe;
    		this.fl=fl;
    		if (fl!=null){
    			fl.fastListeAdd(this);
    		}
    	}
    	public void fastEditRemove() {
    		if (fl!=null){
    			fl.fastListeRemove(getName());
    		}		
    	}
    	public void fastEditEnable(boolean enable) {
    		if (fl!=null){
    			setEditable(enable);
    			setEnabled(enable);
    			fl.fastListeEnable(getName(),enable);
    		}
    	}
    	public void validateFastEditRecord(String nameField,Object value) {
    		if (nameField==null){
     
    		} else {
    			if (nameField.equals(getName())){
    				if (value!=null){
    					this.setText((String) value);
    					userRecordFocusListe();
    				} else {
    					this.requestFocus();
    				}
    			}
    		}
    	}
    	public void userRecordFocusListe() {
    		if (fe!=null){
    			fe.userRecordFocusListe();
    		}
    	}
    	// FICHEFASTEDIT //
    	public void focusGained(FocusEvent arg0) {
    		if (selectAllOnFocus)
    			this.selectAll();
    	}
    	public void focusLost(FocusEvent arg0) {
    		Color saveColor=this.getBackground();
    		this.setBackground(Gp.MOVE_BG_LVERT);
    		this.setSelectionStart(0);
    		this.setSelectionEnd(0);
    		this.setBackground(saveColor);
    	}
    	public double getDouble(){
    		double ret=0.0;
    		String strret=CcsFmt.clearStringDigit(getText());
    		ret=Gp.parseDouble(strret);
    		return ret;
    	}
    	public int getInt(){
    		int ret=0;
    		String strret=CcsFmt.clearStringDigit(getText());
    		ret=Gp.parseInt(strret);
    		return ret;
    	}
    	public void adapteTextLibe(){
    		putClientProperty(Field.TYPE_FIELD, Field.LIBE);
    		setForeground(Gp.VERTFONCE_FG);
    		setBackground(Gp.MOVE_BG_LVERT);
    		setBorder(Gp.getBorder(Gp.LABEL_BORDER));
    		setFocusable(false);
    		setEditable(false);
    		setEnabled(false);
    	}
    	public void adapteTextField(){
    		putClientProperty(Field.TYPE_FIELD,Field.TEXT);
    		setForeground(Gp.LABEL_FG_BLACK);
    		setBackground(Gp.UPDATE_BG_JAUNE);
    		setBorder(Gp.getBorder(Gp.LABEL_FG_BLACK));
    		setFocusable(true);
    		setEditable(true);
    		setEnabled(true);
    	}
    	public void setColor(Color color){
    		setDisabledTextColor(color);
    	}
    	public void setAlert(boolean alert){
    		Color colorF=null;
    		Color colorB=null;
    		if (alert){
    			colorF=Gp.INVALIDE_FG_RED;
    			colorB=Gp.MOVE_BG_LRED;
    		} else {
    			colorF=Gp.VERTFONCE_FG;
    			colorB=Gp.MOVE_BG_LVERT;
    		}
    		if (isEditable()){
     
    		} else {
    			this.setColor(colorF);
    			this.setBackground(colorB);
    		}
    	}
    	public void paint(Graphics g){
    		// dessine le text
    		super.paint(g);
    		Graphics2D g2=(Graphics2D) g;
    		g2.rotate(Math.toRadians(90 * rotation));
     
    		if (borderHeader){
    			headerBorderPaint(g);
    		}
     
    		// dessine le superText
    		surPaint(g,superText,x,y);{
    			surPaintText(g,superText,color);
    		}
     
    		g2.rotate(Math.toRadians(90 * (rotation*-1)));
    	}
     
    	public void headerBorderPaint(Graphics g){
    		Color saveColor=g.getColor();
    		g.setColor(colorBorderHeader);
    		g.drawLine(0,0,getWidth(),0);
    		g.drawLine(0,getHeight()-1,getWidth(),getHeight()-1);
    		g.drawLine(getWidth(),1,getWidth(),getHeight());
    		g.setColor(saveColor);
    	}
    	public void surPaint(Graphics g,String txt, int x,int y){
    		int width	=  g.getFontMetrics(g.getFont()).stringWidth(txt);
    		int height	=  g.getFontMetrics(g.getFont()).getHeight();
     
    		switch (x){
    		case JTextDb.LEFT:
    			this.x=0+this.getInsets().left; break;
    		case JTextDb.RIGHT:
    			this.x=this.getWidth()-(width+this.getInsets().right); break;
    		case JTextDb.CENTER:
    			this.x=((this.getWidth()-(width))/2)-this.getInsets().left; break;
    		default:
    			this.x=x;
    		}
    		switch (y){
    		case JTextDb.TOP:
    			this.y=0+this.getInsets().top; break;
    		case JTextDb.BOTTOM:
    			this.y=this.getHeight()-this.getInsets().top-height; break;
    		case JTextDb.CENTER:
    			this.y=(this.getHeight()/2)+this.getInsets().top; break;
    		default:
    			this.y=y;
    		}
    	}
    	public void surPaintText(Graphics g,String txt,Color color){
    		Color saveColor=g.getColor();
    		g.setColor(color);
    		if (this.getText().length()==0){
    			if (this.getWidth()>txt.length()*Gp.WIDTHCHAR) {
    				g.drawString(txt,this.x,this.y);
    			} else {
    				g.drawString(txt,this.getInsets().left,this.y);
    			}
    		}
    		g.setColor(saveColor);
    	}
    	public void setWebBrowse(String nondusite,String urlsiteweb) {
    		if (urlsiteweb!=null){
    			if (!web) { 
    				addMouseListener(this);
    				web=true;
    			}
    			setUrlWebBrowse(urlsiteweb);
    			setText(nondusite);
    			adapteTextLibe();
    			setUnderline(true);
    		}
    	}
    	public void setUrlWebBrowse(String urlsiteweb){
    		if (urlsiteweb!=null){
    			this.urlsiteWeb=urlsiteweb;
    		}		
    	}
    	public void setWebMailto(String mailto) {
    		if (mailto!=null){
    			if (!mailTo){
    				addMouseListener(this);
    				mailTo=true;
    			}
    			setText(mailto);
    			adapteTextLibe();
    			setUnderline(true);
    		}
    	}
    	boolean clicked=false;
    	public void mouseClicked(MouseEvent e) {
    		if (web){
    			if(Desktop.isDesktopSupported()){
    				if(Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)){
    					URI uri;
    					try {
    						if (urlsiteWeb.contains("http://")){
    							uri = new URI(urlsiteWeb);
    						} else {
    							uri = new URI("http://"+urlsiteWeb);
    						}
    						Desktop.getDesktop().browse(uri);
    						uri=null;
    					} catch (URISyntaxException arg0) {
    						arg0.printStackTrace();
    					} catch (IOException arg0) {
    						arg0.printStackTrace();
    					}
    				}
    			}
    		}
    		if (mailTo){
    			if(Desktop.isDesktopSupported()){		
    				if(Desktop.getDesktop().isSupported(Desktop.Action.MAIL)){
    					URI uri;
    					try {
    						if (getText().contains("mailto:")){
    							uri = new URI(getText());
    						} else {
    							uri = new URI("mailto:"+getText());
    						}
    						Desktop.getDesktop().mail(uri);
    						uri=null;
    					} catch (IOException arg0) {
    						arg0.printStackTrace();
    					} catch (URISyntaxException arg0) {
    						arg0.printStackTrace();
    					}
    				}
    			}
    		}
    	}
    	public void mouseEntered(MouseEvent e) {}
    	public void mouseExited(MouseEvent e) {}
    	public void mousePressed(MouseEvent e) {}
    	public void mouseReleased(MouseEvent e) {}
    	public void keyPressed(KeyEvent e) {}
    	public void keyReleased(KeyEvent e) {}
    	public void keyTyped(KeyEvent e) {}
    	public void dispose() {
    		fe=null;
    		fl=null;
    	}
    	protected void finalize()  {
    		if (debugDisposeTrace){
    			Gp.ad("finalize : "+this.getName());
    		}
    		try {
    			super.finalize();
    		} catch (Throwable arg0) {
    			arg0.printStackTrace();
    		}
    	}
    }
    Ps je n'utilise pas de JLabel dans mes sources mais des JTextField non-editable.

  3. #3
    Membre expérimenté Avatar de uhrand
    Profil pro
    Développeur informatique
    Inscrit en
    Octobre 2009
    Messages
    203
    Détails du profil
    Informations personnelles :
    Localisation : Luxembourg

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Octobre 2009
    Messages : 203
    Par défaut
    Citation Envoyé par viscere Voir le message
    Je souhaite réaliser en java un lien (hypertext?) lié à un libellé placé dans un JPanel à scroll bar vertical qui lors d'un click dessus me permettrait d'afficher directement la zone désirée sans avoir à manipuler la scroll Bar. Un peu comme les sommaires dans Word.
    La classe suivante pourrait être intéressante dans ce contexte.
    Le panneau-sélecteur est la bande à droite de la barre de défilement verticale
    et peut contenir des marques pour des choses différentes dans votre JPanel.
    Le panneau-sélecteur représente le JPanel intégralement, pas seulement la partie actuellement affichée.
    Vous pouvez cliquer sur une marque dans le panneau-sélecteur pour passer à la partie qui se réfère à la marque.
    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
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    /**
     * @author André Uhres (uhrand)
     *
     * PanelSelector.java
     * source level: JDK 1.4
     *
     * The panel-selector is the stripe to the right of the vertical scroll bar
     * and can contain marks for various things in your JPanel.
     * The panel-selector represents the whole JPanel, not just the section currently displayed.
     * You can click a mark in the panel-selector to jump to the part that the mark refers to.
     */
     
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
     
    class PanelSelector extends JPanel implements ComponentListener {
     
        private MarkablePanel panel;
        private SortedSet modifiedRows;
        private int rowCount;
        private double heightStripe;
        private int max;
     
        public PanelSelector(final MarkablePanel panel) {
            this.panel = panel;
            modifiedRows = new TreeSet();
            setLayout(null);
            setPreferredSize(new Dimension(15, 0));
            panel.getParent().addComponentListener(this);
        }
     
        public void buildAllMarks() {
            removeAll();
            rowCount = panel.getRowCount();
            heightStripe = getSize().getHeight();
            max = (int) (heightStripe - 5);
            buildModifiedMarks();
            //TODO: here you may call your methods for building other types of marks
            revalidate();
            repaint();
        }
     
        private void buildModifiedMarks() {
            Iterator it = modifiedRows.iterator();
            while (it.hasNext()) {
                String value = (String) it.next();
                String rowS = value.split(";")[0];
                String label = value.split(";")[1];
                int row = Integer.parseInt(rowS);
                mark(row, label, null);
            }
        }
     
        public void addModifiedMark(int row, String label) {
            modifiedRows.add(row + ";" + label);
        }
     
        private void mark(final int row, final String text, Color bg) {
            double factor = (double) row / (double) rowCount;
            int position = (int) (heightStripe * factor);
            if (position > max) {
                position = max;
            }
            Component mark = getComponentAt(0, position);
            if (!(mark instanceof Mark)) {
                mark = new Mark(position);
                if (bg != null) {
                    mark.setBackground(bg);
                }
                add(mark);
            }
            ((Mark) mark).addRow(row, text);
        }
     
    //    public void tableChanged(final TableModelEvent e) {
    //        modifiedRows.add(new Integer(e.getFirstRow()));
    //        buildAllMarks();
    //    }
        public void componentHidden(final ComponentEvent e) {
        }
     
        public void componentMoved(final ComponentEvent e) {
        }
     
        public void componentResized(final ComponentEvent e) {
            buildAllMarks();
        }
     
        public void componentShown(final ComponentEvent e) {
        }
     
        class Mark extends JLabel implements MouseListener {
     
            private SortedSet rows;
     
            public Mark(final int position) {
                rows = new TreeSet();
                setBorder(BorderFactory.createLineBorder(Color.lightGray));
                setOpaque(true);
                setSize(new Dimension(13, 5));
                setLocation(0, position);
                setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                addMouseListener(this);
                setBackground(Color.YELLOW);
            }
     
            private void addRow(final int row, final String text) {
                if (rows.size() == 0) {
                    setBackground(getBackground());//single row
                    setToolTipText(text);
                } else {
                    setBackground(Color.MAGENTA);//multiple rows
                    String text1 = getToolTipText();
                    if (!text1.startsWith("<html>")) {
                        text1 = "<html>" + text1;
                    }
                    setToolTipText(text1 + "<br>" + text);
                }
                rows.add(new Integer(row));
            }
     
            public void mouseClicked(final MouseEvent e) {
                //get the first row that the mark refers to:
                Iterator it = rows.iterator();
                int row = ((Integer) it.next()).intValue();
                final Rectangle rowsRect = panel.getCellRect(row);
     
                //scroll the rows to visible:
                rowsRect.height += panel.getCellRect(row).y - rowsRect.y;
                panel.scrollRectToVisible(rowsRect);
     
                //finally: center the rows in the visible area:
                Rectangle visibleRect = panel.getVisibleRect();
                if (rowsRect.height < visibleRect.height) {
                    int offset = (visibleRect.height - rowsRect.height) / 2;
                    rowsRect.y += rowsRect.y == visibleRect.y ? -offset : offset;
                    panel.scrollRectToVisible(rowsRect);
                }
            }
     
            public void mouseEntered(final MouseEvent e) {
            }
     
            public void mouseExited(final MouseEvent e) {
            }
     
            public void mousePressed(final MouseEvent e) {
            }
     
            public void mouseReleased(final MouseEvent e) {
            }
        }
     
        //"main" for Testing only:
        public static void main(final String args[]) {
            JFrame f = new JFrame();
            f.setTitle("Panel Selector Demo " + System.getProperty("java.version"));
            f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            f.setSize(400, 300);
            f.setLocationRelativeTo(null);
            //
            MarkablePanel panel = new MyPanel();
            f.getContentPane().add(new JScrollPane(panel), BorderLayout.CENTER);
            PanelSelector rowSelector = new PanelSelector(panel);
            f.getContentPane().add(rowSelector, BorderLayout.EAST);
            panel.setRowCount(500);
            panel.setRowHeight(22);
            panel.setLayout(null);
            //
            int[] rows = new int[]{0, 120, 150, 160, 162, 200, 400, 499};
            for (int i = 0; i < rows.length; i++) {
                int row = rows[i];
                JLabel label = new JLabel("Test " + row);
                rowSelector.addModifiedMark(row, label.getText());
                label.setBounds(0, panel.getRowHeight() * row, 100, panel.getRowHeight());
                panel.add(label);
            }
            f.setVisible(true);
        }
    }
     
    abstract class MarkablePanel extends JPanel {
     
        abstract public int getRowCount();
     
        abstract public void setRowCount(int rowCount);
     
        abstract public int getRowHeight();
     
        abstract public void setRowHeight(int rowHeight);
     
        abstract public Rectangle getCellRect(int row);
    }
     
    class MyPanel extends MarkablePanel {
     
        private int rowCount;
        private int rowHeight;
     
        public int getRowCount() {
            return rowCount;
        }
     
        public void setRowCount(int rowCount) {
            this.rowCount = rowCount;
            setPreferredSize(new Dimension(300, rowHeight * rowCount));
        }
     
        public int getRowHeight() {
            return rowHeight;
        }
     
        public void setRowHeight(int rowHeight) {
            this.rowHeight = rowHeight;
            setPreferredSize(new Dimension(300, rowHeight * rowCount));
        }
     
        public Rectangle getCellRect(int row) {
            return new Rectangle(0, row * rowHeight, 100, rowHeight);
        }
    }

Discussions similaires

  1. Lien sur une ligne de tableau
    Par Oluha dans le forum Général JavaScript
    Réponses: 3
    Dernier message: 09/02/2005, 11h36
  2. se déplacer sur une zone de liste
    Par bb62 dans le forum IHM
    Réponses: 12
    Dernier message: 31/01/2005, 10h47
  3. 2 types de lien sur une seule page
    Par MiJack dans le forum Mise en page CSS
    Réponses: 4
    Dernier message: 06/10/2004, 09h02
  4. lien sur une même page asp
    Par Redouane dans le forum ASP
    Réponses: 4
    Dernier message: 10/03/2004, 15h53
  5. Réponses: 17
    Dernier message: 25/03/2003, 13h45

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