Center Text in TextArea

Status
Nicht offen für weitere Antworten.
hmmm

also ich hab das jetzt in nen jtextpane umgeändert...
aber so richtig versteh ich deinen quellcode noch net...

du hast da nen button wenn du den drückst setzt der es center... das geht ja... aber ich versteh net wieso das geht ... weil du sagst ja nur:
Code:
 b6= new JButton(new StyledEditorKit.AlignmentAction("Center",StyleConstants.ALIGN_CENTER));

wie hat das denn nen bezug zu dem textpane?

Code:
private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLabel = new JLabel();
			jLabel.setText("");			
			jLabel.setMinimumSize(new Dimension(0, 0));
			jLabel.setPreferredSize(new Dimension(10, 10));
			jLabel.setMaximumSize(new Dimension(0, 0));
			jTextPaneText = new JTextPane(); 
			jTextPaneText.setText(uebg_frage);
			jTextPaneText.setEditable(false);
			jTextPaneText.setBackground(new Color(0.0f, 0.0f, 0.0f, 0.0f));
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(new JScrollPane(jTextPaneText), BorderLayout.CENTER); 
			jContentPane.add(getJPanelSouth(), BorderLayout.SOUTH);
		}
		return jContentPane;
	}

und wie muss die funktion dann aussehn damit er in dem textpane den text zentriert anzeigt?
 
Kaladial hat gesagt.:
weil du sagst ja nur:
Code:
 b6= new JButton(new StyledEditorKit.AlignmentAction("Center",StyleConstants.ALIGN_CENTER));
wie hat das denn nen bezug zu dem textpane?
StyledEditorKit.AlignmentAction nimmt die letzte fokusierte Textkomponente als Textpane an.

Kaladial hat gesagt.:
und wie muss die funktion dann aussehn damit er in dem textpane den text zentriert anzeigt?
Code:
MutableAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setAlignment(attr, StyleConstants.ALIGN_CENTER);
int p0 = textpane.getSelectionStart(); 
int p1 = textpane.getSelectionEnd();
StyledDocument doc = textpane.getStyledDocument();
doc.setParagraphAttributes(p0, p1 - p0, attr, true);
Das ist auch im Prinzip das Gleiche, was die StyledEditorKit.AlignmentAction in
ihrer actionPerformed Methode macht :wink:
 
Code:
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLabel = new JLabel();
			jLabel.setText("");			
			jLabel.setMinimumSize(new Dimension(0, 0));
			jLabel.setPreferredSize(new Dimension(10, 10));
			jLabel.setMaximumSize(new Dimension(0, 0));
			jTextPaneText = new JTextPane(); 
			jTextPaneText.setText(uebg_frage);
			jTextPaneText.setEditable(false);
			jTextPaneText.setBackground(new Color(0.0f, 0.0f, 0.0f, 0.0f));
			
			MutableAttributeSet attr = new SimpleAttributeSet(); 
            StyleConstants.setAlignment(attr, StyleConstants.ALIGN_CENTER); 
            int p0 = jTextPaneText.getSelectionStart(); 
            int p1 = jTextPaneText.getSelectionEnd(); 
            StyledDocument doc = jTextPaneText.getStyledDocument(); 
            doc.setParagraphAttributes(p0, p1 - p0, attr, true);

			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(new JScrollPane(jTextPaneText), BorderLayout.CENTER); 
			jContentPane.add(getJPanelSouth(), BorderLayout.SOUTH);
		}
		return jContentPane;
	}

hab das jetzt so gemacht... das ist aber eher subotimal...

das kommt bei raus:

http://rapidshare.com/files/49334722/Unbenannt.JPG.html
 
Ich bin nicht sicher ob du jetzt noch Hilfe brauchst?
Dein letzter Beitrag scheint jedenfalls keine Frage mehr zu enthalten, es sei denn, du hast sie jut versteckt :wink:
 
also das zentrieren hab ich jetzt hinbekommen hatte den teil von dir nur an die falsche stelle gesetzt....

anderes problem:
wenn ich einen test hab der größer ist als mein sichtbares feld wird ja der scrollbalken eingeblendet...

problem hierbei:
wenn ich nun nach oben oder unten scrolle stellt er mir den text nicht mehr richtig dar... also er übermalt ihn immer und löscht das vorhergehende nicht...

sieht aus als ob du in nem 1 px abstand 50 mal das wort hallo geschrieben hast ... also er zieht ne spur... wie bekomm ich das wieder weg?

und wieso ist die schriftart in dem pane anders als beim rest der texte???
 
Das Verschmieren kommt durch den transparenten setBackground.
Nimm den einfach mal raus:
Code:
   private JPanel getJContentPane() {
        if (jContentPane == null) {
            jLabel = new JLabel();
            jLabel.setText("");
            jLabel.setMinimumSize(new Dimension(0, 0));
            jLabel.setPreferredSize(new Dimension(10, 10));
            jLabel.setMaximumSize(new Dimension(0, 0));
            jTextPaneText = new JTextPane();
            jTextPaneText.setText(uebg_frage);
            jTextPaneText.setEditable(false);
            jContentPane = new JPanel();
            jContentPane.setLayout(new BorderLayout());
            jContentPane.add(new JScrollPane(jTextPaneText), BorderLayout.CENTER);
            center();
        }
        return jContentPane;
    }
    private void center() {
        ActionEvent e = new ActionEvent(jTextPaneText, 0, "Center");
        (new StyledEditorKit.AlignmentAction("Center",StyleConstants.ALIGN_CENTER)).actionPerformed(e);
    }
 
Code:
    private JPanel getJContentPane() { 
        if (jContentPane == null) { 
            jLabel = new JLabel(); 
            jLabel.setText(""); 
            jLabel.setMinimumSize(new Dimension(0, 0)); 
            jLabel.setPreferredSize(new Dimension(10, 10)); 
            jLabel.setMaximumSize(new Dimension(0, 0)); 
            jTextPaneText = new JTextPane(); 
            center(); 
            jTextPaneText.setText(uebg_frage); 
            jTextPaneText.setEditable(false); 
            jContentPane = new JPanel(); 
            jContentPane.setLayout(new BorderLayout()); 
            jContentPane.add(new JScrollPane(jTextPaneText), BorderLayout.CENTER); 
        } 
        return jContentPane; 
    } 
    private void center() { 
        ActionEvent e = new ActionEvent(jTextPaneText, 0, "Center"); 
        (new StyledEditorKit.AlignmentAction("Center",StyleConstants.ALIGN_CENTER)).actionPerformed(e); 
    }

thx andre musste das nur geringfügig abändern also das center(); n bissel höher nehmen sonst hätte er wieder nur die letzte zeile center gesetzt..

VIELEN DANK 🙂
 
Status
Nicht offen für weitere Antworten.

Zurück
Oben