How to add buttons/text to right pane when selection is made from left pane

Hi,
I am new to JAVA development with SWING.
I am trying to create an application where in the left pane there is a Tree Menu and in the right pane I have some buttons. Based on the selections in the left Pane, I should be able to add more buttons and or text to the right pane.
How do I accomplish this? If you guys have any sample code, Please post it or suggest different ways of accomplishing.
Thanks in advance.
user2325986

It looks like you are declaring different main_Frame, rightPane and leftPane and have too many items static and final. Take a look at the code below. I have set GridBagLayout for the frame for a better look.
I am not sure what you mean when you say that you want right panel only and want to add to it. Also, I think you would be better off using CardLayout for rightPanel as stated by jduprez
When you want to show code use {_code_} code here {_code_} tags (code in brackets without underscores)
Here is a working sample of what you had:
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import javax.swing.tree.*;
public class Main
    public static void main(String[] args)
        main_Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        main_Frame.setVisible(true);
    public static TestFrame main_Frame = new TestFrame();
class TestFrame extends JFrame
    public TestFrame()
        setTitle("Test Frame");
        setSize(500, 500);
        setLayout(new GridBagLayout());
        leftGBC = new GridBagConstraints();
        leftGBC.gridx = 0;
        leftGBC.gridy = 0;
        leftGBC.fill = leftGBC.BOTH;
        leftGBC.weightx = 10;
        leftGBC.weighty = 100;
        rightGBC = new GridBagConstraints();
        rightGBC.gridx = 1;
        rightGBC.gridy = 0;
        rightGBC.fill = rightGBC.BOTH;
        rightGBC.weightx = 100;
        rightGBC.weighty = 100;
        leftPanel = new JPanel();
        leftPane = new JScrollPane(leftPanel);
        leftPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        leftPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        rightPanel = new JPanel();
        rightPane = new JScrollPane(rightPanel);
        rightPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        rightPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("main_Tree");
        DefaultMutableTreeNode Branch1 = new DefaultMutableTreeNode("Branch1");
        DefaultMutableTreeNode Leaf1 = new DefaultMutableTreeNode("Leaf1");
        root.add(Branch1);
        Branch1.add(Leaf1);
        DefaultMutableTreeNode Branch2 = new DefaultMutableTreeNode("Branch2");
        DefaultMutableTreeNode Leaf2 = new DefaultMutableTreeNode("Leaf2");
        root.add(Branch2);
        Branch2.add(Leaf2);
        JTree tree = new JTree(root);
        tree.setRootVisible(true);
        tree.setShowsRootHandles(true);
        tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        tree.addTreeSelectionListener(new TreeSelectionListener()
            public void valueChanged(TreeSelectionEvent se)
                JTree tree = (JTree) se.getSource();
                DefaultMutableTreeNode node = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
                if (node == null) return;
                String nodeInfo = node.toString();
                if (nodeInfo.equals("Leaf1"))
                    rightPanel = new JPanel();
                    rightPanel.add(label);
                    Main.main_Frame.remove(rightPane);
                    rightPane = new JScrollPane(rightPanel);
                    rightPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
                    rightPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
                    Main.main_Frame.add(rightPane, rightGBC);
                    Main.main_Frame.validate();
                    Main.main_Frame.repaint();
                if (nodeInfo.equals("Leaf2"))
                    rightPanel = new JPanel();
                    rightPanel.add(Jbt2);
                    Main.main_Frame.remove(rightPane);
                    rightPane = new JScrollPane(rightPanel);
                    rightPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
                    rightPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
                    Main.main_Frame.add(rightPane, rightGBC);
                    Main.main_Frame.validate();
                    Main.main_Frame.repaint();
        DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tree.getCellRenderer();
        tree.setRowHeight(30);
        renderer.setLeafIcon(new ImageIcon("blue-ball.gif"));
        renderer.setOpenIcon(new ImageIcon("red-ball.gif"));
        renderer.setClosedIcon(new ImageIcon("yellow-ball.gif"));
        renderer.setFont(new Font("Monospaced",Font.BOLD|Font.ITALIC,15));
        renderer.setTextNonSelectionColor(Color.blue);
        renderer.setTextSelectionColor(Color.white);
        renderer.setBackgroundNonSelectionColor(Color.white);
        renderer.setBackgroundSelectionColor(Color.gray);
        renderer.setBorderSelectionColor(Color.lightGray);
        leftPanel.add(tree);
        add(leftPane, leftGBC);
        add(rightPane, rightGBC);
    private JPanel leftPanel;
    private JPanel rightPanel;
    private JScrollPane leftPane;
    private JScrollPane rightPane;
    private GridBagConstraints leftGBC;
    private GridBagConstraints rightGBC;
    private JButton Jbt1 = new JButton("Button1");
    private JButton Jbt2 = new JButton("Button2");
    private JLabel label = new JLabel("Enter your message below");
    private JTextArea jta = new JTextArea(10,15);
    private JButton Jbt3 = new JButton("Button3");
    private JButton Jbt4 = new JButton("Button4");
}

