aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Scrollbar is not visible Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Scrollbar is not visible" Watch "Scrollbar is not visible" New topic
Author

Scrollbar is not visible

kiran cy
Greenhorn

Joined: Aug 24, 2012
Posts: 24
I created a frame which have panel,splitpane and scrollpane. The splitpane have one scrollpane and panel. I have jtree structure of xml and i added it to the scrollpane of splitpane. The scrollbar is not visible, not able to see the nodes beyond the visibility os the scrollpane
Here is my code

Main app = new Main();
JFrame.setDefaultLookAndFeelDecorated(true);
app.f = new JFrame("NCX Generator");
app.pane1=new JPanel();
app.pane2=new JSplitPane();
app.right=new JPanel();
app.left=new JPanel();
app.Scrpane=new JScrollPane();



app.pane2.setRightComponent(app.right);
app.pane2.setLeftComponent(app.Scrpane);



app.pane3=new JScrollPane();
app.browse = new JButton("BROWSE");
app.save = new JButton("SAVE");
app.search = new JButton("SEARCH");
app.field = new JTextField("Enter Text ");
//app.pane2.setOneTouchExpandable(true);
app.pane2.setDividerLocation(700);
app.Scrpane.setLayout(null);
app.Scrpane.setBounds(10, 30, 700, 700);
app.Scrpane.setBackground(Color.white);

app.right.setLayout(null);
app.right.setBounds(700, 30, 700, 700);
app.right.setBackground(Color.white);

app.f.setLayout(new BorderLayout(100,100));
app.f.setBounds(0, 0, 1300, 720);
app.pane1.setLayout(new FlowLayout());

app.pane1.add(app.browse);
app.pane1.add(app.save);
app.pane1.add(app.search);
app.pane1.add(app.field);
app.f.add(app.pane1,BorderLayout.NORTH);
app.f.add(app.pane2,BorderLayout.CENTER);
app.f.add(app.pane3,BorderLayout.SOUTH);

app.browse.addActionListener(app);
app.save.addActionListener(app);
app.search.addActionListener(app);

app.f.setVisible(true);
}

private TreeSelectionListener createSelectionListener() {
return new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
String temp;
TreePath path = e.getPath();

int pathCount = path.getPathCount();

for (int i = 0; i < pathCount; i++) {
System.out.print(path.getPathComponent(i).toString());
if (i + 1 != pathCount) {
System.out.print("|");
}

if (i + 1 == pathCount) {
temp = path.getPathComponent(i).toString();
System.out.println("/n");
System.out.println(temp);
}
}
System.out.println("");
}
};
}

