Found 7442 Articles for Java

How to align multiple buttons with different height in Java?

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

566 Views

To align multiple buttons with different height in Java, try the following example, Here, we have set 5 buttons with GridBagConstraints −GridBagConstraints constraints = new GridBagConstraints(); constraints.insets = new Insets(5, 5, 5, 5); constraints.anchor = GridBagConstraints.WEST;In addition, to set different height for different buttons, we have used −component. getPreferredSize().heightThe following is an example to align multiple buttons with different height −Examplepackage my; import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class SwingDemo {    public static void main(String[] args) {       final JFrame frame = new JFrame(SwingDemo.class.getSimpleName());       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       JPanel panel = new JPanel(new GridBagLayout());       GridBagConstraints constraints = new GridBagConstraints();       constraints.insets = new Insets(5,  5,  5,  5);       constraints.anchor = GridBagConstraints.WEST;     ... Read More

How to set all the Arrow Buttons in a frame with Java?

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

320 Views

To set arrow buttons in a frame, let us first create a frame −JFrame frame = new JFrame();Now, set the layout for the frame wherein all the arrow buttons would be displayed −frame.setLayout(new GridLayout(0, 5));Set the arrow buttons for all the locations −frame.add(new BasicArrowButton(BasicArrowButton.EAST)); frame.add(new BasicArrowButton(BasicArrowButton.NORTH)); frame.add(new BasicArrowButton(BasicArrowButton.SOUTH)); frame.add(new BasicArrowButton(BasicArrowButton.WEST));The following is an example to set all the arrow buttons in a frame −Examplepackage my; import java.awt.GridLayout; import javax.swing.Box; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.plaf.basic.BasicArrowButton; public class SwingDemo {    public static void main(String[] args) {       JButton button1 = new JButton("One");     ... Read More

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

365 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

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

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

677 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

488 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

218 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

272 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

818 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

Advertisements