Found 4338 Articles for Java 8

How to set Raised and Lowered EtchedBorder for components in Java?

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

191 Views

To set raised EtchedBorder −Border raisedBorder = new EtchedBorder(EtchedBorder.RAISED);To set lowered EtchedBorder −Border loweredBorderEtched = new EtchedBorder(EtchedBorder.LOWERED);Now, set both the borders for components −JButton raisedButton = new JButton("Raised Border"); raisedButton.setBorder(raisedBorder); JLabel loweredLabel = new JLabel("Lowered Border Etched"); loweredLabel.setBorder(loweredBorderEtched);The following is an example to set raised and lowered EtchedBorder for components −Examplepackage my; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.border.Border; import javax.swing.border.EmptyBorder; import javax.swing.border.SoftBevelBorder; import javax.swing.border.EtchedBorder; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Demo");       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       ... Read More

Create a vertical slider with custom min, max, and initial value in Java

Ankith Reddy
Updated on 30-Jul-2019 22:30:26

100 Views

While creating vertical slider, we can set custom values as well. Let us take three integer variable and set the int values for min, max as well as the initial value of the slider −int val = 50; int min = 0; int max = 100;Set it to the slider while creating a new slider. Here, we have set the constant to be VERTICAL, since we are creating a vertical slider −JSlider slider = new JSlider(JSlider.VERTICAL, min, max, val);The following is an example to create a vertical slider with custom values −Examplepackage my; import java.awt.Color; import java.awt.Font; import javax.swing.JFrame; import ... Read More

How to create a horizontal slider with custom min, max, and initial value in Java

George John
Updated on 30-Jul-2019 22:30:26

208 Views

While creating horizontal slider, we can set custom values as well. Let us take three integer variable and set the int values for min, max as well as the initial value of the slider −int val = 75; int min = 0; int max = 100;Set it to the slider while creating a new slider. Here, we have set the constant to be HORIZONTAL, since we are creating a horizontal slider −JSlider slider = new JSlider(JSlider.HORIZONTAL, min, max, val);The following is an example to create a horizontal slider with custom min, max and initial value −Examplepackage my; import java.awt.Color; import ... Read More

How to create a JSpinner in Java with values as numbers?

Chandu yadav
Updated on 30-Jul-2019 22:30:26

176 Views

Use SpinnerNumberModel to create a spinner with value as numbers −SpinnerModel value = new SpinnerNumberModel(10, 0, 20, 1);Now set the values −JSpinner spinner = new JSpinner(value);The following is an example to create a JSpinner with values as numbers −Examplepackage my; import java.awt.Font; import javax.swing.*; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame("Spinner Demo");       SpinnerModel value = new SpinnerNumberModel(10, 0, 20, 1);       JSpinner spinner = new JSpinner(value);       spinner.setBounds(50, 80, 70, 100);       frame.add(spinner);       frame.setSize(550,300);       frame.setLayout(null);       frame.setVisible(true);    } }This will produce the following output −

How can we get the values of a JProgressBar Component and display in Console?

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

69 Views

Let’s say we have set the following values for JProgressBar −int min = 0; int max = 1000; progressBar = new JProgressBar(min, max);Now, get the above values and display in the Console −int value = progressBar.getValue(); System.out.println("Value = "+value); System.out.println("Minimum = "+progressBar.getMinimum()); System.out.println("Maximum = "+progressBar.getMaximum());The following is an example to get the values of a progress bar component −Examplepackage my; import javax.swing.*; public class SwingDemo extends JFrame {    JProgressBar progressBar;    int i = 0;    SwingDemo() {       int min = 0;       int max = 1000;       progressBar = new JProgressBar(min, ... Read More

How to set all the values at once using the model in a JProgressBar with Java?

Ankith Reddy
Updated on 30-Jul-2019 22:30:26

57 Views

The getModel() method is used to set all the values at once for a JProgressBar −int newVal = 5; int newMin = 0; int newMax = 100; progressBar.getModel().setRangeProperties(newVal, 0, newMin, newMax, true);The following is an example to set all the values at once using the model in a progress bar −Examplepackage my; import javax.swing.*; public class SwingDemo extends JFrame {    JProgressBar progressBar;    int i = 0;    SwingDemo() {       int min = 0;       int max = 1000;       progressBar = new JProgressBar(min, max);       int newVal = 5; ... Read More

Disable Tooltip for a component with an already enabled tooltip in Java

George John
Updated on 30-Jul-2019 22:30:26

359 Views

Let’s say we have set the tooltips for all the components using the following method in Java, for example −setToolTipText("Enter Age");Here, we have already enabled tooltips for all the components using the setToolTipText(). But, after enabling all of them, we disabled the tooltips using the following −ToolTipManager.sharedInstance().setEnabled(false);The following is an example to disable Tooltip for a component with an already enabled tooltip −Examplepackage my; import java.awt.GraphicsEnvironment; import java.awt.GridLayout; import java.awt.Point; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.SwingConstants; import javax.swing.ToolTipManager; public class SwingDemo {    public static void main(String[] args) throws Exception {       JFrame.setDefaultLookAndFeelDecorated(true);   ... Read More

How to select more than one row at a time in a JTable with Java?

Chandu yadav
Updated on 30-Jul-2019 22:30:26

1K+ Views

To select more than one row in a JTable, use the setRowSelectionInterval() method. Here, set the indexes as interval for one end as well as other end.For multiple rows in a range, set the range. Here, we are selecting rows from index 1 to index 2 i.e. two rows −table.setRowSelectionInterval(1, 2);The following is an example to select more than one row at a time in a JTable −Examplepackage my; import java.awt.Color; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.ListSelectionModel; import javax.swing.border.TitledBorder; public class SwingDemo {    public static void main(String[] args) {       JFrame ... Read More

How to select the first column in a JTable with Java?

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

484 Views

To select the first column in a JTable, use the setColumnSelectionInterval() method. Here, set the indexes as interval for one end as well as other end.For the first column, set the range as 0 and 0 since we want to only select the first column with index 0 −table.setColumnSelectionInterval(0, 0);The following is an example to select the first column in a JTable −Examplepackage my; import java.awt.Color; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.ListSelectionModel; import javax.swing.border.TitledBorder; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame();   ... Read More

How to make a canvas in Java Swing?

Ankith Reddy
Updated on 30-Jul-2019 22:30:26

3K+ Views

To make a canvas with Java Swing, use the Graphics2D class −public void paint(Graphics g) {    Graphics2D graphic2d = (Graphics2D) g;    graphic2d.setColor(Color.BLUE);    graphic2d.fillRect(100, 50, 60, 80); }Above, we have created a rectangle and also added color to it.The following is an example to make a canvas in Java −Examplepackage my; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JFrame; import javax.swing.JPanel; public class SwingDemo extends JPanel {    @Override    public void paint(Graphics g) {       Graphics2D graphic2d = (Graphics2D) g;       graphic2d.setColor(Color.BLUE);       graphic2d.fillRect(100, 50, 60, 80);    }    public ... Read More

Advertisements