Found 4335 Articles for Java 8

Java Program to set the content of the JLabel to be right-justified and bottom-aligned

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

92 Views

To set the text of the label component to be right-justified and bottom-aligned, you need to set the alignment. Set the label to be on the right and bottom aligned −JLabel label = new JLabel("Total Runs", JLabel.RIGHT); label.setVerticalAlignment(JLabel.BOTTOM);Here, we have set the size of the label as well as the color that includes foreground and background color −label.setPreferredSize(new Dimension(220, 70)); label.setOpaque(true); label.setBackground(Color.YELLOW); label.setForeground(Color.RED);The following is an example to set the content of the lable to be right-justified and bottom-aligned −Examplepackage my; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextArea; import javax.swing.WindowConstants; public class SwingDemo ... Read More

Java Program to set the height of only a single row in a JTable with multiple rows

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

596 Views

To set the row height, use the setRowHeight and set a parameter that would be the number of pixels you need to set the row height.However, if you want to set the row height of a single row then you need to use the same method, but set an additional parameter as shown below −table.setRowHeight(3, 30);The above sets row height to 30 pixels for row index 4.The following is an example to set the height of a row in a 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 Exception {       DefaultTableModel tableModel = new DefaultTableModel();       JTable table = new JTable(tableModel);       ... Read More

Java Program to append a row to a JTable in Java Swing

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

608 Views

To append a row, you can use the addRow() method. Ley us first create a table wherein we need to append a row −DefaultTableModel tableModel = new DefaultTableModel(); JTable table = new JTable(tableModel);Add some columns to the table −tableModel.addColumn("Language/ Technology"); tableModel.addColumn("Difficulty Level");Add some rows −tableModel.insertRow(0, new Object[] { "CSS", "Easy" }); tableModel.insertRow(0, new Object[] { "HTML5", "Easy"}); tableModel.insertRow(0, new Object[] { "JavaScript", "Intermediate" }); tableModel.insertRow(0, new Object[] { "jQuery", "Intermediate" }); tableModel.insertRow(0, new Object[] { "AngularJS", "Difficult"});Now, if you need to append a row to the table we created above, use the addrow() method −tableModel.addRow(new Object[] { "WordPress", "Easy" });The ... Read More

How to add a title to JTable in Java Swing?

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

1K+ Views

To display a title to the JTable, you can set a title for the JPanel, which already consists of a JTable.Here, we are using createTitledBorder() for the JPanel to set the title for the panel border that would eventually work for table title.Let’s say the following is the JPanel −JPanel panel = new JPanel();Now, use setBorder() and the BorderFactory class to set a title border for the panel that would be our table title as well −panel.setBorder(BorderFactory.createTitledBorder(    BorderFactory.createEtchedBorder(), "My Demo Table", TitledBorder.LEFT, TitledBorder.TOP));The following is an example to add a title to a JTable −Examplepackage my; import javax.swing.BorderFactory; import ... Read More

How to set Echo Char for JPasswordField in Java?

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

3K+ Views

With echo char, you can set a character that would appear whenever a user will type the password in the JPasswordField.Let us first create a new JPassword field −JPasswordField passwd = new JPasswordField();Now, use the setEchoChar() to set the echo char for password field. Here, we have asterisk (*) as the field for password −passwd.setEchoChar('*');The following is an example to set echo char for password field −Examplepackage my; import java.awt.GridLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.SwingConstants; public class SwingDemo {    public static void main(String[] args) throws Exception {       JFrame frame = new ... Read More

How to set JCheckBoxMenuItem for a MenuItem in Java?

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

93 Views

Let’s say we have a menu and within that we need to set the JCheckBoxMenuItem. Here’s the Menu −JMenu editMenu = new JMenu("Edit"); editMenu.setMnemonic(KeyEvent.VK_E); menuBar.add(editMenu);Now, set the JCheckBoxMenuItem and add it to the editMenu created above −JCheckBoxMenuItem checkMenuItem = new JCheckBoxMenuItem("SelectAll"); checkMenuItem.setMnemonic(KeyEvent.VK_B); editMenu.add(checkMenuItem);The following is an example to set JCheckBoxMenuItem for a MenuItem in Java −Examplepackage my; import java.awt.Color; import java.awt.event.KeyEvent; import javax.swing.JCheckBoxMenuItem; 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 main(final String args[]) {       JFrame frame = new JFrame("MenuBar Demo");       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     ... Read More

How to create a Default Cell Editor that uses a JCheckBox in Java?

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

198 Views

Create a check box first and set valueJCheckBox checkBox = new JCheckBox("In-Stock");Set the JCheckBox for the editor so that the editor uses the check box −TreeCellEditor editor = new DefaultCellEditor(comboBox); tree.setEditable(true); tree.setCellEditor(editor);The following is an example to create a Default Cell Editor that uses a JCheckBox −Examplepackage my; import javax.swing.DefaultCellEditor; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.TreeCellEditor; 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");       ... Read More

How to set foreground color for different words in a JTextPane

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

920 Views

To set the foreground color for different words, use the SimpleAttributeSet and StyleConstants class. With that, use StyledDocument class as well for different words style. For different words, use insertString().At first, create a new JTextPane -At first, create a new JTextPane: JTextPane pane = new JTextPane();Now, use the classes to set the style and color for some words −StyledDocument doc = textPane.getStyledDocument(); Style style = textPane.addStyle("", null); StyleConstants.setForeground(style, Color.red); StyleConstants.setBackground(style, Color.white); doc.insertString(doc.getLength(), "Game of Thrones ", style);Now, style some other words differently −StyleConstants.setForeground(style, Color.yellow); StyleConstants.setBackground(style, Color.gray); doc.insertString(doc.getLength(), "Season 8", style);The following is an example to set foreground color for different ... Read More

Set whether the column in the table model can be selected or deselected in Java?

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

36 Views

We can set or disallow selection of column in the table using setColumnSelectionAllowed().Let’s say the following is our table −DefaultTableModel tableModel = new DefaultTableModel(); JTable table = new JTable(tableModel);If you want to allow selection of column, then set the method to TRUE −table.setColumnSelectionAllowed(true);If you want to disallow selection of column, then set the method to FALSE −table.setRowSelectionAllowed(false);Here, in the below example we have disallowed selection 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();     ... Read More

Java Program to set a spinner of dates in Java

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

157 Views

To set a spinner of dates, at first create a date object −Date today = new Date();Now, use SpinnerDateModel −JSpinner spinner2 = new JSpinner(new SpinnerDateModel(today, null, null, Calendar.MONTH));We will now consider a DateEditor, which is an editor for a JSpinner whose model is a SpinnerDateModel. Set the format for month here −JSpinner.DateEditor editor = new JSpinner.DateEditor(spinner2, "MM"); spinner2.setEditor(editor);The following is an example to set a spinner of date in Java −Examplepackage my; import java.awt.GridBagLayout; import java.util.Calendar; import java.util.Date; import javax.swing.*; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame("Spinner Demo"); ... Read More

Advertisements