Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I get the JLabel to show which button has been selected
    primarykey
    data
    text
    <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(); } public void actionPerformed(ActionEvent e) { folder = ((JButton) e.getSource()).getText(); } public void addActionListener(){ a.addActionListener(this); b.addActionListener(this); c.addActionListener(this); d.addActionListener(this); e.addActionListener(this); f.addActionListener(this); g.addActionListener(this); h.addActionListener(this); i.addActionListener(this); j.addActionListener(this); k.addActionListener(this); l.addActionListener(this); m.addActionListener(this); n.addActionListener(this); o.addActionListener(this); p.addActionListener(this); q.addActionListener(this); r.addActionListener(this); s.addActionListener(this); t.addActionListener(this); u.addActionListener(this); v.addActionListener(this); w.addActionListener(this); x.addActionListener(this); y.addActionListener(this); z.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> <p>I would prefer not to have to write out a listener for every button and I was wandering if I could just get the text contained in the button.</p> <p>such as "You have selected foler X", X is the button chosen.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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