Solved

JTree -> SQL query

Posted on 2004-09-29
17
440 Views
Last Modified: 2006-11-17
Hello,
I have a JTree which represents an SQL where clause:
eg,
AND
   var > 10
   var2 = text
   OR
       var3 > 2
       var4 < 2
etc.

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
eg,
WHERE
{
    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??
Thanks
0
Comment
Question by:cathalmchale
  • 9
  • 8
17 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 12180537
>> 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
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12180563
Sorry, I meant JTextPane, since it allows the use of StyledDocument
0
 

Author Comment

by:cathalmchale
ID: 12180652
Ok, would you have any simple examples with similar functionality you could point me towards please?
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
LVL 37

Expert Comment

by:zzynx
ID: 12180744
0
 

Author Comment

by:cathalmchale
ID: 12180935
thanks.
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" />
        </filter-group>
     </filter-group>
  </filter-definition>

I presume this is OK - I can do something like:
if its a <filter-group>
    set text color, style etc
    print string
else
...
And keeping track of indents??  should all be ok i think
0
 

Author Comment

by:cathalmchale
ID: 12180979
>>And keeping track of indents??  should all be ok i think
 the tab-stops at this link I guess!!

cheers
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12180984
>>should all be ok i think
Don't have any experience with it, but I think it, yeah.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12181012
>> the tab-stops at this link I guess!!
???
0
 

Author Comment

by:cathalmchale
ID: 12188310
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.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12188379
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);
0
 

Author Comment

by:cathalmchale
ID: 12188389
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
jTextPane.setDocument(sqlDocument);

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???
0
 

Author Comment

by:cathalmchale
ID: 12188407
>> 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");
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12188810
>> 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
    doc.setCharacterAttributes(...)

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
0
 

Author Comment

by:cathalmchale
ID: 12189902
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??
0
 
LVL 37

Accepted Solution

by:
zzynx earned 250 total points
ID: 12190013
0
 

Author Comment

by:cathalmchale
ID: 12190577
thanks all working well now,
what a lot of hassle ;-)

Cathal,
P.S. shud i post my solution - it may be of help to others, but the code is quite long
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12190661
Thanks for accepting

>> thanks all working well now,
Nice!
>> 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

0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Checkbox and ListView in Android Layout 4 67
simple java question 3 56
difference of if loops 23 49
Windows 10 IE Certificate Issue 10 47
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question