0

I would like to create a JTree to use in various panels in different parts of my project, therefore, I was trying to create a JTree class to be able to import it later in another frames.

Can you please suggest how to make it workable

This is the class that contains a JScrollPane and a default JTree

public class UserJTree extends Component{


public UserJTree(){

    initialize();

}
void initialize(){

JScrollPane scrollPane = new JScrollPane();
JTree tree = new JTree();
    //rest of the JTree implementation 

tree.setBounds(6, 6, 145, 500);
scrollPane.setViewportView(tree);
}

Component getUserTree(){

    return this;

} 

 } 

This is the panel in another class in which I wish to put my JTree

    UserJTree ujt = new UserJTree();
    JPanel panel_3 = new JPanel();
    panel_3.setBounds(26, 51, 157, 512);
    panel_3.setLayout(null);
    panel_3.add(ujt.getUserTree());
    newUserFrame.getContentPane().add(panel_3); 
QGA
  • 3,114
  • 7
  • 39
  • 62
  • 3
    A component can only have one parent container. Instead have each table object share the same model. – Paul Samsotha Jan 29 '14 at 11:45
  • Thank you - How can I make it? – QGA Jan 29 '14 at 11:52
  • 1
    @peeskillet ITYM [How to Use ***Trees***](http://docs.oracle.com/javase/tutorial/uiswing/components/tree.html).. – Andrew Thompson Jan 29 '14 at 22:30
  • 1
    `panel_3.setBounds(26, 51, 157, 512); panel_3.setLayout(null);` Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Jan 29 '14 at 22:31
  • 1
    @AndrewThompson oops.... Thanks. Yes, you are absolutely correct. – Paul Samsotha Jan 30 '14 at 02:25
  • Thank you for your tips, however, I Have not received a satisfactory answer so far. What I thought to do is to create a class called Example which extends Component. Afterwards, I wanted to put a method in the constructor of this class to create automatically a set of components (In my example a JScrollPane and a JTree). Therefore, if I create an object of the class Example, in theory I should be able to pass the created object to the constructor of a panel (for instance). Why is this approach not possible? – QGA Jan 30 '14 at 12:25
  • (For Instance) Example ex = new Example(); panel.add(ex); – QGA Jan 30 '14 at 12:34

0 Answers0