Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!

Renee Zhang

Ranch Hand
+ Follow
since Sep 10, 2001
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Renee Zhang

Hi Pascal,
I don't understand your second question.
For the first question, try this
(1) Calculete your Jpopupmenu size
JPopupMenu popup = new JPopupMenu();
Dimension popupSize = popup.getPreferredSize();
(2) Calculate your screen size
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
(3) Get your mouse position by passing mouseEvent me
int showX = me.getX();
int showY = me.getY();
(4) Turn up or turn left
if (me.getY() + popupSize.height > screenSize.height)
showY = me.getY() - popupSize.height;
if (me.getX() + popupSize.width > screenSize.width)
showX = me.getX() - popupSize.width;
popup.show(me.getComponent(), showX, showY);
Help it helps and good luck.
Renee
17 years ago
Why don't you use JTextPane? It supports pdf file and html file?
17 years ago
Hi Eric,
Here is a sample code. When user enters [ENTER] key, you will see the behavior is correct although you will get a pair of <p></p> tags. This is the way I do it. The problem is that when you want to display saved html in JTextPane next time, you have to replace all <br> tags to <p></p> tags. I am not sure if this will help you or not. As I said before this is the way I solved the problem before deadline and you may overwrite [ENTER] key or add [SHIFT][ENTER] to yourTextPane's keymap. Good luck to you.

import java.awt.*;
import java.net.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;

public class test extends JFrame {
private StyleSheet css = new StyleSheet();
private JEditorPane editor = new JEditorPane();
private HTMLEditorKit kit = new HTMLEditorKit();
private HTMLDocument doc = new HTMLDocument();
private JButton showSource = new JButton("Show Source");
public test() {
css.addRule("BODY{ margin : 0;}");
kit.setStyleSheet(css);
editor.setDocument(doc);
editor.setEditorKit(kit);
editor.setBorder(null);
editor.setEditable(true);
JScrollPane scroll = new JScrollPane(editor);
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(scroll, BorderLayout.CENTER);
this.getContentPane().add(showSource, BorderLayout.SOUTH);
this.showSource.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
System.out.println("Original html:");
System.out.println(editor.getText());
System.out.println("After you take care of <p></p>");
System.out.println(takeCareOfPTag(editor.getText()));
}
});
setSize(220, 180);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}
public String takeCareOfPTag (String origHtmlString) {
if (origHtmlString == null) return null;
//Step 1:RemoveAll invisible characters which ASII value is less than 32
StringBuffer sb = new StringBuffer(origHtmlString);
for (int i = sb.length()-1; i >=0; i--) {
char aChar = sb.charAt(i);
if (((int)aChar) < 32)
sb.deleteCharAt(i);
}
//Step 2 elete all <P> or <p>
int index5 = sb.lastIndexOf("<P>");
while (index5 >= 0) {
sb.delete(index5, index5+ 3);
index5 = sb.lastIndexOf("<P>");
}
int index6 = sb.lastIndexOf("<p>");
while (index6 >= 0) {
sb.delete(index6, index6+3);
index6 = sb.lastIndexOf("<p>");
}
//Step 3:Replace all </p> by <br> and take care of white space
origHtmlString = sb.toString();
if (origHtmlString.indexOf("/p>") >0) {
String[] qTextString = origHtmlString.split("</p>");
if (qTextString != null) {
origHtmlString = qTextString[0].trim();
if (qTextString.length > 1) {
for (int i = 1; i < qTextString.length; i ++) {
origHtmlString = origHtmlString + "<br>" + qTextString[i].trim();
}
}
}
}
return origHtmlString;
}
public static void main(String[] args) {
new test().setVisible(true);
}
}

Renee
17 years ago
Hi Nathan,
I finally find a easy way to do it. The order is very important to me because I want to cut/copy/paste the exactly same order to another folder node. Please let me know if you have better solution and thanks again for your time.
<code>
import java.awt.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.JTree.*;
import java.awt.event.*;
import javax.swing.event.*;

public class SelectableTree extends JFrame {
private JTree tree;
private JButton showSelectionButton = new JButton("Show Selection");
public static void main(String[] args) {
new SelectableTree();
}
public SelectableTree() {
super("JTree Selections");
Container content = getContentPane();
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
DefaultMutableTreeNode child;
DefaultMutableTreeNode grandChild;
for(int childIndex=1; childIndex<4; childIndex++) {
child = new DefaultMutableTreeNode("Child " + childIndex);
root.add(child);
for(int grandChildIndex=1; grandChildIndex<6; grandChildIndex++) {
grandChild =
new DefaultMutableTreeNode("Grandchild " + childIndex + "." + grandChildIndex);
child.add(grandChild);
}
}
tree = new JTree(root);
tree.getSelectionModel().setSelectionMode(javax.swing.tree.TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
content.add(new JScrollPane(tree), BorderLayout.CENTER);
showSelectionButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
TreePath[] treePaths = tree.getSelectionPaths();
int [] treeRows = tree.getSelectionRows();
for (int i = 0; i < treePaths.length; i++) {
DefaultMutableTreeNode Node
= (DefaultMutableTreeNode)treePaths[i].getLastPathComponent();
System.out.println(Node.toString());
}
System.out.println("Before sorting");
for (int i = 0; i < treeRows.length; i++) {
TreePath treePath = tree.getPathForRow(treeRows[i]);
DefaultMutableTreeNode Node
= (DefaultMutableTreeNode)treePath.getLastPathComponent();
System.out.println(Node.toString());
}
System.out.println("After sorting");
java.util.Arrays.sort(treeRows);
for (int i = 0; i < treeRows.length; i++) {
TreePath treePath = tree.getPathForRow(treeRows[i]);
DefaultMutableTreeNode Node
= (DefaultMutableTreeNode)treePath.getLastPathComponent();
System.out.println(Node.toString());
}
}
});
content.add(showSelectionButton, BorderLayout.SOUTH);
setSize(250, 275);
setVisible(true);
}
}
</code>
17 years ago
Thanks Nathan, After I call getLastPathComponent(), I will get a treeNode array which is ordered by selection (The first selected node will be the first element in the array. In the previous example, it's {[NodeE],[NodeA]} ). But I want to get a treeNode array which is ordered by index(which is the closest to the parentNode.(In the previous example, it's {[NodeA],[NodeE]}).
Thanks in advance.
Renee
17 years ago
By default, JTree has a method call getSelectionPaths()which returns a treePath array of all selected nodes in selection order.
For example. I have a tree and there are 5 leaf nodes and the order is
nodeA
nodeB
nodeC
nodeD
nodeE
if I select nodeE first then select nodeA, I will get nodeE's path first, then nodeA's path if I call getSelectionPaths(). I am wondering if there is any methods that returns selected treePath in index order? I don't want to travel through the whole tree to see which node is selected because I have a huge data tree.
Thanks in advance.
Renee
17 years ago
I had the same problem. I searched the web site and find 2 solution.
1) Generate a pair of <p> </p> tag after press an enter key is a "standard" behavior of most of word product(frontpage, word...). But JTextPane and JEditor doesn't suport another common behavior which is to generate a <br> tag after press [SHIFT] + [ENTER]. You may add this combination to your keymap. It didn't work in my project.
2) put you css in Your JTextPane or JEditorPane. That's means change the behavior of how a paragraph looks like. There is an example in the web site. Here is the link: http://www.jalice.net/attributes.htm and look at the source code of "Zerospace". But you still have to replace <p></p> tag to <br> by yourself.
I hope that will help you a little bit. Good luck.
Renee
17 years ago
What kind of Driver are you using? Maybe you can post it to the JDBC forum since it's like a JDBC question...

Good Luck.
Renee
17 years ago
If you want to call ResultSet.last() and manipulate the resultSet, then when you create the statement, you have to call Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE); You may try to see if your JDBC version support update-resultset function by the return value of yourResultSet.getConcurrency(). if it returns 1007, that means the resultset is ResultSet.CONCUR_READ_ONLY. if it returns 1008, it means ResultSet.CONCUR_UPDATABLE.
There are lots of examples in the book "JDBC API Tutorial and Reference, Second Edition"... Also there is a book call "JAVA Database" has lots of similar examples too.
Good Luck!
Renee
17 years ago
I have some doubt about JDBC+ JTable... How do we populate a table when table data comes from a database?
What I am doing now is to seperate JDBC from JTable.
Step 1: Use JDBC to get a data Vector and a column Vector and close connection after that. The structure of the data Vector is up to you, I have a tableData Object which matches a table in the database if the table is editable, or it's a Vector of Vecotr if the table is read only.
Step 2: Pass 2 Vector to my tableModel which is extends AbstractorTableModel.
Step 3: After users make some changes. Save the vector in my tableModel into database.
By following these steps, it's easy to debug and doesn't open the connection all the time. I am wondering if I am doing right or anyone has good ideas about how to do JDBC + JTable right. Thanks in advance.
Renee
17 years ago
Hi Phung,
If you want to implement an interface, you have to implement all the methods in this interface. Try to extends DefaultTableModel instead of implemet tableModel...
Good Luck!
Renee
17 years ago
I did something that jumps between 2 JFrames before.
1. pass frame2 to the frame1.
2. when the button is pressed, if frame2 is minimized, then call if (frame2.getState() == JFrame.ICONIFIED) frame2.setState(JFrame.NORMAL); then you can max the size by yourself. If frame2 is showing behind the frame1, then call frame2.show() to bring it the sceen.
Good luck!
Renee
17 years ago
I remember there is a similar question that has been posted before...
Just set the column.setPreferredWidth(0); and column.setMaxWidth(0); to hide the column and reset the size to show it. I use this trick in my project and it works very well.
Gook luck!
Renee
17 years ago
I have a CallableStatement which calls a stored PL/SQL procedure. It takes about 2 minutes(Oops). I feel that once I call callableStatement.execute(); there is nowhere that I can stop it in the middle.
It looks like I have to rewrite the procedure in JAVA. Am I right? Any suggestion will be greatly appreciated.
Renee