Found 133 Articles for Swing

How can we implement the HTML text of JButton in Java?

raja
Updated on 10-Feb-2020 07:28:09

145 Views

A JButton is a subclass of AbstractButton and it is an important component in the Java Swing hierarchy. A JButton can be used mostly in the login based applications. A JButton can generate an ActionListener interface when we trying to press or click a button. A JButton has a text or icon or both text and icon, we can also implement bold, italic text using HTML tags.Exampleimport java.awt.*; import javax.swing.*; public class JButtonHtmlTextTest extends JFrame {    private JButton jbutton1, jbutton2;    public JButtonHtmlTextTest() {       setTitle("JButtonHtmlText Test");       jbutton1 = new JButton("Normal Button");       jbutton1.setHorizontalAlignment(SwingConstants.CENTER);       add(jbutton1, BorderLayout.WEST); ... Read More

How can we implement the word wrap JTableHeader of a JTable in Java?

raja
Updated on 11-Feb-2020 11:07:07

514 Views

JTableHeaderA JTableHeader is a subclass of JComponent class.When we create a JTable object, the constructor creates a new JTableHeader object to manage the table component's header.The JTableHeader object associated with the table component's column model so that its UI delegate can drag the columns and render each column's header cell.A JTable supplies a setTableHeader() method that establishes the table header component's JTableHeader object and a getTableHeader() method that returns a reference to the table header component's JTableHeader object.We can implement the word-wrap table header of a JTable by customizing the DefaultTableModel class or AbstractTableModel class.Exampleimport java.util.*; import javax.swing.*; import javax.swing.table.*; public class WordWrappingTableHeaderTest extends JFrame {    private JTable table;    public WordWrappingTableHeaderTest() {   ... Read More

How can we set the shortcut key to a JButton in Java?

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

1K+ Views

A JButton is a subclass of AbstractButton and it can be used for adding platform-independent buttons to a Java Swing application. A JButon can generate an ActionListener interface when the button is pressed or clicked, it can also generate the MouseListener and KeyListener interfaces. We can also set the short cut keys for a JButton using the setMnemonic() method.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class JButtonTest extends JFrame {    private JButton button;    public JButtonTest() {       setTitle("JButtonTest");       button = new JButton("Click or press ALT-C");       button.setMnemonic('C');       add(button, BorderLayout.CENTER);       button.addActionListener(new ActionListener() ... Read More

How to change each column width of a JTable in Java?

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

6K+ Views

JTableA JTable is a subclass of JComponent for displaying complex data structures.A JTable can follow the Model View Controller (MVC) design pattern for displaying the data in rows and columns.The DefaultTableModel class can extend AbstractTableModel and it can be used to add the rows and columns to a JTable dynamically.The DefaultTableCellRenderer class can extend JLabel class and it can be used to add images, colored text and etc. inside the JTable cell.A JTable can generate TableModelListener, TableColumnModelListener, ListSelectionListener, CellEditorListener, RowSorterListener interfaces.By default the width of a JTable is fixed, we can also change the width of each column by using table.getColumnModel().getColumn().setPreferredWidth() method of JTable class.Exampleimport java.awt.*; import javax.swing.*; import javax.swing.table.*; public class ... Read More

How to display a bold text inside the JTextArea in Java?

raja
Updated on 10-Feb-2020 06:47:37

1K+ Views

A JTextArea class can extend JTextComponent and allow a user to enter multiple lines of text inside it. A JTextArea can generate a CaretListener interface, which can listen to caret update events. We can set a font to a text inside the JTextArea by using setFont() method.Exampleimport java.awt.*; import javax.swing.*; public class JTextAreaTextBoldTest extends JFrame {    private JTextArea textArea;    public JTextAreaTextBoldTest() {       setTitle("JTextAreaTextBold Test");       setLayout(new BorderLayout());       textArea= new JTextArea();       textArea.setLineWrap(true);       textArea.setWrapStyleWord(true);       Font boldFont=new Font(textArea.getFont().getName(), Font.BOLD, textArea.getFont().getSize());       textArea.setFont(boldFont); // bold ... Read More

What is the importance of JSeparator class in Java?

raja
Updated on 10-Feb-2020 06:49:41

138 Views

JSeparatorA JSeparator is a horizontal or vertical line or an empty space that separates the components.A JSeparator class is used to draw a line to separate the components in a Layout.The easiest way to add a separator to a menu or toolbar is to call the addSeparator() method provided by the classes JMenu, JPopupMenu and JToolBar.The important methods of JSeparator class are setOrientation() and getOrientation().Exampleimport java.awt.*; import javax.swing.*; public class JSeparatorTest extends JFrame {    private JLabel label1, label2;    public JSeparatorTest() {       setTitle("JSeparator Test");       setLayout(new GridLayout(0, 1));       label1 = new JLabel("Above Separator");       add(label1);       JSeparator sep ... Read More

What is the importance of JViewport class in Java?

raja
Updated on 11-Feb-2020 11:11:40

323 Views

JViewportA JViewport class defines the basic scrolling model and it is designed to support both logical scrolling and pixel-based scrolling.The viewport's child called the view is scrolled by calling JViewport.setViewPosition() method.A JViewport class supports logical scrolling, that is a kind of scrolling in which view coordinates are not pixels.To support a logical scrolling, JViewport defines a small set of methods that can be used to define the geometry of a viewport and a view. By default, these methods just report the pixel dimensions of the viewport and view.Exampleimport java.awt.*; import javax.swing.*; public class JViewportTest extends JFrame {    public JViewportTest() {       setTitle("JViewport Test"); ... Read More

What are the differences between paint() method and repaint() method in Java?

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

6K+ Views

Paint() and Repaint()paint(): This method holds instructions to paint this component. In Java Swing, we can change the paintComponent() method instead of paint() method as paint calls paintBorder(), paintComponent() and paintChildren() methods. We cannot call this method directly instead we can call repaint().repaint(): This method cannot be overridden. It controls the update() -> paint() cycle. We can call this method to get a component to repaint itself. If we have done anything to change the look of the component but not the size then we can call this method.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; public class PaintRepaintTest extends JPanel implements ... Read More

How can we catch a double click and enter key events for a JList in Java?

raja
Updated on 10-Feb-2020 06:55:05

494 Views

A JList can extend JComponent class that allows the user to choose either a single or multiple selections. A JList can generate a ListSelectiionListener interface and it includes one abstract method valueChanged(). A JList can also generate a MouseListener interface to catch a double click event in the list and generates a KeyListener interface to catch an enter key event.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import java.util.*; public class JListTest extends JFrame {    public JListTest() {       setTitle("JList Test");       setLayout(new FlowLayout());       Vector v = new Vector();       for (int i = 1; i < ... Read More

What are the differences between a Font and a FontMetrics in Java?

raja
Updated on 10-Feb-2020 06:57:14

1K+ Views

A Font class is used to set the screen fonts and it maps the characters of the language to their respective glyphs whereas a FontMetrics class defines a font metrics object, which encapsulates information about the rendering of a particular font on a particular screen.FontA Font class can be used to create an instance of a Font object to set the font for drawing text, label, text fields, buttons, etc and it can be specified by its name, style, and size.The fonts have a family name, a logical name and a face-namefamily name: It is the general name of the font, such as Courier.logical ... Read More

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