Found 133 Articles for Swing

How to display a JFrame to the center of a screen in Java?

Updated on 03-Jul-2020 13:39:50
A JFrame is a subclass of Frame class and the components added to a frame are referred to as its contents, these are managed by the contentPane. We can add the components to a JFrame to use its contentPane instead. A JFrame is like a Window with border, title, and buttons. We can implement most of the java swing applications using JFrame.By default, a JFrame can be displayed at the top-left position of a screen. We can display the center position of JFrame using the setLocationRelativeTo() method of Window class.Syntaxpublic void setLocationRelativeTo(Component c)Exampleimport javax.swing.*; import java.awt.*; public class JFrameCenterPositionTest extends JFrame {    public JFrameCenterPositionTest() {     ... Read More

How to display a JRadioButtonMenuItem in Java?

Updated on 03-Jul-2020 12:47:44
A JRadioButtonMenuItem is a subclass of the JMenuItem class in Java. A JRadioButtonMenuItem is a menu item that is part of a group of menu items in which only one item in the group can be selected and the selected item displays its selected state. We can add multiple radio button menu items to a ButtonGroup object to form a button group. If one radio button menu item in a button group is selected, all other radio button menu items will be unselected.Syntaxpublic class JRadioButtonMenuItem extends JMenuItem implements AccessibleExampleimport javax.swing.*; import java.awt.*; public class JRadioButtonMenuItemTest extends JFrame {    private JMenuBar mb;   ... Read More

When can we use the pack() method in Java?

Updated on 03-Jul-2020 12:33:28
The pack() method is defined in Window class in Java and it sizes the frame so that all its contents are at or above their preferred sizes. An alternative to the pack() method is to establish a frame size explicitly by calling the setSize() or setBounds() methods. In general, using the pack() method is preferable to call than setSize() method, since pack leaves the frame layout manager in charge of the frame size and layout managers are good at adjusting to platform dependencies and other factors that affect the component size.Syntaxpublic void pack()Exampleimport java.awt.*; import javax.swing.*; public class PackMethodTest extends JFrame {    public ... Read More

How can we apply different borders to JButton in Java?

Updated on 03-Jul-2020 12:29:27
A JButton is a subclass of AbstractButton class and it can be used for adding platform-independent buttons in a Java Swing application. A JButon can generate an ActionListener interface when the user clicking on a button, it can also generate MouseListener when a user can do some actions from the mouse and KeyListener when a user can do some actions from the keyboard.We can set different borders like LineBorder, BevelBorder, EtchcedBorder, EmptyBorder, TitledBorder, etc to JButton using the setBorder() method of JComponent class.Syntaxpublic void setBorder(Border border)Exampleimport javax.swing.*; import java.awt.*; public class JButtonBordersTest extends JFrame {    private JButton button[];    private JPanel panel;    public JButtonBordersTest() {   ... Read More

How to set a different look and feels to swing components in Java?

Updated on 03-Jul-2020 12:21:53
The Java Swing allows us to customize the GUI by changing the look and feel(L&F). The look defines the general appearance of components and the feel defines their behavior. L&Fs are subclasses of the LookAndFeel class and each L&F is identified by its fully qualified class name. By default, the L&F is set to the Swing L&F ( Metal L&F)To set the L&F programmatically, we can call the method setLookAndFeel () of the UIManager class. The call to setLookAndFeel must be done before instantiating any Java Swing class, otherwise, the default Swing L&F will be loaded.Syntaxpublic static void setLookAndFeel(LookAndFeel newLookAndFeel) throws UnsupportedLookAndFeelExceptionExampleimport java.awt.*; import ... Read More

How can we set the border to JComboBox items in Java?

Updated on 03-Jul-2020 11:58:06
A JComboBox is a subclass of JComponent class and it is a combination of a text field and a drop-down list from which the user can choose a value. A JComboBox can generate an ActionListener, ChangeListener and ItemListener interfaces when the user actions on a combo box. We can set the border to the items of a JComboBox by rendering a JComboBox which extends the DefaultListCellRenderer class and need to override the getListCellRendererComponent() method.Syntaxpublic Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)Exampleimport java.awt.*; import javax.swing.*; public class JComboBoxTest extends JFrame {    public JComboBoxTest() {       setTitle("JComboBox Test");       ... Read More

How can we rotate a JLabel text in Java?

Updated on 03-Jul-2020 11:56:43
A JLabel is a subclass of JComponent class and an object of JLabel provides text instructions or information on a GUI. A JLabel can display a single line of read-only text, an image or both text and an image. A JLabel can explicitly generate a PropertyChangeListener interface. By default, JLabel can display a text in the horizontal position and we can rotate a JLabel text by implementing the rotate() method of Graphics2D class inside the paintComponent().Syntaxpublic abstract void rotate(double theta, double x, double y)Exampleimport java.awt.*; import java.awt.geom.*; import javax.swing.*; public class RotateJLabelTest extends JFrame {    public RotateJLabelTest() {       setTitle("Rotate JLabel");       JLabel label ... Read More

How can we add padding to a JTextField in Java?

Updated on 03-Jul-2020 11:49:59
A JTextField is a subclass of JTextComponent class and it is one of the most important components that allow the user to input text value in a single-line format. A JTextField class will generate an ActionListener interface when we trying to enter some input inside it. The important methods of a JTextField class are setText(), getText(), setBorder(), setEnabled(),  etc. We can add padding to a JTextField using the setMargin(Insets s) of JTextComponent class.Syntaxpublic void setMargin(Insets m)Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class JTextfieldPaddingTest extends JFrame {    private JTextField jtf;    public JTextfieldPaddingTest() {       jtf = new JTextField("Welcome to Tutorials Point"); ... Read More

How can we filter a JTable in Java?

Updated on 12-Feb-2020 07:54:44
A JTable provides a very flexible possibility to create and display tables. The TableModel interface defines methods for objects that specify the contents of a table. The AbstractTableModel class is typically extended to provide a custom implementation of a model table. A JTable class provides the ability to edit tables using the method setCellEditor() allows an object of the TableCellEditor interface.We can filter a table using the setRowFilter() method of TableRowSorter class.Exampleimport java.awt.*; import java.awt.event.*; import java.util.regex.*; import javax.swing.*; import javax.swing.table.*; public class FilterTableTest extends JFrame {    private JTable table;    private TableModel model;    public FilterTableTest() {       setTitle("FilterTable Test");       ... Read More

How to validate if JTable has an empty cell in Java?

Updated on 12-Feb-2020 08:03:55
A JTable is a subclass of JComponent class for displaying complex data structures. A JTable can follow the Model View Controller (MVC) design pattern for displaying the data in rows and columns. A JTable will generate TableModelListener, TableColumnModelListener, ListSelectionListener, CellEditorListener and RowSorterListener interfaces.We can validate whether the JTable cell is empty or not by implementing the getValueAt() method of JTable class. If we click on the "Click Here" button, it will generate an action event and display a popup message like "Field is Empty" to the user.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; public class JTableEmptyValidateTest extends JFrame {    private JPanel panel;    private JTable table;   ... Read More
1 2 3 4 5 ... 14 Next