Found 4338 Articles for Java 8

Java Program to insert styled text in a JTextPane Component

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

427 Views

Insert styled text in a JTextPane component using the SimpleAttributeSet and StyleConstants classes. With that, we will also use StyledDocument.The following is an example to insert styled text in a JTextPane component −package my; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextPane; import javax.swing.text.BadLocationException; import javax.swing.text.SimpleAttributeSet; import javax.swing.text.Style; import javax.swing.text.StyleConstants; import javax.swing.text.StyledDocument; public class SwingDemo {    public static void main(String args[]) throws BadLocationException {       JFrame frame = new JFrame("Demo");       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       Container container = frame.getContentPane();       JTextPane textPane = new JTextPane();     ... Read More

Program to combine BorderLayout, GridLayout and FlowLayout in Java Swing?

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

1K+ Views

Here, we have set panels with BorderLayout, GridLayout and FlowLayout. Within the panels, we have created components such as Button, ComboBox, etc. The following is an example to combine layouts in Java −Examplepackage my; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JPanel; public class SwingDemo {    public static void main(String[] args) {       JButton btnA = new JButton("Button1 (Left)");       JButton btnB = new JButton("Button2 (Right)");       JButton btnC = new JButton("Button3 (Left)");       JButton btnD = new JButton("Button4 (Right)");       ... Read More

How to create titled border for a Panel in Java?

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

376 Views

To create titled border for a panel, use the createTitledBorder() method. Let us create a panel first −JPanel panel = new JPanel(); JButton btn1 = new JButton("One"); JButton btn2 = new JButton("Two"); panel.add(btn1); panel.add(btn2);Now, set titled border with BorderFactory class −panel.setBorder(BorderFactory.createTitledBorder("Title of the border"));The following is an example to create titled border for a panel in Java −Examplepackage my; import java.awt.Component; import java.awt.Dimension; import javax.swing.BorderFactory; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame();       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       frame.getContentPane().setLayout(new ... Read More

How to set vertical alignment for a component in Java?

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

3K+ Views

For vertical alignment, create a frame and set the layout using the BoxLayout manager −JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));Above, we have set the BoxLayout to set the alignment since it is a layout manager that allows multiple components to be laid out either vertically or horizontally. We have set vertical alignment here −BoxLayout.X_AXISThe following is an example to set vertical alignment for a component −Examplepackage my; import java.awt.Component; import java.awt.Dimension; import javax.swing.BorderFactory; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class SwingDemo {    public static void main(String[] args) {       JFrame frame ... Read More

How to create a Borderless Window in Java?

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

447 Views

To create a borderless window in Java, do not decorate the window. The following is an example to create a BorderLess Window −Examplepackage my; import java.awt.GraphicsEnvironment; import java.awt.GridLayout; import java.awt.Point; import javax.swing.JLabel; import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.JWindow; import javax.swing.SwingConstants; public class SwingDemo {    public static void main(String[] args) throws Exception {       JWindow frame = new JWindow();       JLabel label1, label2, label3;       frame.setLayout(new GridLayout(2, 2));       label1 = new JLabel("Id", SwingConstants.CENTER);       label2 = new JLabel("Age", SwingConstants.CENTER);       label3 = new JLabel("Password", SwingConstants.CENTER);     ... Read More

Set a component and place it next to the previously added component with GridBagLayout in Java

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

92 Views

We have set a component first −GridBagConstraints constraints = new GridBagConstraints(); constraints.gridy = 0; panel.add(new JButton("First row"), constraints);Now, we will place it next to the previously added component −constraints.gridx = GridBagConstraints.RELATIVE; constraints.gridy = 1; panel.add(new JButton("2nd row 1st column"), constraints); panel.add(new JButton("2nd row 2nd column"), constraints);Examplepackage my; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame();       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       JPanel panel = new JPanel();       panel.setLayout(new GridBagLayout());       GridBagConstraints constraints = new ... Read More

How to add components with a Relative X Position in Java

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

449 Views

To add components with a relative X position, use the GridBagConstraints.RELATIVE constant. Set this to gridx field −GridBagConstraints constraints = new GridBagConstraints(); constraints.gridx = GridBagConstraints.RELATIVE;The following is an example to add components with a relative X position in Java −Examplepackage my; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame();       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       JPanel panel = new JPanel();       panel.setLayout(new GridBagLayout());       GridBagConstraints constraints = new GridBagConstraints();       constraints.gridy = ... Read More

How to disallow resizing component with GridBagLayout in Java

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

658 Views

To disallow resizing component with GridBagLayout, use the GridBagConstraints NONE constant −GridBagConstraints gbc = new GridBagConstraints(); gbc.fill = GridBagConstraints.NONE;The following is an example to disallow resizing component with GridBagLayout −Examplepackage my; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.WindowConstants; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame("Demo Frame");       JPanel panel = new JPanel();       GridBagLayout layout = new GridBagLayout();       panel.setLayout(layout);       GridBagConstraints gbc = new GridBagConstraints();       gbc.fill = GridBagConstraints.NONE;   ... Read More

Distribute extra horizontal and vertical space in a GridBagLayout with Java

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

447 Views

To distribute the extra horizontal and vertical space, use the fields weightx and weighty. The following is an example to distribute extra horizontal and vertical space −Examplepackage my; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.WindowConstants; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame("Demo Frame");       JPanel panel = new JPanel();       GridBagLayout layout = new GridBagLayout();       panel.setLayout(layout);       GridBagConstraints gbc = new GridBagConstraints();       JLabel label = new JLabel("Rank: ");   ... Read More

How to set columnWeights and rowWeights in a GridBagLayout?

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

137 Views

Create a panel and set the layout −JPanel panel = new JPanel(); GridBagLayout layout = new GridBagLayout(); panel.setLayout(layout);Now, set the constrainst and with that the columnWeights and rowWeights −GridBagConstraints gbc = new GridBagConstraints(); JLabel label = new JLabel("Rank: "); JTextArea text = new JTextArea(); text.setText("Add rank here..."); layout.columnWeights = new double[]{0.0f, 0.0f, 2.0f}; layout.rowWeights = new double[]{0.0f, 1.0f};Now, set the constraints for the label and add it to the panel −gbc.gridx = 0; gbc.gridy = 0; layout.setConstraints(label, gbc); panel.add(label);The following is an example to set columnWeights and rowWeights in a GridBagLayout −Examplepackage my; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import javax.swing.JFrame; import ... Read More

Advertisements