adding html to joptionpane or dialog?

Hi there, I have these two string arrays... one with sucessfull connections and another with failed connections. i want to display them in a joption pane or dialog listed like so:

connection1 Success (in Green)

connection2 Sucess

connection 3 Failure (in Red)

String failsedsockets]=[Socket[addr=dbappsrv-11/167.192.14.11,port=8109,localport=4563]

String addressockets[] =[Socket[addr=dbappsrv-06/167.192.13.10,port=8109,localport=9874]]

JOptionPane.showMessageDialog(null,"Success: \n"+addressockets+"\n\n Failed:\n"+failedsockets,"Quaestor Result", JOptionPane.PLAIN_MESSAGE);

[696 byte] By [h2opologirly69a] at [2007-11-27 10:17:50]
# 1

one or the ways

import javax.swing.*;

class Testing

{

public static void main(String[] args)

{

JLabel success = new JLabel("Success");

success.setForeground(java.awt.Color.GREEN);

JLabel failure = new JLabel("Failure");

failure.setForeground(java.awt.Color.RED);

JPanel p = new JPanel(new java.awt.GridLayout(2,1));

p.add(success); p.add(failure);

JOptionPane.showMessageDialog(null,p);

System.exit(0);

}

}

Michael_Dunna at 2007-7-28 15:54:00 > top of Java-index,Java Essentials,Java Programming...
# 2

You can also slip a little HTML into label's text:

new JLabel("<html><font color='red'>red stuff</font> <font color='green'>green stuff</font> </html>")

BigDaddyLoveHandlesa at 2007-7-28 15:54:00 > top of Java-index,Java Essentials,Java Programming...
# 3

lets say i have a string name="this is a string that shows up in black";

can i change the color of the string?

h2opologirly69a at 2007-7-28 15:54:00 > top of Java-index,Java Essentials,Java Programming...
# 4

I think the question is more thisL I want to add a string and jlabel to a jpane.

jpane.add(string,jlabel) then call it in

JOptionPane.showMessageDialog(null,jpane," Result", JOptionPane.PLAIN_MESSAGE);?

I was trying to do that but it doesnt display the string?

ublic static void main(String[] args)

{

JLabel success = new JLabel("Success");

success.setForeground(java.awt.Color.GREEN);

JLabel failure = new JLabel("Failure");

failure.setForeground(java.awt.Color.RED);

JPanel p = new JPanel(new java.awt.GridLayout(2,2));

String printout="dbav-05";

String another="blah"

p.add(printout,failure);

p.add(another,success);

JOptionPane.showMessageDialog(null, p,"Quaestor Result", JOptionPane.PLAIN_MESSAGE);

System.exit(0);

}

h2opologirly69a at 2007-7-28 15:54:00 > top of Java-index,Java Essentials,Java Programming...
# 5

The add method intends the String argument to be a constraint for the

Layout Manager, it is expecting strings like BorderLayout.SOUTH. You cannot

directly add strings into a container. Instead, you use JLabel and set its text

property, but don't you know that already? What are you trying to do? What is

your goal?

BigDaddyLoveHandlesa at 2007-7-28 15:54:00 > top of Java-index,Java Essentials,Java Programming...
# 6

Swing related questions should be posted in the Swing forum.

Try using a JTextPane to display your text messges. This posting shows you how to color the text using attributes:

http://forum.java.sun.com/thread.jspa?forumID=57&threadID=342068

camickra at 2007-7-28 15:54:00 > top of Java-index,Java Essentials,Java Programming...
# 7

Sorry after much code looking I didn't realize what I was writing... well what I want to do is printout the success and failures of the connection in a joption pane with success in green and failure in red. write now my code just prints it out a string with successes and failures in black... here is my code

static JLabel works=new JLabel("<html><font color='green'>Success</font></html>");

static JLabel fails=new JLabel("<html><font color='red'>Fail</font></html>");

private static Properties loadProperties(String filename) {

Properties props=new Properties();

try {

props.load(new FileInputStream(filename));

} catch(FileNotFoundException fnfe) {

System.out.println("Can not find "+filename);

fnfe.printStackTrace();

} catch (IOException ioe) {

System.out.println("Problem loading "+filename);

ioe.printStackTrace();

}

return props;

}

public static void Build() throws IOException{

Properties props;

Socket quaestor=null;

props = loadProperties("quaestor.ini");

String invokeit=props.getProperty("Command");

ArrayList <Socket> listofsockets=new ArrayList <Socket>();

ArrayList <String> failedsockets=new ArrayList <String>();

//ArrayList <String> addressockets=new ArrayList <String>();

String[] splitvals= props.getProperty("Quaestors").split(" ");

String printout ="";

for (int i=0; i<splitvals.length; i++){

String[] temp=splitvals[i].split(":");

String host=temp[0];

int port=Integer.parseInt(temp[1]);

try{

quaestor=new Socket(host,port);

listofsockets.add(quaestor);

printout+=quaestor.getInetAddress().getHostName()+" Success\n";

}

catch (UnknownHostException e) {

failedsockets.add(temp[0]);

printout+=temp[0]+"Fail\n";

System.err.println("Not known host. "+ failedsockets);

e.printStackTrace();

} catch (IOException e) {

failedsockets.add(temp[0]);

printout+=temp[0]+"Fail\n";

System.err.println("Couldn't get I/O for " + "the connection: "+failedsockets);

e.printStackTrace();

}

}

System.out.println(failedsockets + "fail");

System.out.println(listofsockets+ "works");

//System.out.println(printout);

JOptionPane.showMessageDialog(null,printout,"Quaestor Result", JOptionPane.PLAIN_MESSAGE);

}

public static void main (String[] args) throws IOException{

Build();

}

Sorry for putting in the wrong area , it went from a java programming question to a swing one. w/o thinking i didnt post it in the right place... .. saw the link below about the joption pane but it didnt really clear up much for me. maybe if ilook at it today..i can figure something out. much help appreciated.>

h2opologirly69a at 2007-7-28 15:54:00 > top of Java-index,Java Essentials,Java Programming...
# 8

any help would be appreciated..

h2opologirly69a at 2007-7-28 15:54:00 > top of Java-index,Java Essentials,Java Programming...
# 9

> any help would be appreciated..

I already gave you a solution.

camickra at 2007-7-28 15:54:00 > top of Java-index,Java Essentials,Java Programming...
# 10

Thanks for the help

Message was edited by:

h2opologirly69

h2opologirly69a at 2007-7-28 15:54:00 > top of Java-index,Java Essentials,Java Programming...