Programming Articles - Page 2580 of 3366

Can we create nested TitiledBorder in Java?

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

173 Views

Yes, we can create nested TitledBorder. Let us first create a component for which we will set the border −JLabel label = new JLabel();Now, we will create the 1st border −TitledBorder border = BorderFactory.createTitledBorder("Top Border"); border.setTitlePosition(TitledBorder.TOP);Following is how we will creater border 2. We have set the 1st border here −TitledBorder border2 = new TitledBorder(border, "Bottom CENTER Border", TitledBorder.CENTER, TitledBorder.BOTTOM);Now, set it for the label component −label.setBorder(border2);The following is an example to create nested TitledBorder in Java −package my; import java.awt.BorderLayout; import java.awt.Container; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.border.TitledBorder; public class SwingDemo {    public static ... Read More

Program to get JTextArea font information

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

124 Views

Let’s say the following is our JTextArea −JTextArea textArea = new JTextArea("This is demo text.");Now, get the font using the Font class getFont() method as shown below −Font font = textArea.getFont(); System.out.println("Font = "+font);The following is an example to get JTextArea font information in Java −Examplepackage my; import java.awt.Font; import java.awt.GridLayout; import javax.swing.*; public class SwingDemo {    SwingDemo(){       JFrame frame = new JFrame();       JTextArea textArea = new JTextArea("This is demo text.");       Font font = textArea.getFont();       System.out.println("Font = "+font);       frame.add(textArea);       frame.setSize(550, 300); ... Read More

How to get the Tab Size of a JTextArea in Java?

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

375 Views

To get the tab size from a JTextArea, you can use the getTabSize() method −textArea.getTabSize();We will assign it to int variable and display the size in the Console −int size = textArea.getTabSize(); System.out.println("Tab Size = "+size);The following is an example to set the tab size of a JTextArea −Examplepackage my; import java.awt.GridLayout; import javax.swing.*; public class SwingDemo {    SwingDemo() {       JFrame frame = new JFrame();       JTextArea textArea = new JTextArea("This is demo text.");       int size = textArea.getTabSize();       System.out.println("Tab Size = "+size);       frame.add(textArea);     ... Read More

How to make JTable single selectable in Java?

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

290 Views

To make JTable single selectable in Java, you need to set the selection mode to be SINGE_SELECTION. Let’s say the following is our table −String[][] rec = {    { "001", "Shirts", "40" },    { "002", "Trousers", "250" },    { "003", "Jeans", "25" },    { "004", "Applicances", "90" },    { "005", "Mobile Phones", "200" },    { "006", "Hard Disk", "150" }, }; String[] header = { "ID", "Product", "Quantity" }; JTable table = new JTable(rec, header);Set the selection more to make it single selectable with setSelectionMode() method −table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);Let us see an example to set the ... Read More

How to deselect a range of rows from a table in Java?

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

265 Views

Let’s say, we have selected a range of rows using addRowSelectionInterval() as shown in the demo screenshot −Now, we will deselect the above shown selected rows using removeRowSelectionInterval(). The range is to be set here for interval i.e rows 3 to 6 (index 2 to 5) will get deselected −table.removeRowSelectionInterval(2, 5);The following is our example to deselect a range of rows −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); ... Read More

Java program to deselect a range of columns in a JTable

Chandu yadav
Updated on 14-Nov-2024 17:36:26

208 Views

In this article, we will learn how to deselect a range of columns in a JTable using Java. JTable is a part of the Swing framework in Java, which is used to display and edit tables. Sometimes, we may want to deselect a specific range of columns after selecting columns in a table. This can be done by using the removeColumnSelectionInterval() method. Problem Statement Given a JTable with columns selected, write a Java program to deselect a range of columns from the table. Input A JTable with columns selected.Output The specified range of columns will be deselected. Steps to ... Read More

How to create a border with a lowered beveled edge in Java?

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

283 Views

Use the createLoweredBevelBorder() method to create a border with a lowered beveled edge. We will set it on the label component −JLabel label; label = new JLabel("This has a border with a lowered bevel edge!"); label.setBorder(BorderFactory.createLoweredBevelBorder());The following is an example to create a border with a lowered beveled edge −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("This has a border with a lowered bevel edge!"); ... Read More

How to disable a MenuItem in Java?

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

1K+ Views

To disable a MenuItem, use the setEnabled() method and set it to FALSE. Let’s say we have the following MenuBar −JMenuBar menuBar = new JMenuBar();Now, create a Menu −JMenu fileMenu = new JMenu("File"); fileMenu.setMnemonic(KeyEvent.VK_F); menuBar.add(fileMenu);We will now create two MenuItems inside the above Menu −JMenuItem menuItem1 = new JMenuItem("New", KeyEvent.VK_N); fileMenu.add(menuItem1); JMenuItem menuItem2 = new JMenuItem("Open File", KeyEvent.VK_O); fileMenu.add(menuItem2);Now, let us disable the 2nd MenuItem −menuItem2.setEnabled(false);The following is an example to disable a MenuItem in Java −Examplepackage my; import java.awt.Color; import java.awt.event.KeyEvent; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.UIManager; public class SwingDemo {    public static void ... Read More

Java program to remove the first row from a table with DefaultTableModel

Ankith Reddy
Updated on 23-Oct-2024 17:30:38

806 Views

In this article, we will learn how to display a table with multiple rows and columns in Java using the JTable component. Additionally, we will demonstrate how to remove the first row from the table using the removeRow() method. The first program shows how to create and display a table with 9 rows, and the second program demonstrates how to remove the first row, leaving the remaining 8 rows intact.  Create a table with rows and columns using JTable Following are the steps to create a table with rows and columns using JTable − Start ... Read More

Move the first row to the end of the JTable in Java Swing

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

656 Views

To move the first row to the end of the table in Java, use the moveRow() method. It has three parameters. The first two parameters allows you to set the starting and ending row index to be moved. The last parameter sets the destination of the rows to be moved.As discussed above, move the first row to the end −tableModel.moveRow(0, 0, tableModel.getRowCount() - 1);The following is an example to move the first row to the end of the table −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 ... Read More

Advertisements