简体   繁体   中英

How to list JComponents from GridBagLayout?

I am trying to figure out how to print the component names and/or their values after a button is pressed. I currently am using a GridBagLayout with 2 columns and 6 rows but I don't know how to traverse the layout or anything like that inside of my actionPerformed() method. I think it may have to do with getContentPane() but I'm not entirely sure.

在此处输入图片说明

In the following code, I print:

            System.out.println(comp.getClass().getSimpleName() + 
                    " Bounds: " + comp.getBounds());

But for a much more comprehensive view of the component, change that to:

            System.out.println(comp);

import java.awt.*;
import java.awt.event.ActionEvent;
import javax.swing.*;

class ListComponents {

    public static void listComponents(Container c) {
        Component[] components = c.getComponents();
        for (Component comp : components) {
            System.out.println(comp.getClass().getSimpleName() + 
                    " Bounds: " + comp.getBounds());
            if (comp instanceof Container) {
                listComponents((Container)comp);
            }
        }
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                final JPanel gui = new JPanel(new BorderLayout(3,3));

                JTree tree = new JTree();
                tree.setVisibleRowCount(8);
                gui.add(new JScrollPane(tree), BorderLayout.LINE_START);

                JToolBar tb = new JToolBar();
                gui.add(tb, BorderLayout.PAGE_START);
                Action list = new AbstractAction("List") {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        listComponents(gui);
                    }
                };
                tb.add(list);
                tb.add(new JToggleButton("Toggle"));
                tb.add(new JCheckBox("Check"));

                gui.add(new JScrollPane(new JTextArea("Default Text",3,20)));

                JOptionPane.showMessageDialog(null, gui);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency
        SwingUtilities.invokeLater(r);
    }
}

Output when tool-bar is on GUI

JScrollPane Bounds: java.awt.Rectangle[x=0,y=35,width=81,height=147]
JViewport Bounds: java.awt.Rectangle[x=1,y=1,width=78,height=144]
JTree Bounds: java.awt.Rectangle[x=0,y=0,width=78,height=144]
CellRendererPane Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]
ScrollBar Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]
MetalScrollButton Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]
MetalScrollButton Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]
ScrollBar Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]
MetalScrollButton Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]
MetalScrollButton Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]
JToolBar Bounds: java.awt.Rectangle[x=0,y=0,width=307,height=32]
 Bounds: java.awt.Rectangle[x=16,y=1,width=33,height=28]
JToggleButton Bounds: java.awt.Rectangle[x=49,y=1,width=50,height=28]
JCheckBox Bounds: java.awt.Rectangle[x=99,y=1,width=65,height=28]
JScrollPane Bounds: java.awt.Rectangle[x=84,y=35,width=223,height=147]
JViewport Bounds: java.awt.Rectangle[x=1,y=1,width=220,height=144]
JTextArea Bounds: java.awt.Rectangle[x=0,y=0,width=220,height=144]
ScrollBar Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]
MetalScrollButton Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]
MetalScrollButton Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]
ScrollBar Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]
MetalScrollButton Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]
MetalScrollButton Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]

Output when tool-bar is dragged off GUI

JScrollPane Bounds: java.awt.Rectangle[x=0,y=0,width=81,height=182]
JViewport Bounds: java.awt.Rectangle[x=1,y=1,width=78,height=179]
JTree Bounds: java.awt.Rectangle[x=0,y=0,width=78,height=179]
CellRendererPane Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]
ScrollBar Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]
MetalScrollButton Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]
MetalScrollButton Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]
ScrollBar Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]
MetalScrollButton Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]
MetalScrollButton Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]
JScrollPane Bounds: java.awt.Rectangle[x=84,y=0,width=223,height=182]
JViewport Bounds: java.awt.Rectangle[x=1,y=1,width=220,height=179]
JTextArea Bounds: java.awt.Rectangle[x=0,y=0,width=220,height=179]
ScrollBar Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]
MetalScrollButton Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]
MetalScrollButton Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]
ScrollBar Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]
MetalScrollButton Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]
MetalScrollButton Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]

Assuming everything is contained inside a java.awt.Component , you can call getComponents() on the container. This will give you an array of Component objects which you can iterate over to print out whatever you like.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM