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

Hello

I am new to the JAVA swing development.

I am creating an application where in the pane on the left, there is a tree Menu and in the right pane, I have a few buttons. In view of the options in the left pane, I should be able to add more buttons and or the text to the right pane.

How to achieve this? If you guys have any example code, please post or suggest ways to accomplish.

Thanks in advance.
user2325986

It seems that you declare leftPane, rightPane and different main_Frame and have too much static and final elements. Look at the code below. I put GridBagLayout to the framework for a better look.

I don't know what you mean when you say that you only want to right panel and you want to add. Also, I think you'd be better off using CardLayout to right panel as stated jduprez

When you want to display using code {_code_} code here {_code_} (code in parentheses without underscores) tags

Here is a working example 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");
}

Tags: Java

Similar Questions

  • How to align my text on the LEFT in this custom component?

    package {
         import mx.controls.LinkButton;
         import flash.text.TextLineMetrics;
     
         public class multiLineLinkButton extends LinkButton {
              override protected function createChildren():void {
                   super.createChildren();
                   if (textField){
                        textField.wordWrap = true;
                        textField.multiline = true;
                        
                   }
              }
     
              override public function measureText(s:String):TextLineMetrics {
                   textField.text = s;
                   var lineMetrics:TextLineMetrics = textField.getLineMetrics(0);
                   lineMetrics.width = 700;
                   lineMetrics.height = textField.textHeight;
                   
                   
                   return lineMetrics;
              }
         }
    }
    

    I found this component that encapsulates LinkButton.

    When I use this component it makes centered

    I tried autosize up there, but it doesn't work here.

    Try this-

    Just add these lines.

    public function multiLineLinkButton (): void {/ / constructor}
    this.setStyle ("textAlign", "left");
    }

  • In the left pane of Windows Live Mail, my four email accounts including pointed "Windows Live Mail, just above Gmail, but now they're gone."

    In the left pane of Windows Live Mail, my four email accounts including pointed "Windows Live Mail, just above gmail.   He disappeared somehow (I probably errased it instead of an email.)  and I would like to know how to back this account to the left pane. This would indicate how many emails were waiting in the Inbox.  If there is a suitable forum that I missed, FEAL free to direct me there.

    original title: account list in windows live mail.

    Please repost in Windows Live Solution Center.

    Mail-forum - section of son of mail:

    http://windowslivehelp.com/forums.aspx?ProductID=15

  • Added option to delete in the left pane of the screen of the Documents

    In Windows XP, I always find option DELETE the left of documents, it is easy to delete a folder or file. In Windows 7, I have to use the DELETE key or I have to right click on aDossier or a file, and then choose Delete... it consumes a little more time and not a direct approach.

    Is it possible that I can add this option to the left pane of the screen.

    Thanks much for all help that you have provided.

    No, the toolbar of Windows Explorer can not be customized in the way that the XP you could so you cannot add anything him.

  • How to add a tail to the end of a text in photoshop?

    How to add a tail to the end of a text in photoshop?

    I tested the police (from dafont.com) in the real Photoshop on a Windows 7/32 bit computer.

    It is all ok.

    Fenja

  • How to add an outline to the text?

    How to add an outline to the text?

    Add a line in the character of the text layer settings.

  • How can I add a video iPhone portrait on the right with text on the left?

    Hello

    I'm new to the premier pro and I have had a look online but can't find the instructions on adding a video portrait on the right with text on the left. I found information on add a title on top and using blending modes, but how can I move the iPhone video portrait to the right of the screen to maintain the proportion of the landscape?

    Thank you

    Jess

    Basically, you use the motion in the effect controls panel effect, specifically the balance and Position. The next page has links to video tutorials both on photo in photo workflows:

    Adobe Premiere Pro help. Create common results

  • How to add buttons in the script, and then click on run different codes?

    If we run the script, then it should display ok, cancel button 6 button. If we click on button1 coding with button1 dangerousness must be running and so on.

    Button1

    Button2

    Button3

    Button4

    button5

    Button6

    OK Cancel.

    Marie rosine

    // DialogSimpleButton.jsx
    // http://forums.adobe.com/thread/1327372?tstart=0
    // how to add buttons in script, which on click run different codes?
    
    // regards pixxxelschubser
    
    function SimpleDialog() {
    
    var w = new Window('dialog', '');
    this.windowRef = w;
    w.orientation = "column";
    
    btn1 = w.add('button',undefined,'Button 1');
    btn2 = w.add('button',undefined,'Button 2');
    btn3 = w.add('button',undefined,'Button 3');
    btn4 = w.add('button',undefined,'Button 4');
    btnOK = w.add('button',undefined,'OK');
    btnCancel = w.add('button',undefined,'Cancel');
    
    btn1.onClick = function() {alert("Button 1");};
    btn2.onClick = function() {alert("Button 2");};
    btn3.onClick = function() {alert("Button 3");};
    btn4.onClick = function() {alert("Button 4");};
    
    defaultElement = btnOK;
    cancelElement = btnCancel;
    btnOK.active = true;
    
    btnOK.onClick = function() {
        stuff = "your stuff";
        w.close();
        }
    
    w.show();
    }
    
    var stuff = null;
    SimpleDialog ();
    if (stuff) alert(stuff);
    
  • How to add buttons to the Quick Launch toolbar

    I can't find the buttons for the "switch between windows" Flip 3D previews and the 'show desk top' on the Quick Launch toolbar.  They used to be there.  Where can I find them so I can put them on the quick launch area?  I have Vista Home Premium.

    Try this:

    1. click on start
    2. in the search field, type the following (you can copy and paste for accuracy):
     
    %SystemDrive%\Users\Default\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch
     
    3. Windows Explorer should open the phonebook.
    4. you should now see the missing shortcuts in the left pane.
    5. left click shortcuts and drag them one at a time to the Quick Launch bar. If you want to reposition shortcuts, just left click and drag them to the position.

  • How to add simple text to document programmly?

    Can not know. How to add simple text to document programmly? Could not find any information on this subject in Communication monitor QAnywhere.

    OK, I got it. In this case the text form is better :-) Thank you!

  • How to add a text anchor point

    I just discovered the function of cross-references in InDesign, but I want my cross reference to point to the text anchor point but for the life of me I can't work out how to add a text anchor point.

    So how can you add an anchor to the text?

    Thank you.

    Highlight the text you want to be the anchor point. Open the hyperlinks Panel. In the Fisher panel choose the new Destination of the hyperlink, then the dialog box should appear with the anchor text and your highlighted text information. Click OK.

  • I use the full version of DASYLab 8.0, how to add PID module in the design of the façade (window layout)? while I can vary the p, i, d values in the window layout

    I use the full version of DASYLab 8.0, how to add PID module in the design of the façade (window layout)? while I can vary the p, i, d values in the window layout

    To do that you will need to change the varibles PID values and then use the sliders to add numbers to a latch for variables that point.

    This is how I think it works in Version 8.

    In all cases, you should move on to 12 more stable summer.

    In addition, unless that is a very slow process, I generally do not like closed loop PID with DASYLab due to the fact that computers is not reliable with timing.

  • How to add more application under the new tab shortcuts when we right click on the desktop.

    How to add more application under the new tab shortcuts when we right click on the desktop.

    Hello

    I suggest you to post your questions in the forums 'Development of the User Interface for Windows Desktop' for better assistance.

    Here is the link: http://social.msdn.microsoft.com/Forums/en-US/windowsuidevelopment/threads

  • How to add a shortcut in the taskbar next to the Start Menu?

    I would like to know how to add a shortcut in the taskbar next to the Start Menu (a place with a small shortcut icon).

    Does anyone have any suggestions?
    Thanks in advance for your suggestions

    Hello

    Have a look here: http://www.sevenforums.com/tutorials/397-taskbar-pin-unpin-program.html

    When you scroll down on this page, you will find a lot more "pin to taskbar" - tutorials.

    Take a look at this little program free of practical assistance, too: http://winaero.com/comment.php?comment.news.108 of the taskbar Pinner

    I hope this helps.

  • How to add 2 points on the Noel e? I don't have a 2nd series of digits to the right of the keyboard.

    How to add 2 points on the letter e in the Noel case?  I don't have the 2nd series of digits to the right of the keyboard. Windows 8

    Wednesday, April 23, 2014 23:25:53 + 0000, RobertNortonWZ wrote:

    How to add 2 points on the letter e in the Noel case?  I don't have the 2nd series of digits to the right of the keyboard. Windows 8

    You mean you want the e character? These two points are called a
    umlaut. You do not add points; the e with points is another
    a simple e character. There are several ways to get this
    character, but a simple is to hold the ALT key and type 137.

    As a point of information if you are interested, the subject of a
    umlaut is to indicate that the second of the two successive vowels is
    pronounced separately. So the word 'book' is pronounced buk, but the
    Book name is pronounced BO - ock.

Maybe you are looking for