Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For this:</p> <blockquote> <p>would prefer not to have to write out a listener for every button</p> </blockquote> <p>Create a method that accept the <code>JPanel</code> as the parameter and then iterate to each component in <code>JPanel</code>, add the action listener if the component is <code>JButton</code></p> <p>This one:</p> <pre><code>I was wandering if I could just get the text contained in the button. </code></pre> <p>Just set the value in the <code>JLabel</code>. You already get the button content right?</p> <p>So, the full code is:</p> <pre><code>import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; import java.awt.*; public class filecabinet extends JFrame implements ActionListener { private String folder = ""; // create buttons private JButton a = new JButton("A"); private JButton b = new JButton("B"); private JButton c = new JButton("C"); private JButton d = new JButton("D"); private JButton e = new JButton("E"); private JButton f = new JButton("F"); private JButton g = new JButton("G"); private JButton h = new JButton("H"); private JButton i = new JButton("I"); private JButton j = new JButton("J"); private JButton k = new JButton("K"); private JButton l = new JButton("L"); private JButton m = new JButton("M"); private JButton n = new JButton("N"); private JButton o = new JButton("O"); private JButton p = new JButton("P"); private JButton q = new JButton("Q"); private JButton r = new JButton("R"); private JButton s = new JButton("S"); private JButton t = new JButton("T"); private JButton u = new JButton("U"); private JButton v = new JButton("V"); private JButton w = new JButton("W"); private JButton x = new JButton("X"); private JButton y = new JButton("Y"); private JButton z = new JButton("Z"); // create panels private JPanel aF = new JPanel(); private JPanel gL = new JPanel(); private JPanel mR = new JPanel(); private JPanel sX = new JPanel(); private JPanel yZ = new JPanel(); private JPanel readout = new JPanel(); private JLabel notify = new JLabel("You have selected folder " + folder); public void ComposePane() { // set buttons to panels aF.add(a); aF.add(b); aF.add(c); aF.add(d); aF.add(e); aF.add(f); // set layout of panels aF.setLayout(new GridLayout(2, 3)); gL.add(g); gL.add(h); gL.add(i); gL.add(j); gL.add(k); gL.add(l); gL.setLayout(new GridLayout(2, 3)); mR.add(m); mR.add(n); mR.add(o); mR.add(p); mR.add(q); mR.add(r); mR.setLayout(new GridLayout(2, 3)); sX.add(s); sX.add(t); sX.add(u); sX.add(v); sX.add(w); sX.add(x); sX.setLayout(new GridLayout(2, 3)); yZ.add(y); yZ.add(z); yZ.add(readout); readout.add(notify); yZ.setLayout(new GridLayout(2, 3)); } public filecabinet() { setTitle("File Cabinet"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new GridLayout(5, 1)); ComposePane(); add(aF); add(gL); add(mR); add(sX); add(yZ); addActionListener(aF); addActionListener(gL); addActionListener(mR); addActionListener(sX); addActionListener(yZ); } public void actionPerformed(ActionEvent e) { folder = ((JButton) e.getSource()).getText(); // set the label with folder value notify.setText("You have selected folder " + folder); } // Attach event handler to each JButton in JPanel public void addActionListener(JPanel panel) { Component[] components = panel.getComponents(); for (Component component : components) { if (component instanceof JButton) { ((JButton) component).addActionListener(this); } } } public static void main(String[] args) { filecabinet frame = new filecabinet(); final int WIDTH = 600; final int HEIGHT = 600; frame.setSize(WIDTH, HEIGHT); frame.setVisible(true); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload