Found 112 Articles for AWT

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

364 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

How can we implement editable JComboBox in Java?

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

953 Views

JComboBoxA JComboBox can extend JComponent class and it is a combination of a text field and a drop-down list from which the user can choose a value.If the text field portion of the control is editable, the user can enter a value in the field or edit a value retrieved from the drop-down list.By default, the user not allowed to edit the data in the text field portion of the JComboBox. If we want to allow the user to edit the text field, call setEditable(true) method.A JComboBox can generate an ActionListener, ChangeListener or ItemListener when the user actions on a combo box.A ... Read More

How can we implement a scrollable JPanel in Java?

raja
Updated on 10-Feb-2020 06:35:13

3K+ Views

JPanelA JPanel is a subclass of JComponent (a subclass of a Container class). Therefore, JPanel is also a Container.A JPanel is an empty area that can be used either to layout other components including other panels.In a JPanel, we can add fields, labels, buttons, checkboxes,  and images also.The Layout Managers such as FlowLayout, GridLayout, BorderLayout and other layout managers helps us to control the sizes, positions, and alignment of the components using JPanel.The important methods of a JPanel class are getAccessibleContext(), getUI(), updateUI() and paramString().We can also implement a JPanel with vertical and horizontal scrolls by adding the panel object to JScrollPane.Exampleimport java.awt.*; ... Read More

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