How to list JComponents from GridBagLayout?

坚强是说给别人听的谎言 提交于 2019-12-11 19:13:19

问题


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.


回答1:


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]



回答2:


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.



来源:https://stackoverflow.com/questions/21493847/how-to-list-jcomponents-from-gridbaglayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!