public void actionPerformed(ActionEvent e) {
if (e.getSource() == browse) {
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
File file = chooser.getSelectedFile();
filename = file.getPath();
System.out.println("You have selected: " + filename);

if (filename != "") {
InputStream in;
try {
in = new FileInputStream(new File(filename));
new XmlTree(filename, in);
} catch (FileNotFoundException e1) {

e1.printStackTrace();
}

try {
WindowUtilities.setNativeLookAndFeel();
JTree tree = new JTree();
tree=new XmlTree(filename);
tree.setEditable(true);
Scrpane.add(tree);

tree.addTreeSelectionListener(createSelectionListener());
tree.setBounds(10, 30, 700, 700);


//Scrpane.setPreferredSize(tree.getPreferredSize());
Scrpane.setVisible(true);
} catch (IOException ioe) {
System.out.println("Error creating tree: " + ioe);
}
}
}
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

You should post complete code, or at least something that the one is able to compile.
Also post you code between code tags.


The quieter you are, the more you are able to hear.
kiran cy
Greenhorn

Joined: Aug 24, 2012
Posts: 24


public static void main(String args[]) {
String jaxpPropertyName = "javax.xml.parsers.DocumentBuilderFactory";
if (System.getProperty(jaxpPropertyName) == null) {
String apacheXercesPropertyValue = "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl";
System.setProperty(jaxpPropertyName, apacheXercesPropertyValue);
}

Main app = new Main();
JFrame.setDefaultLookAndFeelDecorated(true);
app.f = new JFrame("NCX Generator");
app.pane1=new JPanel();
app.pane2=new JSplitPane();
app.right=new JPanel();
app.left=new JPanel();
app.Scrpane=new JScrollPane();



app.pane2.setRightComponent(app.right);
app.pane2.setLeftComponent(app.Scrpane);



app.pane3=new JScrollPane();
app.browse = new JButton("BROWSE");
app.save = new JButton("SAVE");
app.search = new JButton("SEARCH");
app.field = new JTextField("Enter Text ");
//app.pane2.setOneTouchExpandable(true);
app.pane2.setDividerLocation(700);
app.Scrpane.setLayout(null);
app.Scrpane.setBounds(10, 30, 700, 700);
app.Scrpane.setBackground(Color.white);

app.right.setLayout(null);
app.right.setBounds(700, 30, 700, 700);
app.right.setBackground(Color.white);

app.f.setLayout(new BorderLayout(100,100));
app.f.setBounds(0, 0, 1300, 720);
app.pane1.setLayout(new FlowLayout());

app.pane1.add(app.browse);
app.pane1.add(app.save);
app.pane1.add(app.search);
app.pane1.add(app.field);
app.f.add(app.pane1,BorderLayout.NORTH);
app.f.add(app.pane2,BorderLayout.CENTER);
app.f.add(app.pane3,BorderLayout.SOUTH);

app.browse.addActionListener(app);
app.save.addActionListener(app);
app.search.addActionListener(app);

app.f.setVisible(true);
}
JTree tree = new JTree();
tree=new XmlTree(filename);
tree.setEditable(true);
Scrpane.add(tree);java.lang.Main

tree.addTreeSelectionListener(createSelectionListener());
tree.setBounds(10, 30, 700, 700);


//Scrpane.setPreferredSize(tree.getPreferredSize());
Scrpane.setVisible(true);
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

You could really try harder.
Try putting this code into a file and compile it.
Again, use code tags for your source.
kiran cy
Greenhorn

Joined: Aug 24, 2012
Posts: 24
OK if it is not what you say it is guide me step by step(literally).
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Let's start with this:


Where is Main class definition?
kiran cy
Greenhorn

Joined: Aug 24, 2012
Posts: 24
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.TreePath;

//import HighlightText.MyTreeCellRenderer;

public class Main implements ActionListener {

JButton browse, save;
String filename = "", filesave = "";
JFrame f;
JPanel pane1;
JPanel right;
JPanel left;
JSplitPane pane2;
JScrollPane pane3;
JScrollPane Scrpane;
JTextField field;
JButton search;
static JTree tree;


public static void main(String args[]) {
String jaxpPropertyName = "javax.xml.parsers.DocumentBuilderFactory";
if (System.getProperty(jaxpPropertyName) == null) {
String apacheXercesPropertyValue = "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl";
System.setProperty(jaxpPropertyName, apacheXercesPropertyValue);
}

Main app = new Main();
JFrame.setDefaultLookAndFeelDecorated(true);
app.f = new JFrame("NCX Generator");
app.pane1=new JPanel();
app.pane2=new JSplitPane();
app.right=new JPanel();
app.left=new JPanel();
app.Scrpane=new JScrollPane();

app.pane2.setRightComponent(app.right);
app.pane2.setLeftComponent(app.Scrpane);

app.pane3=new JScrollPane();
app.browse = new JButton("BROWSE");
app.save = new JButton("SAVE");
app.search = new JButton("SEARCH");
app.field = new JTextField("Enter Text ");
//app.pane2.setOneTouchExpandable(true);
app.pane2.setDividerLocation(700);
app.Scrpane.setLayout(null);
app.Scrpane.setBounds(10, 30, 700, 700);
app.Scrpane.setBackground(Color.white);

app.right.setLayout(null);
app.right.setBounds(700, 30, 700, 700);
app.right.setBackground(Color.white);

app.f.setLayout(new BorderLayout(100,100));
app.f.setBounds(0, 0, 1300, 720);
app.pane1.setLayout(new FlowLayout());

app.pane1.add(app.browse);
app.pane1.add(app.save);
app.pane1.add(app.search);
app.pane1.add(app.field);
app.f.add(app.pane1,BorderLayout.NORTH);
app.f.add(app.pane2,BorderLayout.CENTER);
app.f.add(app.pane3,BorderLayout.SOUTH);

app.browse.addActionListener(app);
app.save.addActionListener(app);
app.search.addActionListener(app);

app.f.setVisible(true);
}

private TreeSelectionListener createSelectionListener() {
return new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
String temp;
TreePath path = e.getPath();

int pathCount = path.getPathCount();

for (int i = 0; i < pathCount; i++) {
System.out.print(path.getPathComponent(i).toString());
if (i + 1 != pathCount) {
System.out.print("|");
}

if (i + 1 == pathCount) {
temp = path.getPathComponent(i).toString();
System.out.println("/n");
System.out.println(temp);
}
}
System.out.println("");
}
};
}

