Questions tagged [swing]

23337 questions
1

votes
1

answer
89

Views

Wrong colors in JLabel when using HTML string

I'm trying to use HTML styling in a JLabel and convert the BufferedImage to jpg. However the colors are quite different than what I expect. Here is the code: val html = "" + "teststring1" + key + " teststring" val textLabe = new JLabel(html) textLabe.setSize(textLabe.getPreferredSize) val d: Dimensi...
burak emre
1

votes
3

answer
5k

Views

Why methods mouseExited and mouseEntered run together when I enter or exit JDialog?

Yes, this question is already posted on JavaRanch but I do not understand them well. I have JDialog which has MouseListener added as follows: super.addMouseListener(new MouseAdapter() { @Override public void mouseExited(MouseEvent e) { if (e.getSource() instanceof ConnectionTreeTooltip) { System.out...
Volodymyr Levytskyi
1

votes
1

answer
1.7k

Views

Resizing a JFrame and maintain aspect ratio?

I want the user to resize the window while maintaining the same aspect ratio (rectangle in which the width > height. In other words, if height is changed, I want to force width to change by a larger amount, and vice versa. Preferably, I want the resize to happen while the window is being resized, n...
Andrew Kor
1

votes
1

answer
992

Views

How does JButton control it's size?

I have added a button to a frame and controlling it's text and/or minimum size with timer. I noticed, that button does grow sometimes, and sometimes does not. If in null layout, then it ignores both text and minimum width changing. If in FlowLayout layout, then it grows if text changing, but ignores...
Suzan Cioc
1

votes
2

answer
17.5k

Views

<No main classes found>

Followed an oracle tutorial on creating a GUI, the tutorial promised that upon running the application I will have to select the main class, however, Netbeans doesn't find any Main classes? Here is my code: public class GUI extends javax.swing.JFrame { /** * Creates new form GUI */ public GUI() { in...
user2403162
1

votes
2

answer
7.5k

Views

using setTitle on a method

I need to set the title from a method (not constructor). I tried doing it like this, but it's not working: import javax.swing.*; import java.awt.*; public class PointGraphWriter extends JPanel { public String title; public void setTitle(String name) { title = name; } public PointGraphWriter() { JFra...
Drilon Blakqori
1

votes
5

answer
2k

Views

how to make my JTable refresh after adding a new row

After I add a new row to a JTable. The information wrote goes to the txt file, but my JTable doesn't shows the last raw. But if I close the program, and run it again, the information it's in the table. So, is there a way to refresh the data in the JTable without closing the application and running i...
user3307020
1

votes
1

answer
461

Views

Adding JLabel and Displaying it With a For-Loop

I am creating a hangman game using a java gui. I was wondering how to add a JLabel to an array list and to a JPanel. The problem is that the JLabels have to be added according to the length of the word. I don't know how to add a JLabel if it doesn't have a predefined name. The code is below: for(int...
user2280906
1

votes
3

answer
2.2k

Views

How to auto click OK on JOptionPane when testing?

In my program I am testing a button which when clicked throws up a JOptionPane window in the application. However when this window pops up it expects the user to click OK which means that the test can't continue unless the window is manually closed down. I want to be able to close this window prog...
user2989759
1

votes
1

answer
677

Views

Java Canvas Won't Paint

I want my program to display the canvas that is repainted once at the start and then whenever a change is made afterwards. I thought I had everything coded correctly, but for some reason nothing that is painted onto the canvas actually shows (I know it's repainting, I tested that). Here are the code...
StrongJoshua
1

votes
1

answer
204

Views

Image not showing on JLabel

I've been tasked to make a java replica of Candy Crush Saga. Right now im quite stuck on the GUI part. I've decided that each candy will be represented by a JLabel holding the candy icon, and a mouselistener to control the functionality. What happens is, after i finish running the screen shows, the...
vito
1

votes
1

answer
3k

Views

JList - getting value from Click

Is there any way to use ListSelectionListener or MouseAdapter to get information about selected value (if value is a String for example), is there any built-in method for that? I only know how to get proper indexes but not the content or content.toString() I'm adding element like this: { DefaultList...
1

votes
1

answer
854

Views

Making a Swing Logo Quiz

So first off, I'm a novice programmer, so I don't have the best knowledge of code and I don't really know how to use stackoverflow very well either. import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; import java.io.*; public class Logoquiz extends JPanel { private...
reivermello
1

votes
2

answer
80

Views

Java Swing edit user popup

I'm trying to create a window to edit user info in a database but I can't seem to figure out a proper way to do the popup window. I want it to show the current info of the user, save the info to database on save button click and close the window when cancel is clicked - and I've done this using a ne...
Branden Patey
1

votes
2

answer
104

Views

NullPointerException error and cardlayout [duplicate]

This question already has an answer here: What is a NullPointerException, and how do I fix it? 12 answers The NullPointerException only comes about when I click the 'next' button. I'm trying to get the frame to transition to another panel using cardlayout, but I have failed to do so. Second quest...
hahaashton
1

votes
2

answer
1.1k

Views

“AWT-EventQueue-0” NullpointerException inconsistent error message

I'm getting this error message when im starting my Java application: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at engine.Display.GamePanel.paintComponent(GamePanel.java:102) at javax.swing.JComponent.paint(Unknown Source) at javax.swing.JComponent.paintChildren(Unknown So...
Rakso
1

votes
1

answer
514

Views

Java Math.random not random in while loop [duplicate]

This question already has an answer here: math.random, only generating a 0? 5 answers I am running the following code to get random (x, y) coordinate pairs. However, I only get the same pair over and over again. int counter=0; while(counter
BeastX-Men
1

votes
4

answer
1.7k

Views

Terminate a while loop when clicking JFrame.EXIT_ON_CLOSE

Currently I have a live active while loop running and it does what I want my only problem is closing the frame (exit the app) while the while loop is still active. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); I tried using the if statement to terminate the loop if (frame.getDefaultCloseOper...
gcclinux
1

votes
1

answer
118

Views

Java ComboBox returns -1/null

I've used a ComboBox in my program (Example is below) which has various choices. If I want to read out the ComboBox it returns -1 for the Index and "null" for the item. Did somebody else had that problem and could help me? Thank in advance! import java.awt.*; import java.awt.event.*; import javax.sw...
user3633026
1

votes
1

answer
40

Views

My JPanel program not working successfully

This program when run shows a oval at location 70,70 and there is a start button. After i click the start button the program stops for some time and the oval moves one position southeast . It actually should have rolled down towards the other corner. This is the program.... package javaapplication1....
1

votes
2

answer
119

Views

JButton variable not found

I am trying to change the background color on click of a radio button, but the second radio button, "bt2" variable is not found by the compiler. I keep getting this error message: "cannot find symbol bt1" in the e.getSource()==bt1 Here is my code: public class ColorChooser extends JFrame implements...
Akshat Malviya
1

votes
2

answer
90

Views

Creating a window with Java Swing

So, I'm new to to the Java language, and I have come to study about the JFrame class. In a lot of tutorials, I see this way of creating a new JFrame window: class Something extends JFrame{ public static void main(String[] args) { new Something(); } Something() { ... //Code here, the elements of t...
bvitya
-1

votes
1

answer
36

Views

Is the below a Java Multiple Inheritance Design Pattern if yes which one?

I have been working in a Swing application for more than 8 years but it was designed perhaps 20 years ago, the designers created their own custom made components (I think initially using AWT but it was updated to Swing at some stage). There is a widget used by multiple screens that presents data in...
alejob2k
1

votes
2

answer
734

Views

How to use a key listener to verify input to a text field

so I am trying to verify what is typed into my text field is only numeric and not alphanumeric. I've looked into formatted textboxes, but haven't been able to implement them, due to lack of understanding the tutorial oracle has up on their site. I did some more searching and found the key listener...
Jesse Reinhold
1

votes
2

answer
250

Views

Comparing the text of different JButtons

I am currently coding a game and part of it consist of having different tiles to be put in a board. I plan on simulating this by having different buttons that will be used to represent the tiles with their corresponding coordinates. For example, one button will say "A1", "A2", etc. What I would like...
ZeldaX
0

votes
1

answer
12

Views

Display Jlabel based on JtextArea dynamically without button click

I have been wondering if it is possible to update JFrame or JDialogs based on the inputs inside jtextarea without button click. For an example, after i input some text in textarea. it should automatically update jlabel without the need of a button. I have search troughout but all the information i f...
FaridAvesko
0

votes
0

answer
3

Views

removing an another jcomponent when combobox item changed

I want to remove labell10 and combobox-level when i change from programing book item in combobox category.I already done similar coding and they are working. but in this case form remain lower parts of l10 and level.i just want to display jtextfield agegroup and change text of l8 to age group when i...
udara prabath
-1

votes
0

answer
14

Views

java swing how get 12 month date auto when installmen generated?

i have work on loan installment calculator in java swing. how i can get 6 month dates if i make 6 month installment. i want this Scainor in java swing how i get 6,8, 10 month date in loop in java swing? image.jpg for (int i = 0; i < date.length; i++) { // cycle #1: over all initialized dates for (in...
Naveed Islam
0

votes
0

answer
9

Views

GUI using Swing converting to Runnable Jar not working

I'm using a mac and I just made a simple program which I want to make it into a runnable jar so that I can send it to my friend. I tried exporting my project into a runnable jar file but I'm having trouble with launching it. I get a error message that says "The Java JAR file could not be launched. C...
ChatSev
1

votes
3

answer
7.4k

Views

JTextPane does not go to a new line?

I'm displaying RSS feed in my JTextPanel. Result displayed doesn't go to a new line. How can I insert \n in JTextPane? Thanks! writeNews class: public String writeNews() { String result = ""; try { DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();...
Lily S
1

votes
0

answer
8

Views

increasing height of jtable dynamically upon insertion of rows

i am having problem on adjusting height of jtable whenever i insert rows. i have tried using setsize() and setPreferredScrollableViewportsize() for both table and scrollpane of the table. could it be the problem in layout manager? i also tried increasing the size of jpanel too upon inserting each ro...
Bhuwan pandey
-1

votes
0

answer
13

Views

How to Make chips in swing [on hold]

I need to make chips component like the ones in android but for java's swing it's very useful for my input identaion,just like the searching here in this site with tags, but am realy lost on how to make one , please if someone can tell me even just an idea about how to will be enough ,i'v googled it...
a lovely person
1

votes
1

answer
747

Views

Implementing MouseMotionListener in JPanel

Continuing on from this question, I'm implementing a MouseMotionListener in my JPanel so that I can track mouse events to pass down to the objects contained within. This didn't work, so I implemented a completely clean JPanel (that has none of the other stuff my game panel has) with a MouseMotionLis...
Gorbles
1

votes
1

answer
240

Views

What is the correct way to use JPanels in Netbeans?

I'm new to Java GUI. First I created a JFrame and have added a JPanel. After that I set it as a Null Layout. Then added a label and set a background image for the frame. For my project I need to add separate 4 JPanels for this JFrame. On those JPanels I'm going to add Labels and Text boxes. I want...
coderaizer
1

votes
1

answer
253

Views

Change contents of BufferedImage, then update JFrame to reflect it

I am trying to make a Mandelbrot Set renderer with a GUI where you can click and drag to zoom into a specific area. When run, it will do the initial calculations and rendering fine, but when you try to click and drag to zoom in, the console says it is doing the calculations, but the content of the J...
mjones.udri
1

votes
1

answer
59

Views

KeyTyped events - Why can I enter a character but when I delete I cannot enter anymore?

I use this code to restrict the input only for numbers, but if the first key I press is a letter, the code let me enter that letter, only one time, then when I erase it I cannot input anymore letters, what is wrong with the code? I want to imput only numbers. amount.addKeyListener(new KeyAdapter() {...
Lazaro
1

votes
2

answer
142

Views

How to add semi-trasparent JPanel over another JComponent

I'm trying to add a feature to an app. My goal is to cover a JComponent of the JFrame with a semi trasparent JPanel (it will contains a JXBusyLabel in the middle). Let's say we have something like this: and on click we'd like to have the following JPanel, but semitrasparent (which means it should a...
Koop4
1

votes
1

answer
366

Views

JTable:How to initialize a empty cell(type is int/float…) to allow user to input number in JTable?

I only know that there's way to initialize a empty String cell like this: Object[][] tableData={ {""},{""} }; String[] columnTitle = {"aaa", "bbb"}; JTable jTable= new JTable(tableData, columnTitle);
guo
1

votes
1

answer
125

Views

Getting components of a jpanel while on runtime

How to get components of a jpanel while on runtime in Java. I've been trying to get the components in order to perform a method with them as parameters but I realized that for some reason I can't seem to get the components while on runtime. If you need more specifics let me know, I just didn't want...
YOPI
1

votes
0

answer
19

Views

Why does my code just show a slider an my JFrame? (Swing)

Have a JPanel, called contentArea. In it, there is a JSlider and a Compontent to be drawn. But when I run it, I just get the slider. No Component. Why? My main: JFrame mainFrame = new JFrame(); JPanel content = new JPanel(); content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS)); Component p...
Chris

View additional questions