Found 4337 Articles for Java 8

How to set a MatteBorder from BorderFactory in Java?

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

98 Views

Set a MatteBorder from BorderFactory class −MatteBorder border = (MatteBorder)BorderFactory.createMatteBorder(2, -1, 5, 10, icon);Now, set the above created MatteBorder to a component −JButton button = new JButton("Matte Border"); button.setBorder(border);The following is an example to set a MatteBorder from BorderFactory class −Examplepackage my; import java.awt.BorderLayout; import java.awt.Container; import javax.swing.BorderFactory; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.border.Border; import javax.swing.border.EtchedBorder; import javax.swing.border.MatteBorder; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Demo");       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       Border raisedBorder = new EtchedBorder(EtchedBorder.RAISED);       ImageIcon icon = new ImageIcon("new.gif");   ... Read More

How to apply adjustments to the last column of a JTable only, when the width of any column is changed in Java Swing?

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

264 Views

To apply adjustments to the last column only, use the setAutoResizeMode and set the mode. The mode here would be AUTO_RESIZE_LAST_COLUMN. This will allow you to adjust only the last columns even if any of the column header is dragged to resize.Let us first see an example to create a table in Java −Examplepackage my; import java.awt.Color; import java.awt.Font; 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("Technology"); ... Read More

How to create and set an Empty Border from BorderFactory class in Java?

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

160 Views

To create and set empty border to a component, use the BorderFactory class createEmptyBorder() method −EmptyBorder emptyBorder = (EmptyBorder) BorderFactory.createEmptyBorder();To set the above border to a component, use the setBorder() method −JButton button = new JButton("Empty Border"); button.setBorder(emptyBorder);The following is an example to create and set and empty border from BorderFactory class −package my; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.border.Border; import javax.swing.border.EmptyBorder; import javax.swing.border.SoftBevelBorder; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Demo");       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       ... Read More

How to get the number of rows and columns of a JTable in Java Swing

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

1K+ Views

To count the number of rows of a table, use the getRowCount() method −table.getRowCount()To count the number of columns of a table, use the getColumnCount() method −table.getColumnCount()The following is an example to get the number of rows and columns of a JTable −Examplepackage my; import java.awt.Font; 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");       tableModel.addColumn("Video Tutorial");     ... Read More

How to add empty border to a JButton in Java?

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

359 Views

To add empty border to a component, use the BorderFactory class createEmptyBorder() method −Border emptyBorder = BorderFactory.createEmptyBorder(10, 10, 0, 0);To set the above border to a component, use the setBorder() method −JButton button = new JButton("Empty Border"); button.setBorder(emptyBorder);The following is an example to ad empty border to a JButton −Examplepackage my; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.border.Border; import javax.swing.border.SoftBevelBorder; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Demo");       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       Border raisedBorder = new SoftBevelBorder(SoftBevelBorder.RAISED, ... Read More

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

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

333 Views

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

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

2K+ Views

Use the JComboBox to add a combo box to a table -JComboBox comboBox = new JComboBox();Now, add the combo box items -comboBox.addItem("Asia"); comboBox.addItem("Europe"); comboBox.addItem("North America"); comboBox.addItem("South America");Now, use the TableColumn and set the column wherein you want to set the above ComboBox items -TableColumn testColumn = table.getColumnModel().getColumn(0);Set the editor with the combo box -testColumn.setCellEditor(new DefaultCellEditor(comboBox)); The following is an example to add JComboBox to JTable: package my; import java.awt.Color; import java.awt.Font; import javax.swing.DefaultCellEditor; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.JTableHeader; import javax.swing.table.TableColumn; public class SwingDemo {    public static void main(String[] argv) throws Exception {     ... Read More

How to prevent resizing columns in a JTable

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

816 Views

To prevent resizing columns, use the method setResizingAllowed(). Here, we will set setResizingAllowed() to false for table header to disallow resizing of columns from header −table.getTableHeader().setResizingAllowed(false);Let us first see an example wherein we can easily resize columns in a table by resizing the table column header −Examplepackage my; import java.awt.Color; import java.awt.Font; 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

How to highlight a row in a table with Java Swing?

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

943 Views

To highlight a row in a table, you can use the addRowSelectionInterval() method. At first create a table −DefaultTableModel tableModel = new DefaultTableModel(); JTable table = new JTable(tableModel);Add some columns −tableModel.addColumn("Language/ Technology"); tableModel.addColumn("Text Tutorial"); tableModel.addColumn("Video Tutorial"); tableModel.addColumn("Views");Now, add rows to the table −tableModel.addRow(new Object[] { "NodeJS", "No", "Yes", "2350"}); tableModel.addRow(new Object[] { "MVC", "Yes", "No", "1500"}); tableModel.addRow(new Object[] { "ASP.NET", "Yes", "Yes", "3400"}); tableModel.addRow(new Object[] { "F#", "Yes", "No", "7890"}); tableModel.addRow(new Object[] { "Blockchain", "Yes", "No", "10600"}); tableModel.addRow(new Object[] { "SharePoint", "Yes", "Yes", "4900"});Highlight a single row by adding interval of rows. Set the same index for both the parameters ... Read More

Java program to lay out components in a flow to be centered with FlowLayout?

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

155 Views

Use FlowLayout.CENTER to lay out components in a flow to be centered with FlowLayout. The following is an example to lay out components in a flow to be centered with FlowLayout −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 {    public static void main(String[] args) {       JFrame frame = new JFrame("WorldCup2019");       frame.setLayout(new FlowLayout(FlowLayout.CENTER));       JLabel label = new JLabel("WorldCup Hosting Country ");       label.setPreferredSize(new Dimension(220, 70));       label.setOpaque(true);       label.setBackground(Color.ORANGE);     ... Read More

Advertisements