Found 133 Articles for Swing

How can we set the background color to a JPanel in Java?

raja
Updated on 10-Feb-2020 06:58:58

13K+ Views

A JPanel is a container and it is an invisible component in Java. The FlowLayout is a default layout for a JPanel. We can add most of the components like buttons, text fields, labels, table, list, tree and etc. to a JPanel. We can set a background color to JPanel by using the setBackground() method.Exampleimport java.awt.* import javax.swing.*; public class JPanelBackgroundColorTest extends JFrame {    private JPanel panel;    public JPanelBackgroundColorTest() {       setTitle("JPanelBackgroundColor Test");       panel = new JPanel();       panel.add(new JLabel("Welcome to Tutorials Point"));       panel.setBackground(Color.green);       add(panel, BorderLayout.CENTER);       ... Read More

How many types of selection modes for a JList in Java?

raja
Updated on 10-Feb-2020 07:01:22

461 Views

A JList is a component that can extend JComponent class used to display a list of objects that allows the user to select one or more items.There are three types of selection modes for a JList in JavaListSelectionModel.SINGLE_SELECTION: Only one list index can be selected at a time.ListSelectionModel.SINGLE_INTERVAL_SELECTION: Only one contiguous interval can be selected at a time.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION: In this mode, there is no restriction on what can be selected. This is a default mode.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class JListSelectionModeTest extends JFrame implements ActionListener {    private JList list;    private DefaultListModel listModel;    public JListSelectionModeTest() {     ... Read More

How to implement a program to count the number in Java?

raja
Updated on 10-Feb-2020 06:17:01

1K+ Views

The program uses a JLabel to hold a count label, a JTextField component to hold the number count, JButton component to create add, remove and reset buttons. When we click the add button, the count in the JTextField will get incremented by '1' and by clicking the remove button the count will be decremented by '1'. If we click the Reset button, it will reset the count to '0'.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class CounterTest extends JFrame implements ActionListener {    private JLabel label;    private JTextField text;    private JButton addBtn, removeBtn, resetBtn;    private int count;    public CounterTest() {       setTitle("Counter Test");   ... Read More

How can we set a border to JCheckBox in Java?

raja
Updated on 10-Feb-2020 06:19:45

334 Views

A JCheckBox is a component that can extend JToggleButton and an object of JCheckBox represents an option that can be checked or unchecked. If there are two or more options then any combination of these options can be selected at the same time. We can set a border to the JCheckBox component by using the setBorder() method and make sure that setBorderPainted() method set to true.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class BorderedJCheckBoxTest extends JFrame {    private JCheckBox jcb;    public BorderedJCheckBoxTest() throws Exception {       setTitle("JCheckBox Test");       setLayout(new FlowLayout());       jcb = new JCheckBox("BorderedJCheckBox Test");   ... Read More

How can we implement cut, copy and paste functionality of JTextField in Java?

raja
Updated on 10-Feb-2020 06:20:50

1K+ Views

A JTextField is a subclass of JTextComponent class that allows the editing of a single line of text. We can implement the functionality of cut, copy and paste in a JTextField component by using cut(), copy() and paste() methods. These are pre-defined methods in a JTextFeild class.Syntaxpublic void cut() public void copy() public void paste()Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class JTextFieldCutCopyPasteTest extends JFrame {    private JTextField textField;    private JButton cutButton, copyButton, pasteButton;    public JTextFieldCutCopyPasteTest() {       setTitle("JTextField CutCopyPaste Test");       setLayout(new FlowLayout());       textField = new JTextField(12);       ... Read More

What are the differences between a JTextField and a JFormattedTextField in Java?

raja
Updated on 10-Feb-2020 06:27:23

1K+ Views

A JTextField can be used for plain text whereas a JFormattedTextField is a class that can extend JTextField and it can be used to set any format to the text that it contains phone numbers, e-mails, dates and etc.JTextFieldA JTextFeld is one of the most important components that allow the user to type an input text value in a single line format.A JTextField can generate an ActionListener interface when we try to enter some input inside the text field and it can generate a CaretListener interface each time the caret (i.e. the cursor) changes position.A JTextField can also generate a MouseListener and KeyListener interfaces.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class ... Read More

How can we implement a JLabel text with different color and font in Java?

raja
Updated on 10-Feb-2020 06:28:29

2K+ Views

JLabelA JLabel class can extend 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 also display a single line of text with different colors and fonts using Some Text tag inside a HTML tag.A JLabel can explicitly generate a PropertyChangeListener interface.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class MultiColorLabelTest extends JFrame {    public MultiColorLabelTest() {       setTitle("MultiColorLabel Test");       setLayout(new FlowLayout());       // multi colored with different font size label       JLabel ... Read More

How can we insert multiple tabs into a single JTabbedPane in Java?

raja
Updated on 11-Feb-2020 11:03:45

363 Views

JTabbedPaneA JTabbedPane is a component can extend JComponent class and we can able to see one tab at a time.Each tab is associated with a single component that can be displayed when the tab is selected.A JTabbedPane can generate a ChangeListener interface when a tab is selected.It is also possible to insert multiple tabs into a single JTabbedPane and the important methods of JTabbedPane are addTab(), fireStateChanged(), getTabPlacement(),  setSelectedIndex(),  getTabCount() and etc.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class NestedJTabbedPaneTest extends JFrame {    public NestedJTabbedPaneTest() {       setTitle("Nested JTabbedPane test");       setLayout(new BorderLayout());       JTabbedPane tabbedPane ... Read More

How can we draw a rounded rectangle using the Graphics object in Java?

raja
Updated on 10-Feb-2020 06:31:44

3K+ Views

Graphics ClassIn Java, the drawing takes place via a Graphics object, this is an instance of the java.awt.Graphics class.Each Graphics object has its own coordinate system and all the methods of Graphics including those for drawing Strings, lines, rectangles, circles, polygons and etc.We can get access to the Graphics object through the paint(Graphics g) method.We can use the drawRoundRect() method that accepts x-coordinate, y-coordinate, width, height, arcWidth, and arc height to draw a rounded rectangle.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class RoundedRectangleTest extends JFrame {    public RoundedRectangleTest() {       setTitle("RoundedRectangle Test");       setSize(350, 275);       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   ... Read More

How can we implement an editable JLabel in Java?

raja
Updated on 10-Feb-2020 06:32:53

264 Views

JLabelA JLabel class can extend 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 image.The important methods of a JLabel are setText(), setIcon(), setBackground(), setOpaque(), setHorizontalAlignment(), setVerticalAlignment() and etc.A JLabel can explicitly generate a PropertyChangeListener interface.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; import java.text.*; public class JEditableLabel extends JFrame {    public JEditableLabel() {       setTitle("JEditableLabel");       setLayout(new FlowLayout());       final JLabel label = new JLabel(" Welcome to Tutorials Point");       final ... Read More

Previous 1 ... 6 7 8 9 10 ... 14 Next
Advertisements