Found 4335 Articles for Java 8

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

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

222 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 30-Jul-2019 22:30:26

207 Views

Let’s say the following is our JTextPane −JTextPane textPane = new JTextPane(); textPane.setForeground(Color.white); textPane.setBackground(Color.blue);Now, set the style and attributes. Also, set the font −SimpleAttributeSet attributeSet = new SimpleAttributeSet(); StyleConstants.setItalic(attributeSet, true); textPane.setCharacterAttributes(attributeSet, true); textPane.setText("Press the Button "); Font font = new Font("Verdana", Font.BOLD, 22); textPane.setFont(font);After the text displayed above, we will insert a component using setComponent() −StyledDocument doc = (StyledDocument) textPane.getDocument(); Style style = doc.addStyle("StyleName", null); StyleConstants.setComponent(style, new JButton("Submit")); doc.insertString(doc.getLength(), "invisible text", style);The following is an example to insert a component into a component. Here, we have set a button component inside a JTextPane −Examplepackage my; import java.awt.BorderLayout; import java.awt.Color; import ... Read More

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

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

484 Views

To remove the last row from a table, use the removeRow() method and set its parameter to total number of rows minus 1 since you need to remove the last row.Let us first see an example to display rows and columns in a JTable −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();       JTable table = new JTable(tableModel);       tableModel.addColumn("Language/ Technology");       tableModel.addColumn("Text Tutorial");       tableModel.addColumn("Video Tutorial");     ... Read More

How to add empty border to JPanel in Java?

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

802 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

844 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

171 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

485 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

Floating Point Operations and Associativity in C, C++ and Java

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

190 Views

In C, C++, and java, we do some mathematical operations with floating point numbers. Now here we will check whether the floating point numbers are following the associativity rule or not.The answer is NO. The floating point numbers does not follow the associativity rules in some cases. Here we will see some examples.Example Code#include using namespace std; main() {    float x = -500000000;    float y = 500000000;    float z = 1;    cout

Templates in C++ vs Generics in Java

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

2K+ Views

Templates are the foundation of generic programming, which involves writing code in a way that is independent of any particular type.A template is a blueprint or formula for creating a generic class or a function. The library containers like iterators and algorithms are examples of generic programming and have been developed using template concept.There is a single definition of each container, such as vector, but we can define many different kinds of vectors for example, vector or vector .Example Code#include #include using namespace std; template inline T const& Max (T const& a, T const& b) { ... Read More

Can we declare an abstract method, private, protected, public or default in java?

Maruthi Krishna
Updated on 29-Jun-2020 13:45:25

10K+ Views

A method which does not have body is known as abstract method. It contains only method signature with a semi colon and, an abstract keyword before it.public abstract myMethod();To use an abstract method, you need to inherit it by extending its class and provide implementation to it.Declaring an abstract method privateIf a method of a class is private, you cannot access it outside the current class, not even from the child classes of it.But, incase of an abstract method, you cannot use it from the same class, you need to override it from subclass and use.Therefore, the abstract method cannot ... Read More

Advertisements