PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : JEditorPane kein Eingabefeld?



vollkommenegal
13-07-2006, 07:45
Hallo.

Ich habe folgenden Code:


JEditorPane editorfeld = new JEditorPane();
editorfeld.setContentType ("text/html; charset=UTF-8");
BevelBorder myborder = new BevelBorder(BevelBorder.LOWERED);
editorfeld.setBorder( myborder );
editorfeld.setText("<font color=green>gruen</font><font color=red>rot</font>");
editorfeld.setEnabled(true);
editorfeld.setEditable(true);
editorfeld.setBounds(tField.getBounds());
editorfeld.setLocation(tField.getLocation());
editorfeld.setVisible(true);

Eigentlich möchte ich ein einzeiliges Eingabefeld erzeugen, das einen mehrfarbigen Text beinhaltet und in das man reinklicken kann, um den Text scrollen zu können, falls der Text länger als das Eingabefeld sein sollte.
Das mit den Farben hab ich hinbekommen, aber der Text steht einfach nur so da; das ist kein Eingabefeld. Was mache ich denn falsch? :confused:

Danke.

cya

bischi
13-07-2006, 08:04
Eigentlich möchte ich ein einzeiliges Eingabefeld erzeugen
JTextField

MfG Bischi

vollkommenegal
14-07-2006, 08:32
Danke, aber bei einem JTextField kann man den Text nicht farbig markieren.

cya

bischi
14-07-2006, 08:39
Wenn du nur Text anzeigen willst: Wieso nicht ein JLabel nehmen?

MfG Bischi

Corcovado
14-07-2006, 14:19
Auch wenn mir absolut schleierhaft bleibt, wie Du ein einzeiliges editor pane ueber mehrere Zeilen scrollen willst?! nja - vllt hilft Dir ja folgendes Bsp. etwas:

package terminal01;

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;


/**
* A demo for a colored terminal output using a <CODE>JTextPanel</CODE>.
*
* @author Jim Sculley
*/
public class OutputPanel extends JPanel {
private JTextPane outputPane;
private JScrollPane scrollPane;
private DefaultStyledDocument outputDocument;
private static SimpleAttributeSet infoAttributes;
private static SimpleAttributeSet errorAttributes;
private static SimpleAttributeSet hintAttributes;


/* ************************************************** ************************************************** *******
* constructors
* ************************************************** ************************************************** *******/

/**
* constructor
*
*/
public OutputPanel() {
initialize();
}


/* ************************************************** ************************************************** *******
* init methods
* ************************************************** ************************************************** *******/

private void initialize() {
setLayout(new BorderLayout());
add(getScrollPane());
}


/* ************************************************** ************************************************** *******
* append output methods
* ************************************************** ************************************************** *******/

/**
* Generic append output method.
*
* @param output
* The <CODE>String</CODE> to append.
* @param attributes
* The corresponding <CODE>AttributeSet</CODE>.
*/
private void addOutput(String output, AttributeSet attributes) {
try {
getOutputDocument().insertString(
getOutputDocument().getLength(),
output + "\n", attributes);
getOutputPane().setCaretPosition(
getOutputDocument().getLength());
} catch (BadLocationException ble) {
ble.printStackTrace();//DEBUG
}
}


/**
* Appends a hint.
*
* @param outputText
* The text to append.
*/
public void addHintOutput(String outputText) {
addOutput(outputText, getHintAttributes());
}


/**
* Appends an info.
*
* @param outputText
* The text to append.
*/
public void addInfoOutput(String outputText) {
addOutput(outputText, getInfoAttributes());
}


/**
* Appends a general text.
*
* @param outputText
* The text to append.
*/
public void addErrorOutput(String outputText) {
addOutput(outputText, getErrorAttributes());
}


/* ************************************************** ************************************************** *******
* accessors
* ************************************************** ************************************************** *******/

/**
* Inits the <CODE>outputDocument</CODE> with <CODE>DefaultStyledDocument</CODE>.
*
* @return
* The <CODE>outputDocument</CODE>.
*/
private DefaultStyledDocument getOutputDocument() {
if (outputDocument == null) {
outputDocument = new DefaultStyledDocument();
}
return outputDocument;
}


/**
* Generates the <CODE>AttributeSet</CODE> for infos.
*
* @return
* The accourding <CODE>AttributeSet</CODE> object.
*/
private AttributeSet getInfoAttributes() {
if (infoAttributes == null) {
infoAttributes = new SimpleAttributeSet();
Color darkBlue = new Color(0,50,200);
infoAttributes.addAttribute(StyleConstants.Foregro und,
darkBlue);
}
return infoAttributes;
}


/**
* Generates the <CODE>AttributeSet</CODE> for errors.
*
* @return
* The according <CODE>AttributeSet</CODE> object.
*/
private AttributeSet getErrorAttributes() {
if (errorAttributes == null) {
errorAttributes = new SimpleAttributeSet();
errorAttributes.addAttribute(StyleConstants.Foregr ound,
Color.RED);
}
return errorAttributes;
}


/**
* Generates the <CODE>AttributeSet</CODE> for hints.
*
* @return
* The according <CODE>AttributeSet</CODE> object.
*/
private AttributeSet getHintAttributes() {
if (hintAttributes == null) {
hintAttributes = new SimpleAttributeSet();
Color darkGreen = new Color(7,104,0);
hintAttributes.addAttribute(StyleConstants.Foregro und,
darkGreen);
}
return hintAttributes;
}


/* ************************************************** ************************************************** *******
* widget accessors
* ************************************************** ************************************************** *******/

/**
* Inits and returns a <CODE>JScrollPane</CODE>.
*
* @return
* A <CODE>JScrollPane</CODE> object.
*/
private JScrollPane getScrollPane() {
if (scrollPane == null) {
scrollPane = new JScrollPane(getOutputPane());
scrollPane.setBorder(null);
}
return scrollPane;
}


/**
* Inits and returns a <CODE>JTextPane</CODE>.
*
* @return
* A <CODE>JTextPane</CODE> object.
*/
private JTextPane getOutputPane() {
if (outputPane == null) {
outputPane = new JTextPane(getOutputDocument());
outputPane.setEditable(false);
outputPane.setBorder(null);
}
return outputPane;
}


/* ************************************************** ************************************************** *******
*
* main method - test code
*
* ************************************************** ************************************************** *******/

/**
* The main()
*
* Generates a <CODE>JFrame</CODE> and places an <CODE>OutputPanel</CODE>
* in it and adds some lines to it using its append methods.
*
* @param args
* The argument pointers.
*/
public static void main(String[] args) {
JFrame f = new JFrame("OutputPanel Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
OutputPanel op = new OutputPanel();
for (int i = 0; i < 10; i++) {
op.addInfoOutput("INFO: Some Info");
op.addHintOutput("HINT: A hint");
op.addErrorOutput("ERR: An Error");
}
f.getContentPane().add(op);
f.setSize(400,200);
f.setVisible(true);
}
}


PS:
Evtl zu den versch. Text Components sh. vllt auch noch das hier:
http://java.sun.com/docs/books/tutorial/uiswing/components/text.html

suso
25-07-2006, 10:05
@ Corcovado
also bei meinem Professor hättest du ne 1 allein wegen den kommentaren :D

Corcovado
01-08-2006, 15:19
@author Jim Sculley - nicht ich!