Found 7442 Articles for Java

How to set Echo Char for JPasswordField in Java?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

4K+ Views

With echo char, you can set a character that would appear whenever a user will type the password in the JPasswordField.Let us first create a new JPassword field −JPasswordField passwd = new JPasswordField();Now, use the setEchoChar() to set the echo char for password field. Here, we have asterisk (*) as the field for password −passwd.setEchoChar('*');The following is an example to set echo char for password field −Examplepackage my; import java.awt.GridLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.SwingConstants; public class SwingDemo {    public static void main(String[] args) throws Exception {       JFrame frame = new ... Read More

How to set JCheckBoxMenuItem for a MenuItem in Java?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

150 Views

Let’s say we have a menu and within that we need to set the JCheckBoxMenuItem. Here’s the Menu −JMenu editMenu = new JMenu("Edit"); editMenu.setMnemonic(KeyEvent.VK_E); menuBar.add(editMenu);Now, set the JCheckBoxMenuItem and add it to the editMenu created above −JCheckBoxMenuItem checkMenuItem = new JCheckBoxMenuItem("SelectAll"); checkMenuItem.setMnemonic(KeyEvent.VK_B); editMenu.add(checkMenuItem);The following is an example to set JCheckBoxMenuItem for a MenuItem in Java −Examplepackage my; import java.awt.Color; import java.awt.event.KeyEvent; import javax.swing.JCheckBoxMenuItem; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.UIManager; public class SwingDemo {    public static void main(final String args[]) {       JFrame frame = new JFrame("MenuBar Demo");       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     ... Read More

How to create a Default Cell Editor that uses a JCheckBox in Java?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

328 Views

Create a check box first and set valueJCheckBox checkBox = new JCheckBox("In-Stock");Set the JCheckBox for the editor so that the editor uses the check box −TreeCellEditor editor = new DefaultCellEditor(comboBox); tree.setEditable(true); tree.setCellEditor(editor);The following is an example to create a Default Cell Editor that uses a JCheckBox −Examplepackage my; import javax.swing.DefaultCellEditor; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.TreeCellEditor; public class SwingDemo {    public static void main(String[] args) throws Exception {       JFrame frame = new JFrame("Demo");       DefaultMutableTreeNode node = new DefaultMutableTreeNode("Products");       DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("Clothing");       ... Read More

How to set foreground color for different words in a JTextPane

Smita Kapse
Updated on 30-Jul-2019 22:30:26

1K+ Views

To set the foreground color for different words, use the SimpleAttributeSet and StyleConstants class. With that, use StyledDocument class as well for different words style. For different words, use insertString().At first, create a new JTextPane -At first, create a new JTextPane: JTextPane pane = new JTextPane();Now, use the classes to set the style and color for some words −StyledDocument doc = textPane.getStyledDocument(); Style style = textPane.addStyle("", null); StyleConstants.setForeground(style, Color.red); StyleConstants.setBackground(style, Color.white); doc.insertString(doc.getLength(), "Game of Thrones ", style);Now, style some other words differently −StyleConstants.setForeground(style, Color.yellow); StyleConstants.setBackground(style, Color.gray); doc.insertString(doc.getLength(), "Season 8", style);The following is an example to set foreground color for different ... Read More

Set whether the column in the table model can be selected or deselected in Java?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

75 Views

We can set or disallow selection of column in the table using setColumnSelectionAllowed().Let’s say the following is our table −DefaultTableModel tableModel = new DefaultTableModel(); JTable table = new JTable(tableModel);If you want to allow selection of column, then set the method to TRUE −table.setColumnSelectionAllowed(true);If you want to disallow selection of column, then set the method to FALSE −table.setRowSelectionAllowed(false);Here, in the below example we have disallowed selection of columns −Examplepackage my; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; public class SwingDemo {    public static void main(String[] argv) throws Exception {       DefaultTableModel tableModel = new DefaultTableModel();     ... Read More

Java Program to set a spinner of dates in Java

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

233 Views

To set a spinner of dates, at first create a date object −Date today = new Date();Now, use SpinnerDateModel −JSpinner spinner2 = new JSpinner(new SpinnerDateModel(today, null, null, Calendar.MONTH));We will now consider a DateEditor, which is an editor for a JSpinner whose model is a SpinnerDateModel. Set the format for month here −JSpinner.DateEditor editor = new JSpinner.DateEditor(spinner2, "MM"); spinner2.setEditor(editor);The following is an example to set a spinner of date in Java −Examplepackage my; import java.awt.GridBagLayout; import java.util.Calendar; import java.util.Date; import javax.swing.*; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame("Spinner Demo"); ... Read More

Java Program to set minor tick marks in a JSlider every 10 units

Smita Kapse
Updated on 30-Jul-2019 22:30:26

297 Views

Minor Tick marks is the number passed in representing the distance between each minor tick mark. For example, a slider with range from 0 to 70 and minor tick spacing 10, would give minor ticks next to the following values: 0, 10, 20, 30, 40, 50, 60, 70.To set minor tick marks, use the setMinorTickSpacing() method −JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 60); slider.setMinorTickSpacing(10);Note − For minor ticks to be painted, you need to set setPaintTicks to true.The following is an example to set minor tick marks in a slider every 10 units −Examplepackage my; import javax.swing.JFrame; import javax.swing.JPanel; ... Read More

Java program to insert a component into a JTextPane component

Anvi Jain
Updated on 23-Nov-2024 03:50:48

363 Views

In this article, we will learn how to add a component to a JTextPane in Java. By using StyledDocument and StyleConstants, we can insert elements like buttons within the text pane, allowing us to create dynamic and interactive text-based components.JTextPaneJTextPane is a versatile text component in Java Swing that allows for styled text. It supports multiple text formats like bold, italic, and different fonts. It can also display rich text, such as embedded images or buttons, through the StyledDocument class. Inserting a component into a JTextPane The following are the steps to insert a component into a JTextPane − ... Read More

Java Program to remove the last row from a table with DefaultTableModel

Alshifa Hasnain
Updated on 14-Feb-2025 18:58:58

680 Views

In this article, we will learn to remove the last row from a table with DefaultTableModel in Java. Managing table data dynamically is a common requirement when working with Java Swing applications. One such operation is removing the last row from a JTable using DefaultTableModel. Understanding DefaultTableModel in Java DefaultTableModel is a flexible table model in Java's Swing library that allows developers to dynamically manage table data, including adding and removing rows and columns. Provides built-in methods for adding and removing rows easily. Approach to Removing the Last Row In this program, we create a table with multiple rows and ... Read More

How to add empty border to JPanel in Java?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

1K+ Views

To add empty border, use the createEmtyBorder() method. Let us first create a new JLabel −JLabel label; label = new JLabel("Label with empty border!");Now, set empty border using the setBorder() method −label.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));The following is an example to add empty border to JPanel −Examplepackage my; import javax.swing.BorderFactory; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JLabel; public class SwingDemo {    public static void main(String[] args) throws Exception {       JFrame frame = new JFrame("Demo");       JLabel label;       label = new JLabel("Label with empty border!");       label.setFont(new Font("Verdana", Font.PLAIN, 16));   ... Read More

Advertisements