Similar Messages

  • How to add a text box in the layout (View)

    How to add a text box in the layout (View)

    Hi.
    Right Click in the RootUIElement container.
    Click Insert Element.
    Give Name: Input
    Type : Input Field.
    Press Enter.
    Regards
    Bala.

  • How to add standard text in smartforms?

    Hi friends,
    How to add standard text in smartforms ?
    points rewarded soon
    Regards
    RH

    Creating the Standard Text:
    Go to SO10
    1. Enter Text Name.  For example, ZABC
    2. Text ID ST
    3. Language EN
    Click on Create.
    And enter the Required Information and save.
    To include a Standard text in SmartForm.
    Follow the steps below:
    1. Create a text node in the window.
    2. Under general attributes select "Include text" as Type.
    3. Specify the fields under Text.
    Text Name ZABC
    Text Object TEXT
    Text ID ST
    Language EN.
    Hope it helps.
    Regards.

  • How to add new text field in standard report

    how to add new text field in standard report?

    Hi,
    I presume you are talking about a report display in ALV and u wish to add a column to it .
    If it is a global requirement ,as in table being used there in ALV can be modified, then you can append the table and the system should pick up the same automatically from there.
    Otherwise , you can make a Z program . Modify the catalog being used in ALV.
    Regards,
    Shweta

  • How to add standard text in PO and how it is maintained ?

    How to add standard text (paragraph) in PO and how it is maintained ?

    Hi
    Please go through this document.which has a step by step
    Working with Frequently Used Texts
    Use
    You can copy texts from various sources to create PO texts:
    Standard texts
    Certain standard texts are supplied with the system and can be assigned to a purchasing document in Customizing.
    The content of some standard texts is predefined by your system administrator.
    You can also create standard texts yourself and copy them into purchasing documents.
    Master records
    Database fields
    Activities
    Creating Standard Texts
    You can create your own standard texts via Tools ® SAPscript ® Standard text.
    Inserting Standard Texts
    You can insert your standard text on the long text screen via Insert ® Text ® Standard.
    For more information, refer to the Basis documentation under BC Word Processing with the SAPscript Editor:  Including Texts.
    You can display the print preview via Text ® Print ® Print preview.
    Inserting Texts from the Master Record
    There are three ways of linking texts from a master record with a purchasing document. The relevant settings are made for each text type in Customizing for Purchasing. The indicator in the status column shows the nature of the linkage.
    Option                                              Indicator in status column
    The text is copied:               
    It is then independent of the text                 None
    in the master record. Changes to
    the text in the material master record
    have no influence on the text in the document.
    The text is displayed only:
    It cannot be adopted.                                          N
    The text is offered for copying:
    It can be copied via Header or Item ® Texts ® Adopt text. (In requisitions: Goto ® Texts ® Adopt text.) Changes made in the master record will also be adopted in the document until such time as you copy the text.
    Inserting Texts from Database Fields
    You can include the content of a database field at any point in the text on the long text screen. A value specific to the relevant document is displayed in the printed document.
    Choose Insert ® Symbols ® Program symbols.
    Choose DDIC fields.
    Search for the relevant table/structure (in the case of PO items, this is EKPO) and then choose ENTER .
    Choose the relevant table field.
    Choose Text ® Print preview to see the print preview.
    Reward if useful
    Aasif

  • Urgent - How to add buttons to a Table

    How to add buttons to a Table and enable them for Mouse Listeners/ Action Listeners

    extends the defaultcellrenderer make it return a Jbutton as the component to draw.
    class OverCellRendererClass extends DefaultTableCellRenderer {
    public Component getTableCellRendererComponent(JTable table,
    Object value,
    boolean isSelected,
    boolean hasFocus,
    int row,
    int column) {
    //put your stuff here to make or get a button
    return myButton;
    Use something like this to set the renderer for the column :
    tb.getColumnModel().getColumn(4).setCellRenderer(new YourCellRendererClass());

  • How to add interface to customlize MXML Component when use Flex Builder 3?

    How to add interface to customlize MXML Component when use
    Flex Builder 3?

    David,
    I don't believe you can add the interface via the creation
    dialog in FlexBuilder 3. You can always manually add the
    "implements" property to your MXML Component root tag. Something
    like this: <mx:VBox implements="com.mycorp.IMyInterface">
    If you want autogeneration of the interface, then create an
    ActionScript class with that interface and then copy the generated
    functions and setter/getters into the script block of your MXML
    component.

  • Text getting cut off when printing to PDF from Excel

    Hello,
         I've seen a lot related to this subject but nothing that shows a real solution.
         Using Excel 2003-sp3 and Adobe Acrobat v.9 Standard, the right half of the last character in the string gets cut off. It prints from excel fine.
         It's not always the text that is the right-most on the page either. An affected PDF file is attached - note the cut off "S" on the last line (NET ASSETS OF GOVERNMENTAL ACTIVITIES).
         Any assistance is appreciated.
    Thanks,
    Kevin Murphy

    please delete me from your mailing list.  tks.
    Date: Wed, 10 Feb 2010 11:20:28 -0700
    From: [email protected]
    To: [email protected]
    Subject: Text getting cut off when printing to PDF from Excel
    Hello,
         I've seen a lot related to this subject but nothing that shows a real solution.
         Using Excel 2003-sp3 and Adobe Acrobat v.9 Standard, the right half of the last character in the string gets cut off. It prints from excel fine.
         It's not always the text that is the right-most on the page either. An affected PDF file is attached - note the cut off "S" on the last line (NET ASSETS OF GOVERNMENTAL ACTIVITIES).
         Any assistance is appreciated.
    Thanks,
    Kevin Murphy
    >

  • How can i view a list of previous purchases i have made from the appstore on my iphone that i have now deleted?

    how can i view a list of previous purchases i have made from the appstore on my iphone that i have now deleted?

    Launch iTunes on your computer. Select iTunes Store in the source list on the left then click Sign In right side of the iTunes window just above QUICK LINKS.
    In the Account Information window click See All to the right of Purchase History.

  • How do you mark a message as spam when your mail comes from gmail via OS X Mail.app?

    How do you mark a message as spam when your mail comes from gmail via OS X Mail.app?
    I use IMAP to get Gmail. I seem to remember seeing a "[Gmail]/Spam" IMAP folder in previous versions of the app, but that's not visible any more.
    Without this, I have to log on to the web-based Gmail to mark messages as spam, that's a productivity killer.

    right mouse click on a message and select Mark as Junk Mail

  • How can I set up my email so when I delete mail from my Mac it deletes from my iPhone as well?

    How can I set up my email so when I delete mail from my Mac it deletes from my iPhone as well?

    That is a function of your email account, if it is an Imap account it will sync, if it's a POP account, it won't.
    Who provides your email.

  • How do I get rid of "unreadable files" when I am importing from my camera?

    How do I get rid of "unreadable files" when I am importing from my camera?

    Are all images unreadable? What is the exact error message you are getting?
    If only a few images are unreadable, try to import not all photos at once, but in smaller batches, until you know, which photo it is that cannot be read. Import all photos that can be read and reformat your card in the camera to remove the unreadable files.
    But if all photos are unreadable, try, if Image Capture can read your card.
    Is it a new camera? Have you been able to import from that camera before?

  • How many WHEREs do we need in minimun when we have 4 FROMs in the statement

    How many WHEREs do we need in minimun when we have 4 FROMs <tables> in the statement?

    Technically none - but it produces cartesian join:
    SQL> SELECT * FROM scott.emp, scott.dept, scott.salgrade;
         EMPNO ENAME      JOB               MGR HIREDATE        SAL       COMM
        DEPTNO     DEPTNO DNAME          LOC                GRADE      LOSAL
         HISAL
          7788 SCOTT      ANALYST          7566 87/04/19       3000
            20         40 OPERATIONS     BOSTON                 3       1401
          2000
          7839 KING       PRESIDENT             81/11/17       5000
            10         40 OPERATIONS     BOSTON                 3       1401
          2000
         EMPNO ENAME      JOB               MGR HIREDATE        SAL       COMM
        DEPTNO     DEPTNO DNAME          LOC                GRADE      LOSAL
         HISAL
    280 wierszy zosta│o wybranych.

  • How is the album and folder connected and when I delete photos from the album it also deletes from the folder?

    How is the album and folder connected and when I delete photos from the album it also deletes from the folder?

    In what application? iPhoto? If so: what version?

  • How to add a text area in a Content pane..?

    Hi,
    I created a content pane with 5 buttons. One of them is a quit button, and I was able to create an "system.exit(0)" event handling for him. My problem is.. I have 4 other buttons, and I want them to show some text when I click them. How do I add a text area bellow my content pane..?
    Copy/paste my code to see what im talking about :) :
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class Interface extends JFrame {
    public Interface() {
    Container contentPane = getContentPane();
    contentPane.setLayout(new FlowLayout());
    JButton button1 = new JButton("Test1");
    JButton button2 = new JButton("Test2");
    JButton button3 = new JButton("Test3");
    JButton button4 = new JButton("Test4");
    JButton button5 = new JButton("Quit");
    ButtonHandler handler = new ButtonHandler();
    button5.addActionListener( handler );
    contentPane.add(button1);
    contentPane.add(button2);
    contentPane.add(button3);
    contentPane.add(button4);
    contentPane.add(button5);
    contentPane.setBackground(Color.orange);
    addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    System.exit(0);
    public static void main(String args[]) {
    Interface window = new Interface();
    window.setLocation(250,350);
    window.setTitle("FlowLayout");
    window.pack();
    window.setTitle("Test");
    window.setVisible(true);
    public class ButtonHandler implements ActionListener {
    public void actionPerformed( ActionEvent e )
    System.exit(0);
    Thanks alot! :)

    By default the content pane of the JFrame uses a Border Layout. So you should:
    1) Create a JPanel
    2) Set the layout of the JPanel to FlowLayout
    3) add the buttons to the panel
    4) add the panel to the content pane
    5) add your text area to the content pane
    Read this section from the Swing tutorial on "Using Layout Managers":
    http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html

Maybe you are looking for

  • Don't really know how to calculate an Azure back up Solution

    Hello, Im new on Azure, and Im trying to implement a backup solution for a server we have running on Windows Server 2008. Do I have to calculate the estimated price based on the characteristics of my Server (I suppose I have to turn on a VM with the

  • How to Change color on control break fields

    I have created a master detail report. I then did a control bread on the three fields below. I would like to change the color of the break to blue instead of the standard black. What settings or method would I use to change the color on the control b

  • Data Change Condition Expression

    I need to create a product specification that includes dependencies between order components based on a data change notification wait condition How do I define the XQuery expression in the "Data Change Condition Expression" field? Specifically, I hav

  • Autoaccounting ---Getting Task id for Invoice/revenue function.

    Dear All, I have requirement that UER/UBR should be picked up based on task id during invoice creation. But whenever I am selecting task ID parameter in rule of AA under Invoice/Revenue Function I am getting it's not allowed for this function. Can an

  • BPEL implement while-continue logic

    Greetings, In java, one could use the continue statement to skip current iteration within a while, for or do-while loop. Is it possible to implement similar flow control in BPEL within in a while activity? Thanks.