Found 4338 Articles for Java 8

How can I set a table in a JPanel in Java?

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

893 Views

Let us first set rows and columns for our table −String[][] rec = {    { "1", "Steve", "AUS" },    { "2", "Virat", "IND" },    { "3", "Kane", "NZ" },    { "4", "David", "AUS" },    { "5", "Ben", "ENG" },    { "6", "Eion", "ENG" }, }; String[] header = { "Rank", "Player", "Country" };Now, set the above in a table as rows and columns −JTable table = new JTable(rec, header);Add the table in the panel −JPanel panel = new JPanel(); panel.add(new JScrollPane(table));The following is an example to create a table in a panel −Examplepackage my; ... Read More

What are the Selection Modes in a JTable with Java?

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

559 Views

Selection modes sets the table's selection mode to allow only single selections, a single contiguous interval, or multiple intervals. Let us see the selection modes one by one −Single Selection modeThe following is an example of Single Selection mode for a JTable. It allows you to select one cell at a time −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();       JPanel panel = new JPanel();       ... Read More

How to add ScrollBar to JList in Java?

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

3K+ Views

Let us first set a JList and add items −List myList = new ArrayList(10); for (int index = 0; index < 20; index++) {    myList.add("List Item " + index); }Now, we will set the above list to a new list −final JList list = new JList(myList.toArray(new String[myList.size()]));Now, set the ScrollBar to JList −JScrollPane scrollPane = new JScrollPane(); scrollPane.setViewportView(list); list.setLayoutOrientation(JList.VERTICAL);The following is an example to add ScrollBar to JList −Examplepackage my; import java.awt.BorderLayout; import java.util.ArrayList; import java.util.List; import javax.swing.JFrame; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.JScrollPane; public class SwingDemo {    public static void main(String[] args) {       JPanel ... Read More

How to create a Default Cell Editor with textbox in Java?

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

175 Views

Create a JTextBox first −JTextField textField = new JTextField();Set the above JTextFile for the Default Cell Editor −TreeCellEditor editor = new DefaultCellEditor(textField); tree.setEditable(true); tree.setCellEditor(editor);The following is an example to create a default cell editor with textbox −Examplepackage my; import javax.swing.DefaultCellEditor; import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.TreeCellEditor; public class SwingDemo {    public static void main(String[] args) throws Exception {       JFrame frame = new JFrame("Demo");       DefaultMutableTreeNode node = new DefaultMutableTreeNode("Products");       DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("Clothing");       DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("Electronics");       DefaultMutableTreeNode node3 ... Read More

How to make a JTree editable in Java?

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

412 Views

To make JTree editable in Java, use the TreeCellEditor class. With that, set the setEditable() method to true −TreeCellEditor editor = new DefaultCellEditor(textField); tree.setEditable(true); tree.setCellEditor(editor);The above will make the tree editable. The following is an example to make a JTree editable in Java −Examplepackage my; import javax.swing.DefaultCellEditor; import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.TreeCellEditor; public class SwingDemo {    public static void main(String[] args) throws Exception {       JFrame frame = new JFrame("Demo");       DefaultMutableTreeNode node = new DefaultMutableTreeNode("Products");       DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("Clothing");       DefaultMutableTreeNode node2 = ... Read More

How to create a Number Spinner in Java?

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

328 Views

At first, create a SpinnerModel −SpinnerModel value = new SpinnerNumberModel(50, 0, 75, 1);Now, set the values −JSpinner spinner = new JSpinner(value);The following is an example to create a number spinner −Examplepackage my; import javax.swing.*; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame("Spinner Demo");       SpinnerModel value = new SpinnerNumberModel(50, 0, 75, 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 −

Java Program to set the Font and Color of some text in a JTextPane using Styles

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

3K+ Views

Let’s say the following is our JTextPane −JTextPane textPane = new JTextPane();Now, set the font style for some of the text −SimpleAttributeSet attributeSet = new SimpleAttributeSet(); StyleConstants.setItalic(attributeSet, true); textPane.setCharacterAttributes(attributeSet, true); textPane.setText("Learn with Text and ");For rest of the text, set different color −StyledDocument doc = textPane.getStyledDocument(); Style style = textPane.addStyle("", null); StyleConstants.setForeground(style, Color.orange); StyleConstants.setBackground(style, Color.black); doc.insertString(doc.getLength(), "Video Tutorials ", style);The following is an example to set font and text color in a JTextPane with Styles −Examplepackage 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 ... Read More

Java Program to get the next sibling in a JTree

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

243 Views

Use the getNextSibling() method to get the next sibling. Here, we are getting the next sibling of child node “five” and displaying on Console −System.out.println("Get Next Sibling = "+five.getNextSibling());The following is an example to get the next sibling in a JTree −Examplepackage my; import javax.swing.JFrame; import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; public class SwingDemo {    public static void main(String[] args) throws Exception {       JFrame frame = new JFrame("Demo");       DefaultMutableTreeNode node = new DefaultMutableTreeNode("Products");       DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("Clothing (Product1 - P66778)");       DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("Accessories (Product2 - P66779)"); ... Read More

How to set a value in a particular JTable cell with Java?

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

1K+ Views

Let’s say initially our table is the following with a specific cell [2, 1] with value “Kane” −The following is an example to set a new value to the above table. Here, we will update the cell [2, 1] −table.setValueAt("Guptill", 2, 1);Let us see the complete example −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();       JPanel panel = new JPanel();       panel.setBorder(BorderFactory.createTitledBorder(       BorderFactory.createEtchedBorder(), ... Read More

How to limit the values in a Number JSpinner Component with Java?

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

1K+ Views

To limit the values in a number JSpinner component, use the SpinnerNumberModel that allows to set the min, max, step size and even the initial value −value − current value of the model min − first number in the sequence max − last number in the sequence stepSize − difference between elements of the sequenceLet us set the above values −int min = 0; int max = 10; int step = 1; int i = 1; SpinnerModel value = new SpinnerNumberModel(i, min, max, step);Now, we will set these values to our JSpinner −JSpinner spinner = new JSpinner(value);The following is an ... Read More

Advertisements