Posts

Showing posts from April, 2025

Remove (Clear) and remove Selected element

  ///===========Clear=================         jList1.setListData(new String[]{}); //==============Remove Selected Element =========       // TODO add your handling code here:      int count=0;         DefaultListModel listModel1 = new DefaultListModel();         ListModel<String> tempL=  jList1.getModel();                     //Create New Array and add element          for(int i=0;i<tempL.getSize();i++){             listModel1.add(i, tempL.getElementAt(i));         }                  //===Delete From Array         for(int index : jList1.getSelectedIndices()){                  /// System.out.println(index);           //...

Sorting jList

  //=========Sorting=============         ListModel<String> l= jList1.getModel();     ArrayList<String> list = new ArrayList<>();    for(int i=0;i<l.getSize();i++){     //   System.out.println(l.getElementAt(i));        list.add(l.getElementAt(i));    }        //list.sort(null);//    list.sort(Collections.reverseOrder());        String[ ] srt=new String[list.size()]; //   DefaultListModel listModel1 = new DefaultListModel();        for(int i=0;i<list.size();i++){        System.out.println(list.get(i));       //listModel1.addElement(r);       srt[i]=list.get(i).toString();    }            //jList1.setModel(listModel1);        jList1.setListData(srt);         ...

JSpinner

  🔄 Java Swing JSpinner – Overview & Example JSpinner is a Swing component that allows users to select a number or object value from a sequence by clicking up/down arrows or typing. ✅ Key Features Method / Property Description setModel(SpinnerModel) Set a value model (number, list, date, etc.) getValue() Get current spinner value setValue(Object) Set spinner value addChangeListener(...) Listen to value changes 📦 Types of Spinner Models Spinner Model Description SpinnerNumberModel Integer/double values SpinnerListModel List of strings or objects SpinnerDateModel Dates (with min/max & step size) ✅ Example 1: Spinner with Numbers import javax.swing.*; import java.awt.*; public class NumberSpinnerExample { public static void main(String[] args) { JFrame frame = new JFrame("JSpinner Number Example"); frame.setSize(300, 100); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ...

JPasswordField

  🔐 Java Swing JPasswordField – Overview & Example JPasswordField is a subclass of JTextField in Swing used for password input. The characters are masked (e.g., shown as * or • ) for security. ✅ Key Features Method / Property Description setEchoChar(char) Sets the masking character getPassword() Returns the password as a char[] setText(String) Sets the text (not recommended for passwords) getText() Returns the text (deprecated for passwords) ✅ Example: Basic Password Field import javax.swing.*; import java.awt.*; import java.awt.event.*; public class PasswordExample { public static void main(String[] args) { JFrame frame = new JFrame("JPasswordField Example"); frame.setSize(300, 150); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new FlowLayout()); JLabel label = new JLabel("Enter Password:"); JPasswordField passwordField = new JPasswordField(15); ...

JFormattedTextField

  ✅ Java Swing JFormattedTextField – Overview & Usage JFormattedTextField is a Swing component that restricts input to a specific format such as numbers , dates , currency , phone numbers , etc. 🔧 Key Features Feature Description setValue(Object) Set initial/formatted value getValue() Get current formatted value setFormatterFactory(...) Set a custom formatter like number, date, etc. Supports masks Input masks (e.g., for phone numbers) Formatting on focus lost Value becomes formatted when user leaves the field ✅ Common Use Cases with Examples 1. Number Format import javax.swing.*; import java.text.NumberFormat; public class NumberFormattedField { public static void main(String[] args) { JFrame frame = new JFrame("Number Format"); frame.setSize(300, 100); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); NumberFormat numberFormat = NumberFormat.getNumberInstance(); JFormattedTextField ...

JProgressBar

✅ Java Swing JProgressBar – Full Guide JProgressBar is a Swing component that visually displays progress for tasks like downloading, uploading, or processing. 🔧 Common Properties & Methods Method / Property Description setMinimum(int) Set minimum progress value (default 0) setMaximum(int) Set maximum progress value (default 100) setValue(int) Set current progress value getValue() Get current progress value setStringPainted(true) Show progress percentage as a string setIndeterminate(true) Show infinite progress (e.g., loading animation) setOrientation(...) Horizontal/Vertical (rarely used) ✅ Basic Example: Progress Bar with Manual Update import javax.swing.*; import java.awt.*; import java.awt.event.*; public class ProgressBarExample { public static void main(String[] args) { JFrame frame = new JFrame("JProgressBar Example"); frame.setSize(300, 150); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ...

JSlider

 In Java Swing, JSlider is a component used to select a numeric value by sliding a knob along a track. It's commonly used for settings like volume, brightness, zoom, etc. ✅ Important Properties of JSlider Property / Method Description setMinimum(int) Set the minimum value setMaximum(int) Set the maximum value setValue(int) Set the current value getValue() Get the current value setMajorTickSpacing(int) Set space between major tick marks setMinorTickSpacing(int) Set space between minor tick marks setPaintTicks(true/false) Show tick marks setPaintLabels(true/false) Show numeric labels setOrientation(int) Horizontal or Vertical (use JSlider.HORIZONTAL or JSlider.VERTICAL ) addChangeListener() Listen for value changes ✅ Example Code: Simple JSlider Usage import javax.swing.*; import javax.swing.event.*; import java.awt.*; public class SliderExample { public static void main(String[] args) { JFrame frame = new JFrame(...

JScrollBar

 Here are the steps to use JScrollBar in NetBeans with a GUI project: ✅ Step-by-Step in NetBeans GUI Builder: 🎯 1. Create a New Project Open NetBeans. Go to File > New Project . Choose Java > Java Application . Click Next , name the project, and click Finish . 🎯 2. Create a JFrame Form Right-click the Source Packages > your package . Select New > JFrame Form . Name it (e.g., ScrollBarExample ), and click Finish . 🎯 3. Add Components via Drag and Drop Open ScrollBarExample.java in Design view . From the Palette , drag: A JLabel to display the value (e.g., jLabel1 ) A JScrollBar (found under Swing Controls ) 🎯 4. Configure the ScrollBar Click on the JScrollBar (e.g., jScrollBar1 ) In Properties window , set: orientation = HORIZONTAL minimum = 0 maximum = 100 value = 0 unitIncrement = 1 blockIncrement = 10 🎯 5. Write Logic to Handle Scroll Switch to Source view . Find the...

JTextField AND JTextArea

 In Java Swing ( javax.swing ), JTextField and JTextArea are commonly used for user text input. Below are their important properties , how to set/get values , and an example . ✅ 1. JTextField (Single-line Input) Important Properties: Property Method Description Set text setText(String text) Set text in the field Get text getText() Get current text Set columns setColumns(int cols) Width of the text field (in columns) Set editable setEditable(boolean) Allow user editing or not Set font setFont(Font font) Set font style and size Set foreground color setForeground(Color c) Set text color Set background color setBackground(Color c) Set background color ✅ 2. JTextArea (Multi-line Input) Important Properties: Property Method Description Set text setText(String text) Set text in the area Get text getText() Get text Set rows/columns setRows(int), setColumns(int) Set size of the text area Line wrap setLineWrap...

Array, List, Set, and Map

 Here’s a concise overview of key Java collections: Array , List , Set , and Map along with basic usage examples and their documentation. ✅ 1. Array An Array is a data structure that stores a fixed-size sequence of elements of the same type. Documentation: Array in Java is an object that holds a fixed number of values of a single type. Arrays are zero-indexed , meaning the first element is at index 0 . Arrays are immutable in size once created (i.e., you can't resize them). Example: public class Main { public static void main(String[] args) { // Declare and initialize an array int[] arr = {10, 20, 30, 40, 50}; // Accessing array elements System.out.println("Element at index 2: " + arr[2]); // Looping through array for (int i = 0; i < arr.length; i++) { System.out.println("Element at index " + i + ": " + arr[i]); } } } ✅ 2. List A List is an ordered ...