Voici un objet sympa mais quelque peu limité par la façon dont il gère son contenu HTML !
Le JEditorPane
Il permet de réaliser des textes riches avec mise en page.
Il y a aussi le
JTextPane qui vous permet de faire un mini-éditeur de texte très facilement (enfin, tout est relatif...).
Code source de cette fenêtre :
Secret (cliquez pour afficher)
Code : Java 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 | import java.awt.BorderLayout;
import java.awt.Dimension;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.text.html.HTMLEditorKit;
public class Fenetre extends JFrame {
private JEditorPane editorPane, apercu;
private JTabbedPane onglet = new JTabbedPane();
public Fenetre(){
this.setSize(600, 400);
this.setTitle("Conteneur éditable");
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
editorPane = new JEditorPane();
editorPane.setText("<HTML>\n<HEAD></HEAD>\n<BODY>\n\n</BODY>\n</HTML>");
apercu = new JEditorPane();
apercu.setEditable(false);
onglet.addTab("Editeur HTML", new JScrollPane(editorPane));
onglet.addTab("Aperçu", new JScrollPane(apercu));
onglet.addChangeListener(new ChangeListener(){
public void stateChanged(ChangeEvent e) {
FileWriter fw = null;
try {
fw = new FileWriter(new File("tmp/tmp.html"));
fw.write(editorPane.getText());
fw.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//*
try {
File file = new File("tmp/tmp.html");
apercu.setEditorKit(new HTMLEditorKit());
apercu.setPage(file.toURL());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
this.getContentPane().add(onglet, BorderLayout.CENTER);
this.setVisible(true);
}
public static void main(String[] args){
Fenetre fen = new Fenetre();
}
}
|
Le JSlider
Celui-ci est un outil qui vous permet d'utiliser un système de mesure pour une application : re-dimensionner une image, choisir le tempo d'un morceau de musique, l'opacité d'une couleur...
Code source :
Secret (cliquez pour afficher)Code : Java 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 | import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Slide extends JFrame{
private JLabel label = new JLabel("Valeur actuelle : 30");
public Slide(){
this.setSize(250, 150);
this.setTitle("Slider");
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JSlider slide = new JSlider();
slide.setMaximum(100);
slide.setMinimum(0);
slide.setValue(30);
slide.setPaintTicks(true);
slide.setPaintLabels(true);
slide.setMinorTickSpacing(10);
slide.setMajorTickSpacing(20);
slide.addChangeListener(new ChangeListener(){
public void stateChanged(ChangeEvent event){
label.setText("Valeur actuelle : " + ((JSlider)event.getSource()).getValue());
}
});
this.getContentPane().add(slide, BorderLayout.CENTER);
this.getContentPane().add(label, BorderLayout.SOUTH);
}
public static void main(String[] args){
Slide slide = new Slide();
slide.setVisible(true);
}
}
|
La JProgessBar
Elle vous permet de réaliser une barre de progression pour des traitements longs.
Code source :
Secret (cliquez pour afficher)Code : Java 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 | import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
public class Progress extends JFrame{
private Thread t;
private JProgressBar bar;
private JButton launch ;
public Progress(){
this.setSize(300, 80);
this.setTitle("*** JProgressBar ***");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
t = new Thread(new Traitement());
bar = new JProgressBar();
bar.setMaximum(500);
bar .setMinimum(0);
bar.setStringPainted(true);
this.getContentPane().add(bar, BorderLayout.CENTER);
launch = new JButton("Lancer");
launch.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
t = new Thread(new Traitement());
t.start();
}
});
this.getContentPane().add(launch, BorderLayout.SOUTH);
t.start();
this.setVisible(true);
}
class Traitement implements Runnable{
public void run(){
launch.setEnabled(false);
for(int val = 0; val <= 500; val++){
bar.setValue(val);
try {
t.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
launch.setEnabled(true);
}
}
public static void main(String[] args){
Progress p = new Progress();
}
}
|
La modification des valeurs de cet objet doit se faire dans un thread, sinon vous aurez la barre vide, un temps d'attente puis la barre remplie, mais sans défilement des valeurs !