awt

Clearing contents of TextField using setText does not work in AWT

旧城冷巷雨未停 提交于 2019-11-29 15:21:58
I am having problems clearing contents of TextField in AWT using setText() method. Apparently, setText("") does not clear the contents of the TextField on pressing the 'Reset' button. Here's my program: import java.awt.*; import java.awt.event.*; public class form extends Frame { Label lbl = new Label("Name:"); TextField tf = new TextField(); Button btn = new Button("Reset"); public form() { tf.setColumns(20); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); btn.addActionListener(new ActionListener() { public void actionPerformed

Translucent JPopupMenu inside a Translucent Window - alternative?

Deadly 提交于 2019-11-29 14:53:43
I'm not sure if this is possible, but is there a way to safely allow popups to be translucent even when the parent container is also translucent? If not, what would be wise alternative to use or extend instead of JPopupMenu ? Note: Translucent refers to a component not 'having a background', similar to the effect of setOpaque(false); . Thanks. From a forum answer by user camickr in 2009: I don't know if transparency painting has changed in 1.6.0_10. Prior to that I believe transparency can only be achieved in lightweight components (ie. Swing does all the painting). JFrame, JWindow and JDialog

Java - control Z order of JPanels

寵の児 提交于 2019-11-29 13:45:49
In short, my need is to have a background Image in my java app, and upon some event, create some other graphics on top of that image. I was thinking I would use a JPanel to draw the background image in, add it at to my JFrame the start of program, and then add other JPanels on top of that upon certain events. The problem is Swing gives the JPanels added first the highest Z index, so what should be my background is showing up on top of everything. Is there any way to control the Z index/order of the JPanels, or am I going about this completely wrong? Colin Hebert You can use the

Java: Getting resolutions of one/all available monitors (instead of the whole desktop)?

谁说胖子不能爱 提交于 2019-11-29 13:24:19
I have two different-sized monitors, connected together using (I believe) TwinView. I tried System.out.println(Toolkit.getDefaultToolkit().getScreenSize()); and get java.awt.Dimension[width=2960,height=1050] which is true if you count both monitors together . Instead of this, I would like to be able achieving one of the following: getting resolution of the current monitor getting resolution of the main monitor z - you'll want to use the GraphicsEnvironment . In particular, getScreenDevices() returns an array of GraphicsDevice objects from which you can read the width/height of the display mode

Using Java's Robot to hold a key down

十年热恋 提交于 2019-11-29 12:27:40
Currently i'm trying to have java hold down a key like follows: Robot rob; rob.keyPress(KeyEvent.VK_ENTER); Thread.sleep(3000); rob.keyRelease(KeyEvent.VK_ENTER); This should hold enter down for 3 seconds, causing the repeating effect after a second or so. In other words, if you were to manually hold the "r" key, it would first type r, and then after about a second it would go like rrrrrrrr. I want this effect from the robot. I also tried: curTime = System.currentTimeMillis(); while(System.currentTimeMillis() - curTime < duration) { rob.keyPress(whatever); } rob.keyRelease(whatever); This,

java.awt.image.DataBufferByte cannot be cast to java.awt.image.DataBufferInt

半城伤御伤魂 提交于 2019-11-29 12:00:38
I have some errors atm while im coding with JAVA, I have been trying to fix this for along time, also trying to find oterh ppl who have same problem and fixed it but nothing work... Well.. here is the code package ca.vanzeben.game; import java.awt.BorderLayout; import java.awt.Canvas; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.image.BufferStrategy; import java.awt.image.BufferedImage; import java.awt.image.DataBufferInt; import javax.swing.JFrame; public class Game extends Canvas implements Runnable { private static final long serialVerisionUID

Drawing Sierpinski's Triangle in Java

北城以北 提交于 2019-11-29 11:44:23
I'm having some issues with my code to draw a Sierpinski's Triangle (or Sierpinski's Gasket), but I'm not sure what the problem is. The lines for the triangle are drawn, then all the fractals, then it disappears. Help? import javax.swing.*; import java.awt.*; public class SierpinskiGasket extends JFrame { Point x=new Point(5,545), y=new Point(300,25), z=new Point(605,545), current=x, target; private int count=0; public SierpinskiGasket () { super("Sierpinski Gasket"); setSize(610,550); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().setBackground(Color.WHITE);

ImageIcons on JButton are not showing up in Runnable JAR file

空扰寡人 提交于 2019-11-29 11:07:39
I have 6 JButtons on my GUI all have images on it, when I compile and run the code, all images on JButtons show up perfectly but in runnable JAR file, images on JButtons are not showing up.. how do I fix this problem? I used this method in my code to show icons on JButtons ImageIcon SettingsIc = new ImageIcon("bin/images/settings.png"); jb1 = new JButton(SettingsIc); jb1.setFocusPainted( false ); //jb1.setBorderPainted(false); jb1.setContentAreaFilled(false); This is how my GUI looks when I compile my code in Eclipse This is how my GUI looks after executing Runnable JAR file This (as pointed

Empty String validation for Multiple JTextfield

你说的曾经没有我的故事 提交于 2019-11-29 10:59:16
Is there a way to validate a number of JTextfields in java without the if else structure. I have a set of 13 fields, i want an error message when no entry is given for any of the 13 fields and to be able to set focus to that particular textbox. this is to prevent users from entering empty data into database. could someone show me how this can be achieved without the if else structure like below. if (firstName.equals("")) { JOptionPane.showMessageDialog(null, "No data entered"); } else if (lastName.equals("")) { JOptionPane.showMessageDialog(null, "No data entered"); } else if (emailAddress

Swing and AWT Mixing is bad, but still done, why?

帅比萌擦擦* 提交于 2019-11-29 09:22:02
I have noticed that people recommend not intermixing Swing and AWT Components , however we see this alot: import javax.swing.AbstractButton; import javax.swing.JButton; import javax.swing.JPanel; import javax.swing.JFrame; import javax.swing.ImageIcon; //AWT imports though only for listeners import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; So why do many including Java (because I got that off their tutorial here ) still use AWT imports, though I see its mainly for Listener s. How do you add native Swing Listener s/Libraries for stuff like