Click here to Skip to main content
15,887,975 members
Everything / Desktop Programming / Swing

Swing

Swing

Great Reads

by Shubhashish_Mandal
Animated sliding panel in Swing.
by Santhosh Kumar Jayaraman
How to create a simple IMEI validator application using Java Swing
by csanuragjain
Captures, searches, and manages all your screenshots in one place.
by Osmund Francis
Creating templates in Eclipse to generate Java EventQueue statements

Latest Articles

by Michael B Pliam
A recursive descent math expression parser with built-in functions and a variable mapping
by Tung.Nguyen.2k
How to implement B-Tree's searching, insertion and deletion in Java
by AmosShi
Watch the Java class file visually & interactively for the meaning of every byte
by Ngo Tuong Dan
A usecase for Java beginner

All Articles

Sort by Updated

Swing 

15 Jan 2023 by Aura Blue
So, I have a program project in mind where the user enters some specifications, java stores the input as variable and by using an if else statement displays the result accordingly. The program is about finding your anime girl. I need some tips on...
15 Jan 2023 by Richard MacCutchan
See Java Swing How to - Add Background image to JPanel[^]. As to how to make the program more polished, that is really not a question that can be answered simply in a Quick Answers forum.
20 Dec 2022 by gamedevgod
i am maeing a game and i am puting all of the stuff on a JLayeredPane but it is not showing hear is my code: import java.awt.Color; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import...
13 Dec 2022 by pitwi
Hi! A view weeks ago I changed to Java and so far everything's fine - except drawing (with Swing). The code below works - except the part that draws a graphics. What's wrong? package Test2GUI; import java.time.format.*; import...
7 Dec 2022 by pitwi
Hi! A view weeks ago I changed from Visual Studio C# to Java and now I've tried my first project with Swing and WindowBuilder. So far so good and everything works fine - except drawing that line. There's no error message but also no line. Neither...
7 Dec 2022 by Richard MacCutchan
See Line2D Java Example - Computer Notes[^]
21 Nov 2022 by Marc Brolly
Each time I click on the JCombobox's arrow to show the list of exercises, it will continue to populate the combo box with duplicates entries shown in the image, made code is also inserted. I know its to know with the re-clicking of the arrow due...
21 Nov 2022 by Marc Brolly
Question was resolved by adding exerciseList.removeAllItems(); before the for loop public void popupMenuWillBecomeVisible(PopupMenuEvent e) { ArrayList nameList = new ArrayList(getComboValues()); ...
25 Oct 2022 by Rojaa Acharya
I am really new to code project and to java. Here you can see class A (LoginFrame ) which is inside package Automation : package Automation; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.lang.Exception; ...
24 Oct 2022 by FreedMalloc
Your code has 2 main methods, one in LoginFrame and one in FileSelector. A program should have only one main method. You need to rethink your logic. What I believe you might want is some sort of controller class that has the one and only main...
19 Oct 2022 by T1xT
Is there a way to set an image to entire jframe that also covers components as well (only that texts of components are on the image) ? layout is set on null and all components and jframe itself have same background colour. What I have tried: ...
18 Sep 2022 by T1xT
the painted line in the following code does not appear on Jframe, is there something wrong with paintComponent method import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Toolkit; ...
18 Sep 2022 by jimakoskx
public static void main(String[] args) { Gf points = new Gf(); JFrame f = new JFrame("Points"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setUndecorated(true); f.setResizable(false); f.setAlwaysOnTop( true ); ...
13 Sep 2022 by Ralph Daniel Honra
The problem always occurs when I tried to click and then delete a row in my GUI using the Delete JButton that I created. Then this is the exception I always get whenever I try to delete a row from my GUI. And as a result of the error, nothing...
13 Sep 2022 by Richard MacCutchan
java.lang.ArrayIndexOutOfBound...
15 May 2022 by daniel wakeley
So I'm making a festival scheduling app that takes user input from a Swing GUI and then schedules all the songs according to time and priority. I'm just wondering do I store the input in multiple Variables or an array. Also, after that how do...
31 Mar 2022 by ASH 2022
I created a program to drag & drop images, and I want to make the image black & white after right-clicking. What I have tried: What I did is to make the image black & white directly after dnd but what I want to do is to make the right-click...
25 Mar 2022 by Natann 022
Hello so i have a swing java program and i want to turn it into javafx. how can i achieve this? What I have tried: i try using Swingnode but can't work for some reason i can't import javafx.embed.swing.SwingNode into my project
13 Mar 2022 by VINEET MENDON
hii,everybody i am creating a gui where i need a timepicker i got Jspinner but the issue i want to convert the jspinner value into a string when i am doing it ,i am getting this exception ( java.lang.ClassCastException: class java.util.Date...
8 Feb 2022 by INNIT0
Even though I have set the inputPanel layout to GridLayout(4,1), when I run my program, the urlPanel,userPanel,passwordPanel and buttonPanel panels (all of which are contained in the inputPanel) are not displayed in the graphical user interface. ...
31 May 2021 by Farooq Ali 2021
I have been making this project without drag and drop in Java Swing and I have been encountered with an issue where the panel behind my shown panel in card layout do not disposes, i.e : my panel is transparent , so the panel behind it is also...
29 May 2021 by Rahul Negi 2021
i am learning awt and i am confused in java hierarchy. So since java.lang imported by default and it contains Object class. Object class is base class to all class. So when lang package is imported by default, will all the derived class of...
29 May 2021 by Richard MacCutchan
Why have you reposted this question? I gave you a link to the answer yesterday at Can you explain package hierachy in java.[^]. I suggest that before you try to learn awt you spend time learning Java and the inheritance rules, starting with...
29 May 2021 by OriginalGriff
No. Objects know about things they are derived from, all the way back to object - but no class should know about anything that is derived from it. So if ClassA derives from ClassB, and ClassC derives from ClassA, then: ClassC can use anything in...
29 May 2021 by Rahul Negi 2021
Object class present in java.lang.package. java.lang.package imported by default in java. Object class contains Event handling classes and awt. So why we need to import java.awt when we use awt in java...since it should be in lang.package which...
29 May 2021 by Richard MacCutchan
Quote: Object class contains Event handling classes and awt. No, object is the base of all classes and contains almost nothing. See java.awt Class Hierarchy (Java Platform SE 7 )[^].
6 May 2021 by Allan Nanks
i am working on a standalone core java project using graphical user interace. i have created buttons to show days of the week from sunday to saturday. now the problem is that i want to create buttons such that when the user clicks on a day, they...
28 Apr 2021 by Nandini Sathyan S
I am not able to insert data using setDate in java using prepared statement. Is there any way to resolve this problem? What I have tried: String query="insert into...
28 Apr 2021 by Richard MacCutchan
PreparedStatement.setDate (Java Platform SE 7 )[^]
26 Apr 2021 by Kevedvoc
String toPrintOnGUI = ""; while (resultSet.next()) { for (int i = 1; i
26 Apr 2021 by Richard MacCutchan
Your definitions are not valid. Try the following: JComboBox comboBoxSelection = new JComboBox(); comboBoxSelection.setModel(new DefaultComboBoxModel(new...
26 Apr 2021 by Nandini Sathyan S
I have been creating a gui which has a comboBox.I am not able to initialise the combo box items and I get syntax error on below pasted code. What I have tried: ScrollPane scrollPane = new JScrollPane(); comboBoxSelection = new JComboBox();...
18 Apr 2021 by Basmala Haggag
My problem is when I run this code it should normally pop up a window, but that's not the case. can anyone help me with that please. Thank you! What I have tried: import java.awt.FlowLayout; import java.awt.event.ActionEvent; import...
5 Apr 2021 by Basmala Haggag
I am making a game using java. At the beginning of my game there is a panel with a welcome message and then by pressing start you can go to the first level in the game. My question is how to switch panels(to go from the welcome message to level...
5 Apr 2021 by Gerry Schmitz
Create a "wizard". Creating Wizard Dialogs with Java Swing[^]
7 Feb 2021 by Mr.Corona
First open windowbuilder and then choose grouped layout.
4 Feb 2021 by Mr.Corona
I am new to eclipse and use windowbuilder. I saw in YouTube that we can auto resize Buttons by when we select then I arrow will come and when it is clicked a option will come to enable auto resize. But in my eclipse that icon is not coming. What...
13 Jan 2021 by Member 11296776
I have the following example code Public class A extends JFrame{ public A() { setSIze(500,500); } } Public class B extends A{ public B() { setSIze(200,200); } } If i instantiate B it will first call A So it will setSize(500,500) Then it will...
13 Jan 2021 by OriginalGriff
No. A class that extends a base class includes everything from the base as well as adding the new features - so the constructor of the base class is always automatically called before the constructor for the derived class, or the derived class...
13 Jan 2021 by Mr.Corona
I have made a small script for taking text from jtextfiled and save to a variable and print it in eclipse. But it is not getting print. Here is my code.- package not; import javax.swing.*; public class Not { public static void main(String...
13 Jan 2021 by Richard MacCutchan
See my suggested solution to your previous question at Java program ideas..[^]. There really is no substitute, or short cut, to actually learning the language and framework. The tutorials contain working samples that demonstrate, and explain, how...
12 Jan 2021 by Richard Deeming
There is literally nothing to print, because you haven't set any text, and you haven't given the user the opportunity to enter any text. Even if you moved the line that retrieves the text after the line that adds the text field to the frame, it...
3 Jan 2021 by Mr.Corona
I made this script with gui to open url. Here is the script - import javax.swing.*; import java.io.*; import java.awt.*; import java.awt.event.*; import java.awt.Desktop; import java.net.URI; import java.awt.Desktop; import java.net.URL;...
3 Jan 2021 by Richard MacCutchan
The problem is simply that you do not have a URL in your call to desktop.browse(oURL);. You try to capture the contents of turl at the time that you are creating the frame window, but there is no text there at that point. You should change your...
10 Dec 2020 by lelouch_vi 2
Hello I'm trying to use the JOptionPane here to send a warning to the user that he needs to enter a valid input in correct format, but the JOptionPane is not showing when I entered a wrong input. I don't know what is my mistake. boolean...
4 Apr 2020 by Gerry Schmitz
During each frame, move each circle, then loop through each circle, calling a method on the circle, called "IsInContact" and does whatever it needs to detect "contact". "Collision" implies an event, whereas "in contact" is a state, which is more...
4 Apr 2020 by Jeremy Maldonado
I am creating a program to simulate an outbreak. The program will have a number of circles bouncing around the screen. I have a class for the window and a class for the entities. I create instances of the class and add them to an ArrayList. I...
13 Mar 2020 by Member 13253016
hey im trying to print a billing receipt in netbeans. Billing receipt is getting table data from another table data.Every thing is working fine:) problem is that when i add rows more then the length of Jtable it makes a scroll bar and data under scrollbar bar is not printing. i want to print my...
13 Mar 2020 by Mitesh Awasthi
Take a button. add the following code : Syntax -> .print(); table.print(); Here, table - is my table name print() - inbuild method Thanks me later
5 Mar 2020 by fMuwouJjdjdoue
In the below program I am just trying to move the box on the screen based on the key pressed.I have to clear the last position of the box and draw its new position .When flag is true I will clear the screen and when the flag is false I will draw...
5 Mar 2020 by Darwin Ramos
I am currently designing my System. I added a cell renderer in order to create an alternate row color on my Jtable but when I insert cell renderer in my code, JTabel selection becomes cell unlike when before I insert the code where the selection...
5 Mar 2020 by OriginalGriff
Events don't happen immediately: they go into a "queue" and are answered in order (except paint - it's a low priority event, so anything more important will be moved up the queue past it). And the messages queue is only examined once processing...
15 Feb 2020 by jchacon28
I finally managed to get my GUI setup, and the listeners registered. I have a Student class, a StudentDemo class, and a StudentInfoWindow. In the StudentInfoWindow class i have a addButton, when i click it, it grabs the info from the text fields, creates a student object, adds it to an array...
5 Nov 2019 by m_smith
Hi guys, I would like to change the color of Jtable cell when it is clicked by mouse (like change it when click it once, change back when click it again). private void jTableMouseReleased(java.awt.event.MouseEvent evt) { int row =...
14 Jul 2019 by tihicaa
Hi guys, I'm very new to java and I've been dealing with this issue for more then 10 days. I have an app to create with two options, on time and count down, where user chooses desired option, and on time set new window opens and it blinks changing colors. Option two works just fine, but option...
14 Jul 2019 by Richard MacCutchan
Could be something in the area with all that messing about with strings and times has thrown an exception. But since you ignore any exceptions that you catch there is no way of knowing. Use the debugger to step through the code and see what is happening. You are also using the same text input...
7 Oct 2018 by User 13204940
Hi, I'm creating a game in Java and my JPanel calls repaint() on itself every time in a while loop. Some graphics are static, and some are animated, so I'm having trouble figuring out how to avoid rendering things every time which don't need re-rendering. Game loop: private int...
27 Aug 2018 by Astronomize
package graphq.gui; import java.awt.AlphaComposite; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Point; import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import...
19 Aug 2018 by FerdouZ
I already done a socket program where multiple client send data to server1 and server1 forward the data to server2. Now I want to do this program with Swing GUI. Can anyone help me how to create it? I submit my code below == What I have tried: Client import java.net.*; import java.io.*; ...
19 Aug 2018 by Richard MacCutchan
See Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)[^].
8 Apr 2018 by Member 13757360
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ //package myDesign; //import Util.Source; //import com.multicast.MulticastRx; //import com.multicast.MulticastsRx; import java.awt.Color; import java.awt.event.ActionEvent; import...
21 Feb 2018 by Member 13689569
I would like to add a Jtable into my ordering system. And the function if the page should be like this : Staff need to enter the details in text field to add a customer, when the staff successfully enter the data of the customer, the data will auto insert to table and into the text file. If the...
2 Feb 2018 by Member 13658198
I've tried so many time to where to put the '}' but error keeps coming out. So, where should I place the last '}' ? What I have tried: import java.text.*; import javax.swing.JLabel; import java.awt.BorderLayout; import java.awt.*; import java.awt.event.*; import java.awt.event.ActionEvent;...
2 Feb 2018 by CPallini
I guess you have to complete the actionPerformed method public void actionPerformed(ActionEvent e){ // do useful stuff here //.. } //
3 Jan 2018 by wseng
Use this jFirstForm.getContentPane().setBackground(Color.BLUE);
3 Jan 2018 by Chiranthaka Sampath
Hi guys, I have a small question. I have developed a JFrame Form from scratch using Java SWING package. The appearance is ok. I have not used a layout manager so that I have manually given the locations & the dimensions. It also connect to a MySQL database and there aren't any issues...
2 Jan 2018 by Member 13604495
package cubaawt1; import java.awt.*; public class L1F1044 { L1F1044() { Frame f = new Frame(""); f.setBackground(Color.pink); f.setSize(360,400); f.setLayout(new BorderLayout()); //menu bar declaration MenuBar menubar =...
2 Jan 2018 by CPallini
Did you try Google, didn't you? See, for instance: How Do I Convert My Programs to Swing?[^].
27 Dec 2017 by developer067
So i made a method in my java program, that I want to use to be able to draw a circle. I then want to add that circle to a panel in my code. What's the issue with the following? public void circle(Graphics g, int xLocation, int yLocation, int width, int height) { //Graphics g; ...
27 Dec 2017 by Richard MacCutchan
Graphics g = null; gamePanel.add(circle(g, 200, 100, 50, 50)); You are sending a null reference to the circle method, so there is no Graphics class to do the drawing. Also, the circle method does not return anything, so the gamePanel.add method has nothing to add.
6 Dec 2017 by Chiranthaka Sampath
I have created 3 classes. 1st class is used to store variable. other classes are use to input data and to display data. 1st Class public class Student { /** * @param args the command line arguments */ private String strStudNo; private String...
6 Dec 2017 by dolfijn3000
so i'm learning java and i was trying to make a button. the problem is that whenever i create a button it is as big as the screen. can someone fix this please ? Main.java: package com.softbite.library.TextBasedGame; import javax.swing.JFrame; public class Frame { // method to create a...
6 Dec 2017 by Richard MacCutchan
Your button is created without any constraining controls or size so it will fill the available space. No idea what the other two classes are for. Take a look at How to Use Buttons, Check Boxes, and Radio Buttons (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using...
15 Nov 2017 by CPallini
Your code is almost correct. Try class SequentialNumber { private static int currentNumber = 3000; public static String GetNextNumber() { currentNumber++; return "B"+currentNumber; } } public class program { public static void main(String args[]) { for (int n=0; n
15 Nov 2017 by Member 13522219
bill_number(using jtextfield) must auto increment by 1 after clicking submit button. I need sequtenial values in format like B3001,B3002, and so on....kindly plz help me What I have tried: public static class SequentialNumber { private static int currentNumber=3000; public...
20 Oct 2017 by Member 13476442
I am new to JAVA and i am trying to add print-preview functionality in my frame, i found a print preview class. Now i want to use it in my frame, I have created an object to PrintPreview class it is asking for (Frame,canvas,page). In frame i pass this for my current frame,and in page "A4" but I...
23 Jul 2017 by NumanSE
I am developing a Java Swing Application. The application ends with a form to take the customer's input information (name, last name, email, gender, etc) I am trying to validate the text fields in a way that when the application is launched focus will go on the first text box (first name). I...
23 Jul 2017 by Richard MacCutchan
How to Write a Focus Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)[^]
6 Jul 2017 by Bilal Zafar
Hello all, I am very new to java, so please ignore if you think this question is stupid. A task has been assigned to me in which I have to extract data from h2 database table with **more than 1 crore rows (SAD) from one table.** And finally show that data into a j table with pagination. What i...
6 Jul 2017 by KarstenK
Open a database and fetch data is expensive, so normally is a bunch of data fetched, stored in the data model and than displayed. I would fetch data set for at least two pages depending on the speed. A somehow similar example by fetching all data and storing it into the plain model array ("data").
24 Jun 2017 by Member 13265840
I am new to Jtree and Java. So, I have a tree structure like this : -Abcd --Efghi ---Pqrst ---Uvwxyz ---Xyza ---Hdwik ---Lmnop ---Bcdef --Tqrsp ---Jumak ----Uoaha ----Lobte -----Cshnt ----Karke Now i want to get the count of Abcd = 14 (i.e Count of all children...
23 Jun 2017 by Patrice T
ppolymorphe wrote: I wouldn't use a tree for this problem, it just complicate things. Quote: Then what should i do, can you help me That is a matter of "problem analyze" : you can build a tree, but do you need to ? As you have already found, you need to read lines 1 by 1 while...
3 Jun 2017 by Mat3
How to populate jList by the keys from Map collection? I have added jListCars to a jScrollPanel and now I want to populate jListCars by key Strings from Map. So far I have used demoList that is added to jListCars. However I will need often to add new vehicles and I dont want each time to be...
29 May 2017 by Michael B Pliam
A recursive descent math expression parser with built-in functions and a variable mapping
26 May 2017 by Maciej Los
I'd suggest to read this: Java JButton - javatpoint[^] import java.awt.event.*; import javax.swing.*; public class ButtonExample { public static void main(String[] args) { JFrame f=new JFrame("Button Example"); final JTextField tf=new JTextField(); ...
26 May 2017 by Member 13225387
//Main Class: import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Order { public static void main(String[] args) { OrderFrame frame = new OrderFrame(); } } //Frame Class: import java.awt.*; import java.awt.event.*; import javax.swing.*; public class OrderFrame...
22 May 2017 by Jepoyoyoyo
import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Midterms extends JFrame implements ActionListener { JMenuBar b = new JMenuBar(); JMenu file = new JMenu("File"); JMenu color = new JMenu("Color"); JMenu font = new JMenu("Size"); JMenuItem neww =...
11 May 2017 by Member 11639357
Greetings, I tried asking this question on stack overflow, but lately that website has been getting very snippy (but worst of all, unhelpful). So I am going to try other places (like here). Here is the question - java - Set Size on JScrollPane with multiple panels resizes panels on window...
11 May 2017 by Richard MacCutchan
Since you did not post the details of your problem I can only suggest you take a look at Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)[^].
27 Apr 2017 by wseng
Scanner sc = new Scanner(System.in); This line is used for scanner to read an input from the console. How would you expect it to read from the JTextField ? To retrieve value from JTextField, it should be something like int a = t1.getText(); If you want to increase the width of the...
19 Apr 2017 by scoobyyyy
i want to display the records in jtable from mysql my table contains four rows. Username varchar, Login timestamp, Logout timestamp, Status char. i am trying to display username,login time,logout time and status of the username from the table.need a solution thanks in advance What I have...
12 Apr 2017 by Member 13124828
My problem is whene i put the two number in the TextField , after clicking on the "Result" button it won't give me the total of two numbers ! and i m not sure about the Scanner that i add into the event, it s just my opinion about reading numbers in the JFrame . here is my Code : ...
2 Apr 2017 by User1454
Hi, I need a label, a textfield and a button in 2 rows. I used flow layout for that. But after adding all components to the Jpanel which inherits JFrame, only 2nd row is displayed. Please help where am going wrong and I am very new to Java Swing. Thanks in advance What I have tried: //This...
10 Jan 2017 by Member 10850253
for (int i = 0; i
28 Dec 2016 by GaneshRfromSpace
This is my xml file which contains Tamil language scripts. வீடு I am using DOM parser to parse the xml file. ...
7 Dec 2016 by Member 12701313- questforimprovement
I have a project design that uses Java Swing for the GUI part of the application and it consists of multiple button classes that corresponds to each button on the main GUI. Is it useful to have so many button classes? What are the advantages of this approach? Are there alternative ways I can...
7 Dec 2016 by Richard MacCutchan
If you have too many buttons on the display then the chances are that the design is going to confuse your users. Why do you need multiple button classes, why not one class that contains all the logic for a button?
2 Dec 2016 by Tung.Nguyen.2k
How to implement B-Tree's searching, insertion and deletion in Java
19 Oct 2016 by Member 12804008
I'm trying to create a class which reads an ambulances.csv and patients.csv file, run for a set period and then output the updated information to ambulances-2.csv and patients-2.csv (both input and output files use the same data formats). Each patient has an (x, y) location. The initial...