Found 9150 Articles for Object Oriented Programming

How to add Icon to JButton in Java?

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

23K+ Views

To add icon to a button, use the Icon class, which will allow you to add an image to the button.We are creating a button wherein we are adding an icon with Icon class −Icon icon = new ImageIcon("E:\editicon.PNG"); JButton button7 = new JButton(icon);Above, we have set icon for button 7.The following is an example to add icon to JButton−Examplepackage my; import javax.swing.Box; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; public class SwingDemo {    public static void main(String[] args) {       JButton button1 = new JButton("One");       JButton button2 = new JButton("Two");     ... Read More

Allow multiple selection of nodes not necessarily contigous in JTree

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

364 Views

To allow multiple selection of nodes not necessarily contiguous, set the selection mode for tree to be DISCONTIGUOUS_TREE_SELECTION −tree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);The following is an example to allow multiple selection of nodes, which are not necessarily contigous −Examplepackage my; import javax.swing.JFrame; import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.TreeSelectionModel; 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");       DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("Electronics");       DefaultMutableTreeNode node3 = new DefaultMutableTreeNode("Home Decor");       DefaultMutableTreeNode node4 = new DefaultMutableTreeNode("Furniture");       node.add(node1);       node.add(node2);       node.add(node3);       node.add(node4);       DefaultMutableTreeNode one = new DefaultMutableTreeNode("Shirt");       DefaultMutableTreeNode two = new DefaultMutableTreeNode("Trousers");       DefaultMutableTreeNode three = new DefaultMutableTreeNode("Jeans");       DefaultMutableTreeNode four = new DefaultMutableTreeNode("Mobiles");   ... Read More

What is the use of Map in JavaScript?

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

239 Views

MapMap holds key value pairs and remembers the actual insertion order of the keys. Map allows to store only a unique value.syntaxnew Map([iterable])Case-1: Absence Of MapIn the absence of Map, since javascript object endorses only one key object, If we provide multiple keys only the last one will be remembered. In the following example despite providing many keys such as a and b only b is remembered and displayed as output.So to eliminate this drawback "Map" came in to existence in javascript.ExampleLive Demo    const x = {};    const a = {};    const b = { ... Read More

How to add some rows to a table through DefaultTableModel in Java

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

673 Views

Let us first create a demo table −JTable table = new JTable(tableModel);Now, add a column to it −tableModel.addColumn("Languages");Add some rows to the table using insertRow() method −tableModel.insertRow(0, new Object[] { "CSS" }); tableModel.insertRow(0, new Object[] { "HTML5" }); tableModel.insertRow(0, new Object[] { "JavaScript" }); tableModel.insertRow(0, new Object[] { "jQuery" }); tableModel.insertRow(0, new Object[] { "AngularJS" });The following is an example to add a row to a table with DefaultTableModel −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

How to disable only the horizontal scrollbar in Java?

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

1K+ Views

To disable only the horizontal scrollbar in Java, use the JScrollPane.HORIZONTAL_SCROLLBAR_NEVER. Let’s say you created a Box with some button components. Now, create a JScrollPane −JScrollPane scrollPane = new JScrollPane();Set the Viewport view as Box −scrollPane.setViewportView(box);Now, disable the horizontal scrollbar −scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);The following is an example to disable only the horizontal scrollbar −Examplepackage my; import java.awt.BorderLayout; import java.awt.Dimension; import javax.swing.Box; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JScrollPane; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Demo");       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       JButton button1 = new JButton("Tutorials");       JButton ... Read More

How to create a Box to display components from left to right in Java

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

486 Views

Box is lightweight container that uses a BoxLayout object as its layout manager. To display components from left to right, use Box createHorizontalBox() method.Let us first create some button components −JButton button1 = new JButton("One"); JButton button2 = new JButton("Two"); JButton button3 = new JButton("Three"); JButton button4 = new JButton("Four"); JButton button5 = new JButton("Five"); JButton button6 = new JButton("Six");Now, crate a Box and align all the buttons from left to right −Box box = Box.createHorizontalBox(); box.add(button1); box.add(button2); box.add(button3); box.add(button4); box.add(button5); box.add(button6);The following is an example to create a Box to display components from left to right −Examplepackage my; import ... Read More

Java Program to get the previous node from a JTree

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

217 Views

Use the getPreviousNode() method to get the previous node of this node in Java. Here, we are displaying the previous node of child node “eight” −System.out.println("Get Previous Node = "+eight.getPreviousNode());The following is an example to get the previous node 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 - ... Read More

Java Program to set the extent in JSlider

Arjun Thakur
Updated on 02-Jan-2025 19:12:54

269 Views

In this article, we will learn how to set the extent of a JSlider using Java Swing. The extent defines the size of the range that the slider's knob can move within, restricting its movement past a certain point. JSlider JSlider is a component in Java Swing that provides a visual way to select a value by sliding a knob within a specified range. The setExtent() method in JSlider sets the size of the range that the knob covers, which controls how far it can move. setExtent() method The setExtent() method in Java is used to: ... Read More

Disable auto resizing to make the JTable horizontal scrollable in Java

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

812 Views

To disable auto resizing, you need to use the setAutoResizeMode() method. After that, set the table to AUTO_RESIZE_OFF.Let’s say the following is our table −String[][] rec = {    { "1", "Virat", "840" },    { "2", "David", "835" },    { "3", "Shikhar", "656" },    { "4", "Steve", "530" },    { "5", "Kane", "515" },    { "6", "Eion", "509" },    { "7", "AB de Villiers", "498" },    { "8", "Quinton", "470" },    { "9", "Glenn", "410" },    { "10", "Tom", "360" },    { "11", "Johnny", "320" },    { "12", "Shreyas", ... Read More

How to display horizontal grid lines in a JTable with Java?

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

943 Views

To display horizontal grid lines in a table, use the setShowHorizontalLines() method and set it to TRUE. Let us first create a table −String[][] rec = {    { "1", "Steve", "AUS" },    { "2", "Virat", "IND" },    { "3", "Kane", "NZ" },    { "4", "David", "AUS" },    { "5", "Ben", "ENG" },    { "6", "Eion", "ENG" }, }; String[] header = { "Rank", "Player", "Country" }; JTable table = new JTable(rec, header);Now, let us display vertical grid lines −table.setShowHorizontalLines(true);You can also give a color to these lines −table.setGridColor(Color.orange);The following is an example to display ... Read More

Advertisements