Programming Articles - Page 2578 of 3363

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

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

400 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

314 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

290 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

234 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

310 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

845 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

677 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

How to set font for text in JTextPane with Java?

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

1K+ Views

Use the Font class to set font for text. Let us first create JTextPane component −JTextPane textPane = new JTextPane();Now, set the font with the Font class setFont() method −Font font = new Font("Serif", Font.ITALIC, 18); textPane.setFont(font);The following is an example to set font for text −Examplepackage my; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextPane; import javax.swing.text.BadLocationException; import javax.swing.text.SimpleAttributeSet; import javax.swing.text.Style; import javax.swing.text.StyleConstants; import javax.swing.text.StyledDocument; public class SwingDemo {    public static void main(String args[]) throws BadLocationException {       JFrame frame = new JFrame("Demo");       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       ... Read More

Java program to increase the row height in a JTable

George John
Updated on 29-Sep-2024 02:48:44

2K+ Views

In the given article, we will learn to write a program in Java to increase the row height in a JTable. The example creates a table with different languages and their difficulty levels and then modifies the row height using the setRowHeight() method. Steps to increase the row height in a JTable  Following are the steps to increase the row height in a JTable − Import the necessary classes from javax.swing package. Initialize a DefaultTableModel and create a JTable with it. Add columns to represent languages and ... Read More

Advertisements