• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Scrollbar is not visible

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
}
}
 
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should post complete code, or at least something that the one is able to compile.
Also post you code between code tags.
 
kiran cy
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK if it is not what you say it is guide me step by step(literally).
 
Kemal Sokolovic
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's start with this:


Where is Main class definition?
 
kiran cy
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it.Thank you one and all for your patience.
Anyway this is the code
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic