Java Swing – Draw Station Names using GPS Coordinates on JPanel

I am working on an assignment and got stuck at a certain point. I am given a .txt file with a series of stations, and I am supposed to draw the station names using the GPS coordinates given to me, but I can’t translate those coordinates to XY coordinates that a JPanel uses. Also, GPS … Continue reading “Java Swing – Draw Station Names using GPS Coordinates on JPanel”

Resizing a frame when clicking a button

I’ve recently started a Java course to get familiar with programming. So sorry if this seems to be a stupid question. What i’m trying to do is resize the window when a button is clicked. Code says more then words: import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class GrowShrink extends JFrame implements ActionListener … Continue reading “Resizing a frame when clicking a button”

How to use addMouseListener on the elements of an array of JPanels without adding it to each single element.?

I am trying to make a chess program where I have an 8×8 array of JPanels which all require an addMouseListener but in this addMouseListener I need to make use of the index of that array for it to work, like this: panels[0][0].addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { panels[0][0].setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED)); } public void mouseReleased(MouseEvent … Continue reading “How to use addMouseListener on the elements of an array of JPanels without adding it to each single element.?”

Swing Timer and poor performance (Java, Swing)

I have a problem regarding a Swing Timer. I’m trying to create a “sliding” animation for a JPanel that comes sliding in from the side when pressing a button. This actually works fine, but when I want it to slide out again, I am hit by extremely poor performance. Note: the JPanel is in a … Continue reading “Swing Timer and poor performance (Java, Swing)”

Displaying JLabels on top of each other with Java and swing

I am writing GUI for klondike solitaire game in Swing for my school assignment. What I am struggling with is this. I can’t make cards appear on each other with an offset, like in this picture: I have already tried a few Layout managers, like GridBagLayout or OverlayLayout and I can|t get either of them … Continue reading “Displaying JLabels on top of each other with Java and swing”

Automated Drawing with logic inside of paintComponent() not working

I have spent the last couple days trying to learn about 2D graphics in java, and I’m starting to get the hang of it. I went through a tutorial, and I now understand how to draw lines (or other objects) to a JFrame. However, I’ve hit a mental roadblock. I want to create a grid … Continue reading “Automated Drawing with logic inside of paintComponent() not working”

How do I stop previous graphics from being redrawn in Java?

I’ve been working on a game but messed something up and I can’t figure what it is. I’ve simplified the code as much as possible in this example and it retains the same issue. In this example, instead of the red square going upwards without leaving a trace, it leaves behind a red trace meaning … Continue reading “How do I stop previous graphics from being redrawn in Java?”

Check if all JComboBoxes have something selected any indexes (Other than index 0)

I have this code sample. JComboBox[] set = {jComboBox1, jComboBox2, jComboBox3}; for (int i = 0; i < set.length; i++) { JComboBox boxes = set[i]; int index = boxes.getSelectedIndex(); if (index == 0) { System.out.println(“Not every JcomboBox has something selected”); continue; } else { System.out.println(“Every combobox has selected something”); } I know every time the … Continue reading “Check if all JComboBoxes have something selected any indexes (Other than index 0)”