Posts

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(...