Found 9150 Articles for Object Oriented Programming

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

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

295 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

361 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

674 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

Explain in detail about the memory life cycle of JavaScript?

vineeth.mariserla
Updated on 30-Jul-2019 22:30:26

397 Views

Memory cycleRegardless of the programming language, the memory cycle is almost same for any programming language. There are 3 steps in a memory life cycle1) Allocation of memory .2) use the allocated memory(reading or writing)3) Release the allocated memory when it is unnecessary.The first and last parts are directly connected in low-level languages but are indirectly connected in high-level languages such as JavaScript.1) Allocation of memory in javascriptJavaScript is called garbage collected language, that is when variables are declared, it will automatically allocate memory to them.When there are no more references for the declared variables, allocated memory will be released. ExampleIn the ... 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

How to set the background color of a single tab in a JTabbedPane Container with Java?

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

1K+ Views

To set the background color of a single tab, use the setBackgroundAt() method. This gives an option to mention the index and the color. The index here is the index of the specific tab you want to color.Let us first create a JTabbedPane −JTabbedPane tabbedPane = new JTabbedPane();Now, set the background color for one of the tabs with index 2 −tabbedPane.setBackgroundAt(2, Color.RED);The following is an example to set the background color of a single tab in a JTabbedPane container −Examplepackage my; import javax.swing.*; import java.awt.*; public class SwingDemo {    public static void main(String args[]) {       JFrame ... Read More

How to highlight multiple rows in a sequence in a table with Java Swing?

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

271 Views

To highlight multiple rows in a table, you can use the addRowSelectionInterval() method. At first create a table −DefaultTableModel tableModel = new DefaultTableModel(); JTable table = new JTable(tableModel);Add some columns −tableModel.addColumn("Language/ Technology"); tableModel.addColumn("Text Tutorial"); tableModel.addColumn("Video Tutorial"); tableModel.addColumn("Views");Now, add rows to the table −tableModel.addRow(new Object[] { "NodeJS", "No", "Yes", "2350"}); tableModel.addRow(new Object[] { "MVC", "Yes", "No", "1500"}); tableModel.addRow(new Object[] { "ASP.NET", "Yes", "Yes", "3400"}); tableModel.addRow(new Object[] { "F#", "Yes", "No", "7890"}); tableModel.addRow(new Object[] { "Blockchain", "Yes", "No", "10600"}); tableModel.addRow(new Object[] { "SharePoint", "Yes", "Yes", "4900"});Highlight multiple rows by adding interval of rows from both ends. Set interval (index) for both the ... Read More

Inserting an Image into a JTextPane Component with Java

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

682 Views

Let’s say the following is our JTextPane with orange background color −JTextPane textPane = new JTextPane(); textPane.setBackground(Color.orange);Now, set the style and attributes. Also, set the font −SimpleAttributeSet attributeSet = new SimpleAttributeSet(); StyleConstants.setItalic(attributeSet, true); textPane.setCharacterAttributes(attributeSet, true); textPane.setText("Recall this and "); Font font = new Font("Verdana", Font.BOLD, 22); textPane.setFont(font);After the text displayed above, we will insert an image using setIcon() −StyledDocument doc = (StyledDocument) textPane.getDocument(); Style style = doc.addStyle("StyleName", null); StyleConstants.setIcon(style, new ImageIcon("E:\kind.png")); doc.insertString(doc.getLength(), "invisible text", style);The following is an example to insert an image into a component. Here, we have inserted an image into a JTextPane component −Examplepackage my; import ... Read More

Can we have a try block without a catch block in Java?

raja
Updated on 21-Nov-2023 10:08:40

34K+ Views

Yes, It is possible to have a try block without a catch block by using a final block. As we know, a final block will always execute even there is an exception occurred in a try block, except System.exit() it will execute always. Example 1 public class TryBlockWithoutCatch { public static void main(String[] args) { try { System.out.println("Try Block"); } finally { System.out.println("Finally Block"); } } } Output Try Block Finally Block A ... Read More

What are the differences between an Exception class and an Error class in Java?

raja
Updated on 30-Jul-2019 22:30:26

737 Views

Both an Exception class and an Error class are subclasses of java.lang.Throwable class, we can able to handle the exceptions at runtime but the errors we cannot handle.Exceptions are the objects representing the logical errors that occur at the run time and makes JVM enters into the state of "ambiguity".The objects which are automatically created by the JVM for representing these run time errors are known as an Exception. An Error is a subclass of Throwable class that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions.If an exception occurs we ... Read More

Advertisements