Hi, is there any website have a lots of sample code and eg. to download. I'm quite new to JAVA cos no sticky threads for this.

& also how can I do a simple buttongroup with captions?

Dani AI

Generated

A compact follow-up for : this thread asks for downloadable sample code and "a simple buttongroup with captions." 's pointer to the official tutorials is a solid start, and 's reminder that merely running samples won't teach programming is worth noting — read and step through examples, not just execute them. For quick finds, 's suggestion to search for beginner tutorials works, but curated collections and runnable projects shorten the learning curve.

Useful, reliable resources with runnable examples:

A minimal Swing example that shows a caption label plus a ButtonGroup of radio buttons:

import javax.swing.*;
import javax.swing.ButtonModel;

public class ButtonGroupExample {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame("ButtonGroup example");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel panel = new JPanel();

            JLabel caption = new JLabel("Choose an option:");
            JRadioButton r1 = new JRadioButton("Option A");
            JRadioButton r2 = new JRadioButton("Option B");
            JRadioButton r3 = new JRadioButton("Option C");

            ButtonGroup group = new ButtonGroup();
            group.add(r1);
            group.add(r2);
            group.add(r3);

            r1.setActionCommand("A");
            r2.setActionCommand("B");
            r3.setActionCommand("C");

            panel.add(caption);
            panel.add(r1);
            panel.add(r2);
            panel.add(r3);

            JButton ok = new JButton("Show");
            ok.addActionListener(e -> {
                ButtonModel sel = group.getSelection();
                String chosen = (sel == null) ? "none" : sel.getActionCommand();
                System.out.println("Selected: " + chosen);
            });
            panel.add(ok);

            frame.getContentPane().add(panel);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        });
    }
}

Clarifying notes and common pitfalls: ButtonGroup is a logical grouping (not a container), so buttons still need to be added to a panel/layout. Creating the UI must occur on the Event Dispatch Thread (hence SwingUtilities.invokeLater). group.getSelection() can be null if nothing is selected, and setActionCommand is handy to retrieve a simple identifier. Combining the official docs with small GitHub projects and stepping through code with a debugger provides the best learning path.

Recommended Answers

All 3 Replies

Here you go, straight from the source: link.

Great tutorials, well organized, you'll find anything in there you might need.

Black Box

PS: If you're new to programming, don't skip on getting to know all of the basic types and the bits and bytes behind them. Such knowlegde comes in handy when you'll find yourself in your first debugging sessions.

and don't think that just running some samples will teach you programming (it does seem a common misconception at the moment).

ok search on google for Java Beginners tutorial.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.