public void actionPerformed(ActionEvent e) {
if (e.getSource() == browse) {
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
File file = chooser.getSelectedFile();
filename = file.getPath();
System.out.println("You have selected: " + filename);

if (filename != "") {
InputStream in;
try {
in = new FileInputStream(new File(filename));
new XmlTree(filename, in);
} catch (FileNotFoundException e1) {

e1.printStackTrace();
}

try {
WindowUtilities.setNativeLookAndFeel();
JTree tree = new JTree();
tree=new XmlTree(filename);
tree.setEditable(true);
Scrpane.add(tree);

tree.addTreeSelectionListener(createSelectionListener());
tree.setBounds(10, 30, 700, 700);
//Scrpane.setPreferredSize(tree.getPreferredSize());
Scrpane.setVisible(true);
} catch (IOException ioe) {
System.out.println("Error creating tree: " + ioe);
}
}
}

if (e.getSource() == save) {
JFileChooser chooser = new JFileChooser();
chooser.showSaveDialog(null);
File f = chooser.getSelectedFile();
String filesave = f.getAbsolutePath();
System.out.println("You have selected to save file: " + filesave);
// new WriteDB(filename,filesave);
}

}
}
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

What are you trying to accomplish with this:

You are just invoking constructor of XmlTree class (which is not standard Java class, by the way, and you didn't provide more information about it). What is the purpose of invoking class constructor if you are not assigning class instance to anything?

Also:

As I recall, there is no WindowUtilities class in Java.

Considering what I wrote above, this is still not a code that could be compiled.

EDIT: And, once again, please put the code between code tags.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
basic rules of scrollpanes:
never use scrollpane.setLayout(null)
never use scrollpane.add(...)
preferredSize of what's in the scrollpane
has to be greater than the preferredSize
of the scrollpane.

too much code for me to wade through so
I'll leave it to you to check what you've done.
kiran cy
Greenhorn

Joined: Aug 24, 2012
Posts: 24
This is the XML tree code

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Comment;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

public class XmlTree extends JTree {
static JTextField field;
static JButton search;

public XmlTree(String filename) throws IOException {
this(filename, new FileInputStream(new File(filename)));
}

public XmlTree(String filename, InputStream in) {
super(makeRootNode(in));
}

private static DefaultMutableTreeNode makeRootNode(InputStream in) {
try {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document document = builder.parse(in);
document.getDocumentElement().normalize();
Element rootElement = document.getDocumentElement();
DefaultMutableTreeNode rootTreeNode = buildTree(rootElement);
return (rootTreeNode);

} catch (Exception e) {
String errorMessage = "Error making root node: " + e;
System.err.println(errorMessage);
e.printStackTrace();
return (new DefaultMutableTreeNode(errorMessage));
}
}

private static DefaultMutableTreeNode buildTree(Element rootElement) {
DefaultMutableTreeNode rootTreeNode = new DefaultMutableTreeNode(
treeNodeLabel(rootElement));
addChildren(rootTreeNode, rootElement);
return (rootTreeNode);
}

private static void addChildren(DefaultMutableTreeNode parentTreeNode,
Node parentXMLElement) {

NodeList childElements = parentXMLElement.getChildNodes();
for (int i = 0; i < childElements.getLength(); i++) {
Node childElement = childElements.item(i);
if (!(childElement instanceof Text || childElement instanceof Comment)) {
final DefaultMutableTreeNode childTreeNode = new DefaultMutableTreeNode(
treeNodeLabel(childElement));
parentTreeNode.add(childTreeNode);
addChildren(childTreeNode, childElement);
/*
search.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e1) {
DefaultTreeModel model = new DefaultTreeModel(
childTreeNode);
DefaultMutableTreeNode node = searchNode(field
.getText()); // Searching the tree to math the
// given string
JTree tree = new JTree(model);
if (node != null) {
// make the node visible by scroll to it
TreeNode[] nodes = model.getPathToRoot(node);
TreePath path = new TreePath(nodes);
System.out.println(path.toString()); // Able to get
// the exact
// node here
tree.setExpandsSelectedPaths(true);
tree.setSelectionPath(new TreePath(nodes));
} else {
System.out.println("Not found");
}
}

private DefaultMutableTreeNode searchNode(String text) {
DefaultMutableTreeNode node = null;
Enumeration e1 = node.children();
while (e1.hasMoreElements()) {
node = (DefaultMutableTreeNode) e1.nextElement();
if ((node.getUserObject().toString())
.contains(text)) {
return null;
}
}

return null;
}
});

*/ }
}
}

private static String treeNodeLabel(Node childElement) {
NamedNodeMap elementAttributes = childElement.getAttributes();
String treeNodeLabel = childElement.getNodeName();
String temp = "";
int j, k = 0;
if (elementAttributes != null && elementAttributes.getLength() > 0) {
int numAttributes = elementAttributes.getLength();
for (int i = 0; i < numAttributes; i++) {
Node attribute = elementAttributes.item(i);
if (attribute.getNodeName() == "id") {
treeNodeLabel = attribute.getNodeValue();
k = 1;

}
}

if (k == 1) {
treeNodeLabel = temp + " (";
k = 0;
}

else {
treeNodeLabel = treeNodeLabel + "(";
}

for (j = 0; j < numAttributes; j++) {
Node attribute = elementAttributes.item(j);
if (j > 0) {
treeNodeLabel = treeNodeLabel + ", ";
}
treeNodeLabel = treeNodeLabel + attribute.getNodeName() + "="
+ attribute.getNodeValue();
}
treeNodeLabel = treeNodeLabel + ")";
}

return (treeNodeLabel);
}
}

class ExitListener extends WindowAdapter {
public void windowClosing(WindowEvent event) {
System.exit(0);
}
}

class WindowUtilities {

public static void setNativeLookAndFeel() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
System.out.println("Error setting native LAF: " + e);
}
}

public static void setJavaLookAndFeel() {
try {
UIManager.setLookAndFeel(UIManager
.getCrossPlatformLookAndFeelClassName());
} catch (Exception e) {
System.out.println("Error setting Java LAF: " + e);
}
}

public static void setMotifLookAndFeel() {
try {
UIManager
.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
} catch (Exception e) {
System.out.println("Error setting Motif LAF: " + e);
}
}

}
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
which looks better, and easier to read?

public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello World");
}
}
or


try editing your post, highlight your code, then click the 'code' button,
which is one of the buttons above the textArea you type into.
*then* click the 'preview' button to ensure it's done correctly, then submit (or edit?)
kiran cy
Greenhorn

Joined: Aug 24, 2012
Posts: 24
I got it.Thank you one and all for your patience.
Anyway this is the code
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Scrollbar is not visible