Last active
December 28, 2015 04:59
-
-
Save Raidok/7447014 to your computer and use it in GitHub Desktop.
A terrible example of using Listeners in a Swing application, do not try this at home
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// this code had been heavily adjusted for the sake of readability | |
JTable tbl = new JTable(new MyTableModel()); | |
tbl.addMouseListener(new ButCmd(this, ButCmd.CMD_UPDATE)); | |
// [...] | |
JPanel panel = new JPanel(new FlowLayout()); | |
JButton jb; | |
// New button T1 | |
panel.add( jb = new JButton(getLabel("NEW_T1"))); | |
jb.addActionListener(new ButCmd(this, ButCmd.CMD_NEW_T1)); | |
jb.setMnemonic(getLabel("NEW_T1_MNEMONIC").charAt(0)); | |
// New button T2 | |
panel.add( jb = new JButton(getLabel("NEW_T2"))); | |
jb.addActionListener(new ButCmd(this, ButCmd.CMD_NEW_T2)); | |
jb.setMnemonic(getLabel("NEW_T2_MNEMONIC").charAt(0)); | |
// New button T3 | |
panel.add(jb = new JButton(getLabel("NEW_T3"))); | |
jb.addActionListener(new ButCmd(this, ButCmd.CMD_NEW_T3)); | |
jb.setMnemonic(getLabel("NEW_T3_MNEMONIC").charAt(0)); | |
// Update button | |
panel.add(jb = new JButton(getLabel("UPD"))); | |
jb.addActionListener(new ButCmd(this, ButCmd.CMD_UPDATE)); | |
jb.setMnemonic(getLabel("UPD_MNEMONIC").charAt(0)); | |
// Refresh button | |
panel.add(jb = new JButton(getLabel("REFRESH"))); | |
jb.addActionListener(new ButCmd(this, ButCmd.CMD_REFRESH)); | |
jb.setMnemonic(getLabel("REFRESH_MNEMONIC").charAt(0)); | |
// [...] | |
// Inner class for processing actions | |
public class ButCmd extends MouseAdapter implements ActionListener { | |
public static final int CMD_CANCEL = 0; | |
public static final int CMD_NEW_T1 = 1; | |
public static final int CMD_NEW_T2 = 2; | |
public static final int CMD_NEW_T3 = 3; | |
public static final int CMD_UPDATE = 4; | |
public static final int CMD_REFRESH = 5; | |
private int cmd; | |
private Panel panel; | |
public ButCmd(Panel parent, int cmd) { | |
panel = parent; | |
cmd = cmd; | |
} | |
public void actionPerformed(ActionEvent event) { | |
switch (cmd) { | |
case CMD_NEW_T1 : | |
panel.cmdNewT1(); | |
break; | |
case CMD_NEW_T2 : | |
panel.cmdNewT2(); | |
break; | |
case CMD_NEW_T3 : | |
panel.cmdNewT3(); | |
break; | |
case CMD_UPDATE : | |
panel.cmdUpdate(); | |
break; | |
case CMD_REFRESH : | |
panel.refresh(); | |
break; | |
} | |
} | |
public void mouseClicked(MouseEvent e) { | |
if (e.getClickCount() != 1 && cmd == CMD_UPDATE) { | |
panel.cmdUpdate(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment