可以获得父边框或选项卡的名称吗?

Can one get the name of the parent border, or tab?

我正在尝试在我们的系统中记录一些信息,为了使其易于阅读,我需要记录特定复选框所在的容器的名称。

比如我在一个面板中有一个复选框,那么这个面板就是一个tabbed pane。我想记录“tab name/panel border name/check box name”

这似乎很难。我有复选框,然后 getParent(),但我似乎没有比这更进一步了。

更新

我所说的名字是指标签顶部显示的名字;实际上我有很多不同的容器,我需要记录它们的所有名称(不是程序员给出的实际名称,而是 GUI 上显示的名称)。因此,对于 JPanel,它将是边框名称,而对于其他一些容器,它将是其他名称。

容器(由 getParent() 返回)是一个 AWT Component,并且有一个 getName() 方法 returns 组件的名义名称。但是,我认为您会发现名称字段通常是 ​​null ... 除非您之前已经努力设置它。

另一方面,如果您想获取容器的类名,可以通过调用 parentObj.getClass().getName().

来获取。

By name I meant the name shown at the top of tab.

好吧,我认为没有通用的方法可以做到这一点。您认为组件的 "name" 可能是多种不同的东西,具体取决于组件是什么。它甚至可能不是父对象的一部分。

某些组件 类 有一个 getText() 方法,可能适合用作名称。其他的没有合适的。

使用 Swing/AWT API 的反射和方法实现以下目标非常简单:

  • 复选框文本。
  • 显示复选框的面板的(的)标题边框。
  • 显示面板的选项卡的标题。


import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.*;

public class PrettyPrintControlChange {

    private JComponent ui = null;
    static final String rightwards = new String(Character.toChars(8594));

    PrettyPrintControlChange() {
        initUI();
    }

    public final void initUI() {
        if (ui != null) {
            return;
        }

        ui = new JPanel(new BorderLayout(4, 4));
        ui.setBorder(new EmptyBorder(4, 4, 4, 4));

        JTabbedPane tabbedPane = new JTabbedPane();
        ui.add(tabbedPane);

        final JTextArea textArea = new JTextArea(5, 45);
        ui.add(new JScrollPane(textArea), BorderLayout.PAGE_END);

        ActionListener al = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JCheckBox cb = (JCheckBox) e.getSource();
                textArea.append(getPrettyCheckBoxString(cb) + "\n");
            }
        };

        for (int i = 1; i < 3; i++) { //tabs
            JPanel tabPanel = new JPanel(new GridLayout(0, 1, 4, 4));
            tabbedPane.addTab("Tab " + i, tabPanel);
            for (int j = 1; j < 4; j++) { // panels with titled border
                JPanel titledPanel = new JPanel(new GridLayout(1, 0, 4, 4));
                titledPanel.setBorder(new TitledBorder("Panel " + j));
                tabPanel.add(titledPanel);
                for (int k = 1; k < 7; k++) { // check boxes
                    JCheckBox checkBox = new JCheckBox("Check Box " + k);
                    checkBox.addActionListener(al);
                    titledPanel.add(checkBox);
                }
            }
        }
    }

    /**
     * Provides a string representing the state and containment hierarchy of a
     * check box. Uses the text of the check box, titled borders and tabbed pane
     * tab in which the check box appears to identify it.
     */
    private static String getPrettyCheckBoxString(JCheckBox cb) {
        StringBuilder sb = new StringBuilder("Check Box: ");
        ArrayList<Container> containerHeirarchy = new ArrayList<Container>();
        containerHeirarchy.add(cb);

        Container parent = cb.getParent();
        boolean foundTabbedPane = false;
        while (parent != null && !foundTabbedPane) {
            if (parent instanceof JTabbedPane) {
                foundTabbedPane = true;
            }
            containerHeirarchy.add(parent);
            parent = parent.getParent();
        }
        // traverse the collection in revers order.
        for (int i = containerHeirarchy.size() - 1; i >= 0; i--) {
            Container c = containerHeirarchy.get(i);
            if (c instanceof JTabbedPane) {
                JTabbedPane tp = (JTabbedPane) c;
                String title = tp.getTitleAt(tp.getSelectedIndex());
                sb.append(" tab: " + title);
            } else if (c instanceof JPanel) {
                JPanel panel = (JPanel) c;
                Border border = panel.getBorder();
                if (border instanceof TitledBorder) {
                    TitledBorder titledBorder = (TitledBorder) border;
                    String title = titledBorder.getTitle();
                    sb.append(" " + rightwards + " panel: " + title);
                }
            } else if (c instanceof JCheckBox) {
                JCheckBox checkBox = (JCheckBox)c;
                String title = checkBox.getText();
                sb.append(" " + rightwards + " check box: " + title);
            }
        }

        return sb.toString();
    }

    private static void prettyPrintCheckBox(JCheckBox cb) {
        System.out.println(getPrettyCheckBoxString(cb));
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                PrettyPrintControlChange o = new PrettyPrintControlChange();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}