Found 7442 Articles for Java

How to add a new row to JTable with insertRow() in Java Swing

Alshifa Hasnain
Updated on 24-Dec-2024 17:46:46

8K+ Views

The JTable component in Java Swing is widely used for displaying and managing tabular data. Adding a new row dynamically to a JTable can enhance user interactivity, especially in applications that require real-time data manipulation. In this article we will tell you a step-by-step procedure, to use the insertRow() method to add a new row to a JTable, complete with a sample implementation and an algorithm. What is JTable in Java Swing? JTable is a Swing component used for displaying and editing tabular data. It is highly customizable and supports features like sorting, selection, and dynamic data manipulation, making it ... Read More

How to set the alignment of the JLabel content along the X axis on the left in Java

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

153 Views

To set the alignment of the label content along the X axis on the left, use the setHorizontalAlignment() method and set the location. Let us first set a label component. We have set the label background color as well so that we can check the alignment of the label’s content properly −JLabel label = new JLabel("Country "); label.setPreferredSize(new Dimension(190, 100)); label.setOpaque(true); label.setBackground(Color.BLUE); label.setForeground(Color.WHITE);Now, we will align the label content along the X axis on the left by seeting location as LEFT −label.setHorizontalAlignment(JLabel.LEFT);The following is an example to set the alignment of the JLabel content along the X axis on the ... Read More

Add multiple number input fields with JOptionPane and display the sum in Console with Java

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

2K+ Views

At first, set multiple number input fields −JTextField text1 = new JTextField(10); JTextField text2 = new JTextField(10); JTextField text3 = new JTextField(10); JTextField text4 = new JTextField(10); JTextField text5 = new JTextField(10); JTextField text6 = new JTextField(10); JTextField text7 = new JTextField(10); JTextField text8 = new JTextField(10); panel.add(text1); panel.add(text2); panel.add(text3); panel.add(text4); panel.add(text5); panel.add(text6); panel.add(text7); panel.add(text8);Now, let us add the values of the multiple number input fields created above −System.out.println(Integer.parseInt(text1.getText()) + Integer.parseInt(text2.getText()) +    Integer.parseInt(text3.getText())+ Integer.parseInt(text4.getText())+    Integer.parseInt(text5.getText())+ Integer.parseInt(text6.getText())+    Integer.parseInt(text7.getText())+ Integer.parseInt(text8.getText()));Above, we have displayed the sum in the Console.The following is an example to sum multiple number input fields with ... Read More

How to use JOptionPane with Array Elements in Java?

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

1K+ Views

Let us first create and array and add elements −String[] sports = { "Football", "Cricket", "Squash", "Baseball", "Fencing", "Volleyball", "Basketball" };Now, set the above array elements to the JOptionPane −String res = (String) JOptionPane.showInputDialog(null, "Which sports you play the most?", "Sports",    JOptionPane.PLAIN_MESSAGE, null, sports, sports[0]);Above, we have also set the initial value i.e. sports(0).The following is an example to use JOptionPane with array elements in Java −Examplepackage my; import javax.swing.JOptionPane; public class SwingDemo {    public static void main(String[] args) {       String[] sports = { "Football", "Cricket", "Squash", "Baseball", "Fencing", "Volleyball", "Basketball" };       ... Read More

Java program to place component in bottom-right corner with BorderLayout

Smita Kapse
Updated on 19-Sep-2024 21:57:30

1K+ Views

In this article, we will learn how to place a component, specifically a button, in the bottom-right corner of a Java Swing application using the BorderLayout manager. The BorderLayout is one of the most commonly used layout managers in Java, and it allows you to place components in five different regions: North, South, East, West, and Center. We will demonstrate how to use BorderLayout to ensure the component is placed precisely in the bottom-right corner of the window. Steps to place the component in the bottom-right corner Following are the steps to place the component in the bottom-right corner with ... Read More

Java program to set an icon for JOptionPane

Anvi Jain
Updated on 20-Sep-2024 21:38:14

2K+ Views

In this program, we will learn how to set a custom icon in a JOptionPane using Java's Swing framework. A JOptionPane allows displaying dialog boxes for different purposes such as messages, inputs, or confirmations. In this example, we will create a dialog box with a custom image icon displayed alongside some text. This can be particularly useful for creating visually appealing user interfaces. Problem Statement Write a Java program to set an icon for JOptionPane. Below is the demonstration of the same − Output Steps to set an icon for JOptionPane Following are the steps to set an ... Read More

How to set Line Border color and width with Java?

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

5K+ Views

To set Line Border color and width, use the LineBorder. At first, set a panel wherein we need to set the line border −JPanel panel = new JPanel();Now, create a border and set on the panel created above −Border border = new LineBorder(Color.ORANGE, 4, true); panel.setBorder(border);The following is an example to set LineBorder color and width −package my; import java.awt.Color; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.border.Border; import javax.swing.border.LineBorder; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame();       JPanel panel = new JPanel();   ... Read More

How to add components with a relative Y position in Java?

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

151 Views

To add components with a relative Y position, use the GridBagConstraints.RELATIVE constant. Set this to gridy field −GridBagConstraints constraints = new GridBagConstraints(); constraints.gridy = GridBagConstraints.RELATIVE;The following is an example to add components with a relative Y position in Java −Examplepackage my; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame();       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       JPanel panel = new JPanel();       panel.setLayout(new GridBagLayout());       GridBagConstraints constraints = new GridBagConstraints();       constraints.gridx = ... Read More

How to create Vertical progress bar occupying the entire frame in Java

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

260 Views

For this, create a vertical progress bar −JProgressBar progressBar = new JProgressBar(JProgressBar.VERTICAL); progressBar.setEnabled(true);Also, set the bounds −progressBar.setBounds(70, 50, 120, 30);The following is an example to create vertical progress bar occupying the entire frame −Examplepackage my; import java.awt.BorderLayout; import java.awt.Color; import javax.swing.JFrame; import javax.swing.JProgressBar; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame();       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       JProgressBar progressBar = new JProgressBar(JProgressBar.VERTICAL);       progressBar.setEnabled(true);       progressBar.setBounds(70, 50, 120, 30);       progressBar.setBackground(Color.orange);       progressBar.setForeground(Color.white);       progressBar.setStringPainted(true);       ... Read More

How to customize how a JTabbedPane looks in Java?

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

895 Views

To customize how a JTabbedPane looks, change its font style, font face, font size and the background and foreground colors.Let’s say the following is the JTabbedPane −JTabbedPane tabbedPane = new JTabbedPane();Now, let us customize the above created JTabbedPane −tabbedPane.setBackground(Color.orange); tabbedPane.setForeground(Color.white); Font font = new Font("Verdana", Font.CENTER_BASELINE, 18); tabbedPane.setFont(font);The following is an example to customize JTabbedPane −Examplepackage my; import javax.swing.*; import java.awt.*; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Technologies");       JTabbedPane tabbedPane = new JTabbedPane();       JPanel panel1, panel2, panel3, panel4, panel5;       ... Read More

Advertisements