13

An question that was brought to my mind when programming with Swing in Java was, is it a recommendation or an "official"/most used naming convention(prefix) on Swing components.

Such as(even though other may prefer other naming conventions this is what I am currently using):

  • txt for JTextField
  • btn for JButton
  • lbl for JLabel
  • pnl for JPanel

But then my list ends..

I think such prefixes enhance the readability in my code so, but I do not have any names for components such as JComboBox, JList, JRadioButton, JCheckButton and so the list goes on.

Thanks in advance.

1
  • Smells like Hungarian notation
    – Steve Kuo
    Jul 7, 2017 at 15:31

4 Answers 4

27

People will tell you that using prefixes is bad, because that is Hungarian notation and nowadays, Hungarian notation is considered a big no-no in programming. Personally, as somebody who has done a certain amount of GUI work, I can tell you that, whereas Hungarian notation should definitely be avoided in non-GUI programming, when doing GUIs it's a very good practice.

Consider, for example, a simple form with a textbox which is to be used to enter the user's name. In front of this textbox should be a label prompting the user to enter his name in the textbox. Now, how are you going to name the textbox? 'Name'? What about the label? A good practice should be prefixing the textbox with txt, and label with label, which is what Hungarian notation is all about. Thus, the textbox is now named 'txtName' and the corresponding label is named 'lblName'. This gives you the additional benefit of easily accessing your textboxes, combo boxes and other widgets when in your IDE's editor. For example, typing 'txt' and pressing CTRL+Space in Eclipse opens up a context menu listing all of your textboxes, if you follow this notation.

Now, to answer your question. The usual way to define what three letters you should use for a prefix is to remove all the vowels from the widget's name and also all repeating consonants. If there are more then three consonants left, they should be ignored. Therefore, a textbox (or TextField, or whatever this widget is called in your preferred widget toolkit) becomes 'txt', a label 'lbl', a combo box 'cmb', a table 'tbl' and so on.

6
  • So you'd use "chc" for JCheckButton? I'm asking, since I'd maybe prefer "chb", otherwise I'd never find out what it means. For other components I can think of, it should work fine.
    – maaartinus
    Jan 22, 2011 at 20:20
  • I have to agree. Gui components are the only variables that get a btn_XXX, pnl_XXX, chk_XXX treatment from me. For one thing, it helps me visually separate GUI vars from state/data vars. But... for the reasons duffy mentioned, I feel a bit dirty doing it.
    – robert_x44
    Jan 22, 2011 at 20:43
  • I'm not sure why someone would downvote an opinion answer to a subjective question.
    – robert_x44
    Jan 22, 2011 at 20:44
  • @maartinus - Following the convention that the repeating consonants are also removed, the prefix for the checkbox-type widgets should be 'chk'.
    – Sandman
    Jan 22, 2011 at 23:10
  • 1
    @Adam - I'd go with either what you proposed (txtf / txta), or possibly txf and txa.
    – Sandman
    Feb 3, 2015 at 12:32
9

I usually hate this sort of thing, because it smacks of Hungarian notation. I don't like the idea of embedding the type in the name of the variable, because if you change types it shouldn't mandate changing all the variable names.

But in the case of Swing, I guess it's acceptable.

A good IDE will generate variable names for you. Why not let it? I'd also just spell out the type if you insist (e.g. submitButton instead of btnSubmit). Keystrokes are cheap.

1
  • Yeah, you have got some good points that I will take with me, thank you!
    – LuckyLuke
    Jan 22, 2011 at 20:05
8
  • btn - JButton
  • chk - JCheckBox
  • clr - JColorChooser
  • cmb - JComboBox
  • ico - JDesktopIcon
  • edt - JEditorPane
  • fch - JFileChooser
  • ifr - JInternalFrame
  • lbl - JLabel
  • lyp - JLayeredPane
  • lst - JList
  • mnu - JMenuBar
  • mni - JMenuItem
  • opt - JOptionPane
  • pnl - JPanel
  • pmn - JPopupMenu
  • prg - JProgressBar
  • rad - JRadioButton
  • rot - JRootPane
  • scb - JScollBar
  • scr - JScrollPane
  • spr - JSeparator
  • sld - JSlider
  • spn - JSpinner
  • spl - JSplitPane
  • tab - JTabbedPaneJTable
  • tbl - JTable
  • tbh - JTableHeader
  • txa - JTextArea
  • txt - JTextField
  • txp - JTextPane
  • tgl - JToggleButton
  • tlb - JToolBar
  • tlt - JToolTip
  • tre - JTree
  • vpr - JViewport

source -: http://zuskin.com/java_naming.htm

6

Why not just call JTextField textField, JButton button, JLabel label, and JPanel panel. Is it so bad to spend a few extra characters to make the variable read like an English word?

Furthermore, when I do put the type in the variable name, I put it on the end. So a JLabel that displays a name is nameLabel (which IMO is more readable than lblName).

And even furthermore, following duffymo's advice, it's bad practice to put the type in the variable name. A better approach is to describe what the variable is. In the case of a name label, it's a UI component that displays name. So a better name might be nameComponent. The fact that nameComponent is a JLabel or some other type is secondary and shouldn't clutter the variable name.

2
  • See my anser on why using a prefix is better than using a suffix (putting the type on the end).
    – Sandman
    Jan 22, 2011 at 20:14
  • @Andrew Sure, you could use a suffix. However, please read the last two sentences in the second paragraph of my answer where I explain the benefits of using a prefix over suffix.
    – Sandman
    Mar 14, 2012 at 13:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.