Found 7442 Articles for Java

How to solve an IllegalArgumentException in Java?

Shriansh Kumar
Updated on 12-May-2025 11:34:05

30K+ Views

In Java, an IllegalArgumentException is thrown in order to indicate that a method has been passed an illegal argument. An illegal argument is one that does not meet the required input from user. This exception extends the RuntimeException class and thus, belongs to those exceptions that can be thrown during the operation of the Java Virtual Machine (JVM). It is an unchecked exception and thus, it does not need to be declared in a method's or a constructor's throws clause. Reasons for java.lang.IllegalArgumentException Some of the reasons for IllegalArgumentException in Java is as follows: ... Read More

How to create a submenu for a Menu in Java

Chandu yadav
Updated on 30-Jul-2019 22:30:26

2K+ Views

Let us first create a MenuBar −JMenuBar menuBar = new JMenuBar(); UIManager.put("MenuBar.background", Color.ORANGE);To create a submenu for a Menu, the following is an example −Examplepackage my; import java.awt.Color; import java.awt.event.KeyEvent; import javax.swing.ButtonGroup; import javax.swing.JCheckBoxMenuItem; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JRadioButtonMenuItem; 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);       JMenuBar menuBar = new JMenuBar();       UIManager.put("MenuBar.background", Color.ORANGE);       JMenu fileMenu = new JMenu("File");       fileMenu.setMnemonic(KeyEvent.VK_F);       menuBar.add(fileMenu); ... Read More

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

Ankith Reddy
Updated on 30-Jul-2019 22:30:26

341 Views

Create a combo box first and set some values −JComboBox comboBox = new JComboBox(new String[]{"Product1", "Product2", "Product3", "Product4"});Seth the JComboBox for the editor so that the editor uses the combo 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 JComboBox −Examplepackage my; import javax.swing.DefaultCellEditor; import javax.swing.JComboBox; 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 ... Read More

How to automatically resize a JTree in Java

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

371 Views

To automatically resize a JTree, use the setVisibleRowCount() method in Java. At first, create a node in the tree −DefaultMutableTreeNode node = new DefaultMutableTreeNode("Project");Now, add nodes to the node created above −DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("App"); DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("Website"); DefaultMutableTreeNode node3 = new DefaultMutableTreeNode("WebApp"); node.add(node1); node.add(node2); node.add(node3);Now, create more nodes and set them as child nodes for the nodes we creted above −DefaultMutableTreeNode one = new DefaultMutableTreeNode("Learning website"); DefaultMutableTreeNode two = new DefaultMutableTreeNode("Business website"); DefaultMutableTreeNode three = new DefaultMutableTreeNode("News publishing website"); DefaultMutableTreeNode four = new DefaultMutableTreeNode("Android app"); DefaultMutableTreeNode five = new DefaultMutableTreeNode("iOS app"); DefaultMutableTreeNode six = new DefaultMutableTreeNode("Editor ... Read More

How to display tree structured data in Java?

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

811 Views

To display tree structured data, use the JTree control in Java Swing.At first, create a node in the tree −DefaultMutableTreeNode node = new DefaultMutableTreeNode("Project");Now, add nodes to the node created above −DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("App"); DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("Website"); DefaultMutableTreeNode node3 = new DefaultMutableTreeNode("WebApp"); node.add(node1); node.add(node2); node.add(node3);Now, create more nodes and set them as child nodes for the nodes we creted above −DefaultMutableTreeNode one = new DefaultMutableTreeNode("Learning website"); DefaultMutableTreeNode two = new DefaultMutableTreeNode("Business website"); DefaultMutableTreeNode three = new DefaultMutableTreeNode("News publishing website"); DefaultMutableTreeNode four = new DefaultMutableTreeNode("Android app"); DefaultMutableTreeNode five = new DefaultMutableTreeNode("iOS app"); DefaultMutableTreeNode six = new DefaultMutableTreeNode("Editor ... Read More

Java Program to enable drag and drop between two text fields in Java

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

509 Views

Yes, we can enable drag and drop between two text fields in Java. Let us first create two JTextFields and set content in it as shown below −JTextField one = new JTextField(20); one.setText("You can drag!"); JTextField two = new JTextField(20); two.setText("Drag here or there");Now, we will enable and drag and drop for both the components created above −one.setDragEnabled(true); two.setDragEnabled(true);The following is an example to enable drag and drop between two text fields −Examplepackage my; import java.awt.Container; import java.awt.FlowLayout; import javax.swing.BoxLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.SwingConstants; public class SwingDemo {    public static void main(String[] args) throws Exception ... Read More

Java program to set horizontal alignment of content in a JTextField

Smita Kapse
Updated on 29-Aug-2024 15:54:48

3K+ Views

In this article, we will learn how to set the horizontal alignment of content in a JTextField class using Java Swing. The content in the JTextFile is by default left aligned, but you can change it using the setHorizontalAlignment() method. Steps to Set Horizontal Alignment of Content in a JTextField Following are the steps to set the Horizontal Alignment of content in a JTextField − Import the required Java Swing packages. Create a JFrame and set layout manager. Create JLabel and JTextField then set horizontal alignment. ... Read More

Can we save content from JTextField to a file in Java?

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

1K+ Views

Yes, we can save the content to a file with FileWriter class. Set a JTextFile component as shown below −JTextField emailId = new JTextField(20); emailId.setText("abc@example.com");Set the file location from to where you want to save the content from the JTextField −String file = "E:ew.txt";Now, with FileWriter, save the content −FileWriter fileWriter = new FileWriter(file); emailId.write(fileWriter); fileWriter.close();The following is an example to save content from JTextFile to a file. Here, we are saving the text from JTextField to a file at location: “E:ew.txt” −Examplepackage my; import java.awt.FlowLayout; import java.io.FileWriter; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.SwingConstants; public class SwingDemo { ... Read More

How to ceate right justified JTextField in Java?

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

2K+ Views

To create right justified JTextField, set the alignment to be RIGHT. Here, we will be using the setHorizontalAlignment() method as well and within that the alignment would be set.Create a JTextField −JTextField emailId = new JTextField(20);Now, align it to the right −emailId.setHorizontalAlignment(JTextField.RIGHT);The following is an example to create right justified JTextField −Examplepackage my; import java.awt.FlowLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.SwingConstants; public class SwingDemo {    public static void main(String[] args) throws Exception {       JFrame frame = new JFrame("Enter emailid...");       JLabel label;       frame.setLayout(new FlowLayout());       label = ... Read More

How to change the minimum value of a JSlider in Java?

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

241 Views

To change the minimum value of a slider in Java, use the setMinimum() method wherein set the minimum value.Let’s say the following is our slider in Java −JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 55); slider.setMinorTickSpacing(10); slider.setMajorTickSpacing(20); slider.setPaintTicks(true); slider.setPaintLabels(true);Now, set the minimum value −slider.setMinimum(10);The following is an example to change the minimum value of a JSlider −Examplepackage my; import java.awt.Color; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JSlider; import javax.swing.WindowConstants; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame("Frame with Slider");       JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, ... Read More

Advertisements