JTree -> SQL query

I have a JTree which represents an SQL where clause:
   var > 10
   var2 = text
       var3 > 2
       var4 < 2

I'm going to stick this tree in a JSplitPane and then Im going to make a dynamic parser as the right component - just the representative SQL statement
    var > 10  AND
    var2 = text AND
or whatever!!

I want to know what would be the best JComponent to use to display this SQL statement??
eg, it would be easy to generate XML from the tree and this XML could allow me to specify, font color, bold,  indents (for groups AND, OR) etc.

What do you think??
Who is Participating?
zzynxSoftware engineerCommented:
>> it would be easy to generate XML from the tree and this XML could allow me to specify, font color, bold,  indents (for groups AND, OR) etc.
JTextArea I suppose
zzynxSoftware engineerCommented:
Sorry, I meant JTextPane, since it allows the use of StyledDocument
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

cathalmchaleAuthor Commented:
Ok, would you have any simple examples with similar functionality you could point me towards please?
zzynxSoftware engineerCommented:
cathalmchaleAuthor Commented:
i have the following XML generated for other reasons, i want to reuse this:

- <filter-definition>
-    <filter-group logic="AND">
        <filter field="Address" operator="=" value="2" />
        <filter field="customer_name" operator="=" value="john" />
-       <filter-group logic="OR">
           <filter field="EleInPort" operator="=" value="22" />

I presume this is OK - I can do something like:
if its a <filter-group>
    set text color, style etc
    print string
And keeping track of indents??  should all be ok i think
cathalmchaleAuthor Commented:
>>And keeping track of indents??  should all be ok i think
 the tab-stops at this link I guess!!

zzynxSoftware engineerCommented:
>>should all be ok i think
Don't have any experience with it, but I think it, yeah.
zzynxSoftware engineerCommented:
>> the tab-stops at this link I guess!!
cathalmchaleAuthor Commented:
Hi again,

im using
StyledDocument sqlDoc = new DefaultStyledDocument();
and I have some Styles set up, so I can use
sqlDoc.insertString(sqlDoc.getLength(), "Some text", style);

but how do I specify the tab position at where I want this text inserted - this position is relative to the recursion number -> each time I recurse ill add 10 to this tab position for example.
zzynxSoftware engineerCommented:
Just an idea: can't you use "\t" ?
sqlDoc.insertString(sqlDoc.getLength(), "\tSome text", style);

StringBuffer tabs = new StringBuffer("\t");
In your loop: tabs.append("\t");
sqlDoc.insertString(sqlDoc.getLength(), tabs.toString()+"Some text", style);
cathalmchaleAuthor Commented:
Also, it seems that you must associate a Style with a document - and initialise everything at the beginning.
I had imagined that each time I wanted to redraw the JTextPane I would do:

StyledDocument sqlDoc = new DefaultStyledDocument();
// could use some Styles initialised as fields in the class
sqlDoc.insertString(sqlDoc.getLength(), "Some text", style);

//then finally to update evrything

but I can't do this because of the StyledDocument interface - so what do I do, just set-up everything (new doc, add new styles etc.) each time???
cathalmchaleAuthor Commented:
>> Just an idea: can't you use "\t" ?

i guess I could try this, but the "tab-stops" idea which was in the link you posted me, perhaps seems like a better idea??  but I am not sure how to dynamically implement this like your dynamic  >>In your loop (RECURSION): tabs.append("\t");
zzynxSoftware engineerCommented:
>> so what do I do, just set-up everything
No. You can

    // 1) Start with the current input attributes for the JTextPane:
    MutableAttributeSet attrs = jTextPane.getInputAttributes();
    // 2) Change some attributes

    // 3) Retrieve the pane's document object
    StyledDocument doc = jtp.getStyledDocument();
    // 4) Apply the new attributes

Have a look at the last example at http://javatechniques.com/public/java/docs/basics/jtextpane-font.html

Also interesting: http://javaalmanac.com/egs/javax.swing.text/style_HiliteWords2.html
cathalmchaleAuthor Commented:
OK thanks, I have it working beautifully now with 1 small exception !! :)

the "\t" tab is too big, how do i set it to a smaller size??
cathalmchaleAuthor Commented:
thanks all working well now,
what a lot of hassle ;-)

P.S. shud i post my solution - it may be of help to others, but the code is quite long
zzynxSoftware engineerCommented:
Thanks for accepting

>> thanks all working well now,
>> what a lot of hassle ;-)

>>P.S. shud i post my solution - it may be of help to others, but the code is quite long
You decide

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.