Found 7442 Articles for Java

How to create a List Spinner in Java?

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

347 Views

For a List Spinner, use the SpinnerListModel class. At first, let us set a list −SpinnerListModel list = new SpinnerListModel(new String[] { "Football", "Cricket", "Hockey", "Squash", "Fencing" });Now, set it and create a new JSpinner −JSpinner spinner = new JSpinner(list);The following is an example to create a List Spinner −Examplepackage my; import java.awt.GridBagLayout; import javax.swing.*; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame("Spinner Demo");       JPanel panel = new JPanel();       JLabel label = new JLabel("Favourite Sports − ");       panel.setLayout(new GridBagLayout());   ... Read More

Java Program to get the previous leaf from a JTree

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

160 Views

Use the getPreviousLeaf() method to get the previous leaf in a JTree. Here, we are displaying the leaf before this node “eight” on Console -System.out.println("Get Previous Leaf = "+eight.getPreviousLeaf());The following is an example to get the previous leaf from a JTree -Examplepackage my; import javax.swing.JFrame; import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; 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 (Product1 - P66778)");       DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("Accessories (Product2 - P66779)"); ... Read More

I want to return the next node after this node in a JTree with Java

George John
Updated on 30-Jul-2019 22:30:26

223 Views

Use the getNextNode() method to get the next node after this node in Java. Here, we are displaying the next node of child node “eight” -System.out.println("Next node after this node = "+eight.getNextNode());The following is an example to return the next node after this node in a JTree -Examplepackage my; import javax.swing.JFrame; import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; 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 (Product1 - P66778)");       DefaultMutableTreeNode node2 ... Read More

How to get the leaf after this node in a JTree component with Java?

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

180 Views

Use the getNextLeaf() method to get the leaf after this node in a JTree. Here, we are displaying the leaf after node “three” in Console −System.out.println("Next leaf after this node = "+three.getNextLeaf());The following is an example to get the leaf node after this node in a JTree component −package my; import javax.swing.JFrame; import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; 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 (Product1 - P66778)");       DefaultMutableTreeNode ... Read More

How to use the SimpleDateFormat class to convert a Java Date to a formatted String?

raja
Updated on 21-Nov-2023 16:11:34

158 Views

The Java SimpleDateFormat class provides convert between a Java String to a Date or Date to String. Example import java.util.Date; import java.text.SimpleDateFormat; import java.util.Calendar; public class SimpleDateFormatTest { public static void main(String[] args) { // get today's date Date today = Calendar.getInstance().getTime(); // create a date "formatter" SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-hh.mm.ss"); // create a new String using the date format String formattedDate = formatter.format(today); // prints converted date to string format System.out.println(" Date is:" + formattedDate); } } Output Date is:2023-11-21-10.39.16

What are the differences between an application and an applet in Java?

raja
Updated on 06-Feb-2020 10:13:32

2K+ Views

A Java program can be classified into two types, one is an Application and another is an Applet.ApplicationAn application is a stand-alone java program that runs with the support of a virtual machine in a client or server-side.A java application is designed to perform a specific function to run on any Java-compatible virtual machine regardless of the computer architecture.An application is either executed for the user or for some other application program.Examples of java applications include database programs, development tools, word processors, text and image editing programs, spreadsheets, web browsers, etc.Examplepublic class Demo {    public static void main(String args[]) ... Read More

How to resolve a NullPointerException in Java?

Shriansh Kumar
Updated on 20-May-2025 17:25:10

2K+ Views

The NullPointerException is a subclass of the RuntimeException class. It is defined in java.lang package of Java. In this article, we are going to understand the reasons for NullPointerException and how to resolve them. Reason for NullPointerException in Java A NullPointerException is a runtime exception thrown by the JVM when our application code, another referenced API, or the middleware encounters the following conditions: Attempting to invoke an instance method of a null object. ... Read More

Java Program to wrap text in a JTextPane and show Scrollbar

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

2K+ Views

Let’s say we have lots of content in our JTextPane component −textPane.setText("This is demo text1. This is demo text2. This is demo text3."    + "This is demo text4.This is demo text5. This is demo text6. "    + "This is demo text7. This is demo text8. This is demo text9. "    + "This is demo text10. This is demo text11. This is demo text12."    + "This is demo text13. This is demo text13. This is demo text14."    + "This is demo text15. This is demo text13. This is demo text16."    + " This is demo ... Read More

I want to highlight all the text in the Java Swing Control Text Pane

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

360 Views

To highlight all the text, use the selectAll() method of the JTextPane component −JTextPane pane = new JTextPane(); pane.selectAll();The following is an example to highlight the JTextPane text. Here, we are displaying a code in the JTextPane and highlighting it −Examplepackage my; import java.awt.BorderLayout; import java.awt.Container; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextPane; import javax.swing.text.BadLocationException; public class SwingDemo {    public static void main(String args[]) throws BadLocationException {       JFrame frame = new JFrame("Demo");       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       Container container = frame.getContentPane();       JTextPane pane = new JTextPane();       pane.setContentType("text/html");     ... Read More

How to use JTextPane to style code in Java?

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

704 Views

To style code in a TextPane component use the setText() for text and work with HTML tags. For code, use the tag. Also, do not forget to set the content type to “text/html” −JTextPane pane = new JTextPane(); pane.setContentType("text/html");If you won’t set the content type, then the output will display all these HTML tags inside the JTextPane. Now, set the code inside −pane.setText(" #include ; using namespace std; main() {cout

Advertisements