Ik heb een probleempje met JTextPane.
Ik wil een klein teksteditortje bouwen,
met een JTextPane, zodat ik ook een beetje aan opmaak kan doen. (lettergrootte etc...)
Maar het probleem is, dat dit voorbeeldprogramma zo instabiel is als wat!
Soms als ik op de backspace-toets druk loopt de dosbox vol errors, en verdwijnt soms alle tekst in het invulvak.
Iemand een ideetje?
het programmaatje moet namelijk uiteraard wel 100% stabiel zijn
Ik wil een klein teksteditortje bouwen,
met een JTextPane, zodat ik ook een beetje aan opmaak kan doen. (lettergrootte etc...)
Maar het probleem is, dat dit voorbeeldprogramma zo instabiel is als wat!
Soms als ik op de backspace-toets druk loopt de dosbox vol errors, en verdwijnt soms alle tekst in het invulvak.
Iemand een ideetje?
het programmaatje moet namelijk uiteraard wel 100% stabiel zijn
code:
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
| import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
class JTextpaneExample extends JFrame implements ActionListener
{
private Hashtable attributes;
private JComboBox styleCombo;
private DefaultStyledDocument doc;
private JTextPane textComponent;
public JTextpaneExample()
{
setTitle( "Document Handling Application" );
setSize( 300, 190 );
setBackground( Color.gray );
JPanel topPanel = new JPanel( new BorderLayout() );
getContentPane().add( topPanel );
// Create styles for the document
StyleContext sc = new StyleContext();
doc = new DefaultStyledDocument( sc );
createStyles( sc );
// Create a text pane to display text
textComponent = new JTextPane( doc );
textComponent.setBackground( Color.white );
topPanel.add( textComponent, BorderLayout.CENTER );
// Create a toolbar to handle style changes
topPanel.add( createToolBar(), BorderLayout.NORTH );
}
// Create a VERY simple toolbar panel
public JPanel createToolBar()
{
JPanel panel = new JPanel( new FlowLayout() );
styleCombo = new JComboBox();
styleCombo.addActionListener( this );
panel.add( styleCombo );
// Add each style to the combobox
for( Enumeration e = attributes.keys(); e.hasMoreElements(); )
styleCombo.addItem( e.nextElement().toString() );
return panel;
}
// Handle changes to the combobox (style changes)
public void actionPerformed( ActionEvent e )
{
if( e.getSource() == styleCombo )
{
try {
// Determine the new style
Style s = (Style)attributes.get( styleCombo.getSelectedItem() );
// Set the style and return to the editor
doc.insertString( textComponent.getCaret().getDot(), " ", s );
textComponent.grabFocus();
}
catch( BadLocationException exception )
{
}
}
}
// Create some different font styles
public void createStyles( StyleContext sc )
{
Style myStyle;
// Allocate a hashtasble for our styles
attributes = new Hashtable();
// No style
myStyle = sc.addStyle( null, null );
attributes.put( "none", myStyle );
// Normal
myStyle = sc.addStyle( null, null );
StyleConstants.setLeftIndent( myStyle, 10 );
StyleConstants.setRightIndent( myStyle, 10 );
StyleConstants.setFontFamily( myStyle, "Helvetica" );
StyleConstants.setFontSize( myStyle, 14 );
StyleConstants.setSpaceAbove( myStyle, 4 );
StyleConstants.setSpaceBelow( myStyle, 4 );
attributes.put( "normal", myStyle );
// Big
myStyle = sc.addStyle( null, null );
StyleConstants.setFontFamily( myStyle, "Dialog" );
StyleConstants.setFontSize( myStyle, 28 );
attributes.put( "big", myStyle );
// Bold
myStyle = sc.addStyle( null, null );
StyleConstants.setBold( myStyle, true );
attributes.put( "bold", myStyle );
}
// Main() method to get the ball rolling
public static void main( String args[] )
{
// Create an instance of the test application
JTextpaneExample mainFrame = new JTextpaneExample();
mainFrame.setVisible( true );
}
} |