Found 131 Articles for Swing

How to add JTable to Panel in Java Swing?

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

3K+ Views

To add JTabel to Panel, let us first crerate a panel −JPanel panel = new JPanel();Now, create JTable and add rows and columns with the records −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" }; JTable table = new JTable(rec, header);Add the above created table to panel −panel.add(new JScrollPane(table));The following is an example to add JTabel to Panel in Java ... Read More

Can we change the Cursor with Java Swing?

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

2K+ Views

Yes, we can change the default cursor representation in Java. Let us first create a button component −JButton button = new JButton("Button with two borders");Whenever user will keep the mouse cursor on the above button component, the cursor would change to hand cursor −Cursor cursor = button.getCursor(); button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));The following is an example to change the cursor −Examplepackage my; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Cursor; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.border.Border; import javax.swing.border.EtchedBorder; import javax.swing.border.LineBorder; import javax.swing.border.TitledBorder; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Demo");   ... Read More

How to get the number of rows and columns of a JTable in Java Swing

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

2K+ Views

To count the number of rows of a table, use the getRowCount() method −table.getRowCount()To count the number of columns of a table, use the getColumnCount() method −table.getColumnCount()The following is an example to get the number of rows and columns of a JTable −Examplepackage my; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; public class SwingDemo {    public static void main(String[] argv) throws Exception {       DefaultTableModel tableModel = new DefaultTableModel();       JTable table = new JTable(tableModel);       tableModel.addColumn("Language/ Technology");       tableModel.addColumn("Text Tutorial");       tableModel.addColumn("Video Tutorial");     ... Read More

How to highlight a row in a table with Java Swing?

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

1K+ Views

To highlight a row in a table, you can use the addRowSelectionInterval() method. At first create a table −DefaultTableModel tableModel = new DefaultTableModel(); JTable table = new JTable(tableModel);Add some columns −tableModel.addColumn("Language/ Technology"); tableModel.addColumn("Text Tutorial"); tableModel.addColumn("Video Tutorial"); tableModel.addColumn("Views");Now, add rows to the table −tableModel.addRow(new Object[] { "NodeJS", "No", "Yes", "2350"}); tableModel.addRow(new Object[] { "MVC", "Yes", "No", "1500"}); tableModel.addRow(new Object[] { "ASP.NET", "Yes", "Yes", "3400"}); tableModel.addRow(new Object[] { "F#", "Yes", "No", "7890"}); tableModel.addRow(new Object[] { "Blockchain", "Yes", "No", "10600"}); tableModel.addRow(new Object[] { "SharePoint", "Yes", "Yes", "4900"});Highlight a single row by adding interval of rows. Set the same index for both the parameters ... Read More

Java Program to append a row to a JTable in Java Swing

Alshifa Hasnain
Updated on 17-Jan-2025 19:44:35

1K+ Views

Adding rows dynamically to a JTable is a common task in Java Swing when working with user interfaces that involve tabular data. This article walks you through creating a Java Swing application that appends a new row to a JTable when the user inputs data and clicks a button. What is JTable? A JTable is a powerful Swing component for displaying and editing tabular data. By using a DefaultTableModel, we can manage the table's data dynamically, including adding or removing rows. This makes it an excellent choice for applications requiring user interaction. Approach Following are the steps to append a row ... Read More

How to add a title to JTable in Java Swing?

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

2K+ Views

To display a title to the JTable, you can set a title for the JPanel, which already consists of a JTable.Here, we are using createTitledBorder() for the JPanel to set the title for the panel border that would eventually work for table title.Let’s say the following is the JPanel −JPanel panel = new JPanel();Now, use setBorder() and the BorderFactory class to set a title border for the panel that would be our table title as well −panel.setBorder(BorderFactory.createTitledBorder(    BorderFactory.createEtchedBorder(), "My Demo Table", TitledBorder.LEFT, TitledBorder.TOP));The following is an example to add a title to a JTable −Examplepackage my; import javax.swing.BorderFactory; import ... Read More

How to highlight multiple rows in a sequence in a table with Java Swing?

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

267 Views

To highlight multiple rows in a table, you can use the addRowSelectionInterval() method. At first create a table −DefaultTableModel tableModel = new DefaultTableModel(); JTable table = new JTable(tableModel);Add some columns −tableModel.addColumn("Language/ Technology"); tableModel.addColumn("Text Tutorial"); tableModel.addColumn("Video Tutorial"); tableModel.addColumn("Views");Now, add rows to the table −tableModel.addRow(new Object[] { "NodeJS", "No", "Yes", "2350"}); tableModel.addRow(new Object[] { "MVC", "Yes", "No", "1500"}); tableModel.addRow(new Object[] { "ASP.NET", "Yes", "Yes", "3400"}); tableModel.addRow(new Object[] { "F#", "Yes", "No", "7890"}); tableModel.addRow(new Object[] { "Blockchain", "Yes", "No", "10600"}); tableModel.addRow(new Object[] { "SharePoint", "Yes", "Yes", "4900"});Highlight multiple rows by adding interval of rows from both ends. Set interval (index) for both the ... Read More

Create gradient translucent windows in Java Swing

karthikeya Boyini
Updated on 19-Jun-2020 12:36:39

3K+ Views

With JDK 7, we can create a gradient based translucent window using swing very easily. Following are the steps needed to make a gradient-based translucent window. Make the background of JFrame transparent first.frame.setBackground(new Color(0, 0, 0, 0)); Create a gradient paint, and fill the panel.JPanel panel = new javax.swing.JPanel() {    protected void paintComponent(Graphics g) {       Paint p = new GradientPaint(0.0f, 0.0f, new Color(R, G, B, 0),        getWidth(), getHeight(), new Color(R, G, B, 255), true);       Graphics2D g2d = (Graphics2D)g;       g2d.setPaint(p);       g2d.fillRect(0, 0, getWidth(), getHeight());    } } Assign ... Read More

Create shaped windows in Java Swing

Samual Sam
Updated on 19-Jun-2020 11:58:25

964 Views

With JDK 7, we can create a shaped window using swing very easily. Following are the steps needed to make a shaped window. Add a component listener to frame and override the componentResized() to change the shape of the frame. This method recalculates the shape of the frame correctly whenever the window size is changed.frame.addComponentListener(new ComponentAdapter() {    @Override    public void componentResized(ComponentEvent e) {       frame.setShape(new  RoundRectangle2D.Double(0, 0, frame.getWidth(),       frame.getHeight(), 20, 20));    } });ExampleSee the example below of a shaped window.import java.awt.Color; import java.awt.GridBagLayout; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import java.awt.geom.Ellipse2D; import java.awt.geom.RoundRectangle2D; import ... Read More

Create a simple calculator using Java Swing

karthikeya Boyini
Updated on 19-Jun-2020 12:01:39

24K+ Views

Swing API is a set of extensible GUI Components to ease the developer's life to create JAVA based Front End/GUI Applications. It is built on top of AWT API and acts as a replacement of AWT API since it has almost every control corresponding to AWT controls.Following example showcases a simple calculator application.import java.awt.BorderLayout; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; public class Calculator implements ActionListener {    private static JTextField inputBox;    Calculator(){}    public static void main(String[] args) {       createWindow();    } ... Read More

Advertisements