Found 4337 Articles for Java 8

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

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

201 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

213 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

182 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 30-Jul-2019 22:30:26

78 Views

At first, let’s say, we have selected a range of columns using addColumnSelectionInterval() as shown in the demo screenshot −Now, we will deselect the above shown selected columns using removeColumnSelectionInterval(). The range is to be set here for interval i.e column 1 to 2 will get deselected −table.removeColumnSelectionInterval(1, 2);The following is our example to deselect a range of columns −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

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

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

182 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 30-Jul-2019 22:30:26

532 Views

To remove the first row from a table, use the removeRow() method and set its parameter to 0 since you need to remove the first row i.e. index 0.Let us first see an example to display a table with rows and columns. The table here has 9 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);       tableModel.addColumn("Language/ Technology");       tableModel.addColumn("Text Tutorial");       ... 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

442 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

933 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 30-Jul-2019 22:30:26

991 Views

To increase the row height, use the setRowHeight() method for a table in Java. Row height is the height of the row in pixels.Let’s say the following is our table −DefaultTableModel tableModel = new DefaultTableModel(); JTable table = new JTable(tableModel);Set the rows and columns and increase the row height by first getting the current height and incrementing it. We are incrementing by 20 pixels here −table.setRowHeight(table.getRowHeight() + 20);The following is an example to increase the row height −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 { ... Read More

Advertisements