| How to place a window in the center of the screen ? |
|
 |
Index ‹ java-programmer
|
- Previous
- 3
- Java Swing font anti-aliasing w/o desktop environmentHello all,
With the newer versions of Java your Swing apps can look extra pretty
thanks to font smoothing. When using Ubuntu (as I am) the builtin
desktop environment configurations set this up for you - when running
GNOME, KDE, or XFCE it looks great. But I choose to run my .xsession
file instead, which loads FVWM. And somewhere along the line something
isn't set right and I don't get pretty smoothed fonts in my Swing apps.
I checked environment variables and I can't see it there, so I'm
thinking it may be an X resource? I don't know; if anybody has any
ideas on which knob I can twiddle to get the pretty fonts back, I'd
greatly appreciate it!
Best regards.
--
Taylor Venable
email***@***.com
http://real.metasyntax.net:2357/
- 5
- EJB: removing local TOsHi!
This applies to WebLogic 7 but probably is generally the case.
I have a Collection of Local objects which I want to remove(). So I thought
collection.clear() would be sufficient and the container would do the job.
But it doesn't.
So, I iterate thru the Collection and call remove() on each Local object.
This throws a ConcurrentModificationException because the container does
remove the element from the Collection when calling remove() on the Local
object.
The only solution seems to iterate over the collection, put the objects to
be deleted in another collection, iterating over that collection and
calling remove() there on any Local object.
Well, there must be something I misunderstood about EJB, right?
- 6
- 6
- Important and UrgentHi all,
Please read on:
We are doing a survey to collect information on middleware usage
and some features of the middleware. Through the survey data as one
source of information, we can do research on middleware, and software
engineering trend analysis and prediction.
Please take out some minutes to fill in this survey. Please this is
really important survey.
Suvey link:
http://swtech.njit.edu/middlewaresurvey/
As this group includes Students/Faculties as well as Professionals,
please fill up the Individual survey and other corresponding surveys.
I am sure you guys will take part and will help this research.
Give little time to make this survey successful.
Your efforts are appreciated.
Thank you all
Purvi
- 6
- What is the difference between a BMP EntityBean and a DAO ?As I read in a couple of articles a BMP (Bean Managed Persistence is an EntityBean
where the programmer has to care about the SQL statements.
On the other side a DAO implements the direct access to an SQL database.
But isn't that the same?
What are the differences ?
Lars
- 6
- TCP client application does detect network failureHi,
This is a behavior of the TCP socket in Java I dont' understand:.
Let say we have a simple client/server application running on TWO different
machines.
The client send bytes to the server.
The server recieved the bytes and wait for about X sec.
During this waiting time, we disconnect the server from the network (just by
unplugging the server network cable).
After the waiting time the server is aware of the network failure (a
SocketException is thrown : connection reset, because it is trying to send
the response)
But on the client side it is still stuck on a "rcv =
commandInput.readLine();" statement (see after for the complete code), it
will never be aware of the network failure !!! Even after one hour the
client is still waiting to read something on closed socket. Is this the
normal behavior ?
In reality we are dealing with an application that use the FTP server to
server mode (we are controlling only the command socket, the data transfer
is made by the servers). We have to transfer huge file, thus we can not set
up a time out. If the last scenario occurs, some of our transfers are stuck
and it is not possible for us to detect the failure. The client will wait
for ever the response.
Does anybody have already deal with that ?
Below is the simple client and server I made in order to make my experiment.
The client just read the keyboard input and send it to the server; the
server just echo the request except if the request
is 'wait X', in this case it will wait for X sec before responding.
Thanks by advance,
Olivier MERIGON
CLIENT:
----------------------------------------------------------------------------
-------
package com.iratensolutions.test.ftp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
/**
* A Client/Server application to test the network failure behavior with TCP.
* This the client part. It just sends the keyboard input to the server.
* @author Olivier MERIGON
*/
public class TestNetworkFailureClient {
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("usage: java
com.iratensolutions.test.ftp.TestNetworkFailureClient serverAdress");
}
Socket commandSocket = null;
BufferedReader commandInput;
BufferedReader keyboardInput;
PrintWriter commandOutput;
try {
commandSocket = new Socket(args[0], 666);
commandInput = new BufferedReader(new
InputStreamReader(commandSocket.getInputStream()));
keyboardInput = new BufferedReader(new InputStreamReader(System.in));
commandOutput = new PrintWriter(new
OutputStreamWriter(commandSocket.getOutputStream()));
} catch (Exception e) {
e.printStackTrace();
return;
}
System.out.println("CLIENT STARTED");
String snd;
String rcv = null;
try {
do {
System.out.print("KEYBOARD: ");
snd = keyboardInput.readLine();
if (snd != null) {
commandOutput.println(snd);
commandOutput.flush();
System.out.println("SND: " + snd);
rcv = commandInput.readLine();
System.out.println("RCV: " + rcv);
}
} while (rcv != null);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
--------------------------------------------------------------------------
SERVER
----------------------------------------------------------------------
package com.iratensolutions.test.ftp;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* A Client/Server application to test the network failure behavior with TCP.
* This the server part. The server can wait X second after responding to a
request.
* If a wait request is received, the server just answer the time waited
after
* the requested wait time, otherwise it respond immediatly the request
number.
* usage: wait intNbSec | any text
* @author Olivier MERIGON
*/
public class TestNetworkFailureServer {
public static void main(String args[]) {
ServerSocket echoServer = null;
String line;
DataInputStream is;
PrintStream os;
Socket clientSocket = null;
try {
echoServer = new ServerSocket(666);
System.out.println("SERVER STARTED");
} catch (IOException e) {
System.out.println(e);
}
while (true) {
try {
clientSocket = echoServer.accept();
System.out.println("Handling new client...");
is = new DataInputStream(clientSocket.getInputStream());
os = new PrintStream(clientSocket.getOutputStream());
int loopId = 0;
do {
line = is.readLine();
System.out.println("RCV: " + line);
//Handle "Wait" command
if (line.trim().startsWith("wait")) {
String[] tab = line.split("\\s");
boolean ok = false;
try {
int nbSec = Integer.parseInt(tab[1]);
System.out.println("...waiting for " + nbSec + " sec.");
try {
Thread.sleep(nbSec * 1000);
} catch (InterruptedException e1) {
}
String resp = "...end of wainting periode of " + nbSec + " sec.";
os.println(resp);
System.out.println("SND: " + resp);
ok = true;
} catch (Exception e) {
ok = false;
}
if (!ok) {
String resp = "usage: wait intNbSec | any text";
os.println(resp);
System.out.println("SND: " + resp);
}
//Handle "normal" action
} else {
os.println("# " + loopId);
System.out.println("SND: # " + loopId);
}
loopId++;
} while (line != null);
} catch (IOException e) {
System.out.println(e);
}
}
}
}
- 8
- JFace getting startedHi
I try to run a simple SWT/Jface example. I am running this from inside
eclipse 3.2.0 and I have included the following in my classpath:
swt.jar - Taken from swt-3.3M3-win32-win32-x86.zip
jface.jar - Taken from program files\eclipse\plugins and Renamed from
org.eclipse.jface_3.2.0.I20060605-1400.jar
Here is the class:
import org.eclipse.jface.window.*;
import org.eclipse.swt.widgets.*;
public class MyJFaceExample1 extends Window {
public MyJFaceExample1() {
// super(null); //Wont compile with null as the beginner example says
it should
super(new Shell()); //Compiles
}
public static void main(String[] args) {
MyJFaceExample1 demo = new MyJFaceExample1();
demo.setBlockOnOpen(true);
demo.open();
Display.getCurrent().dispose();
}
------------------------------------------------------------
This compiles fine but when I try to run this I get:
java.lang.NoClassDefFoundError:
org/eclipse/core/commands/common/EventManager
I then copy c:\program
files\eclipse\plugins\org.eclipse.core.commands_3.2.0.I20060605-1400.jar
and rename it to core.jar.
I then get the following error: java.lang.NoClassDefFoundError:
org/eclipse/core/runtime/ListenerList
Then I am stuck.
Please help me with getting started with JFace.
- 8
- using ClassDep to reduce rt.jari have been trying to use the JINI tool ClassDep to create a list of the
dependencies of a program i have written, but have been unsuccessful. when
i run it i output a list of classes that ClassDeps says my program needs,
but when i create an rt.jar file containing only those classes (yes, in the
appropriate package directories) i get the following:
NoClassDefFoundError: java.lang.ArrayStoreException.
i figured maybe it was a fluke, and just manually included the class.
after doing so i got the following, more confusing error:
error: java.lang.Error: java.io.UnsupportedEncodingException: Cp1252
Cp1252 is not something i specifically make reference to in my program, and
have only a nebulous idea of what it even is/does.
anyway, though, that's beside the point: i would really like to make a
slimmed down version of rt.jar to fit my specific needs according to the
classes that are needed by my app.
is there any way to do this that works, or am i just using ClassDep
incorrectly?
Please Help!
Thank You!
-Voltaic
- 8
- AWT Cursor in vista problemHi,
i have an applet with a black background.
when i set the Cursor to Cursor.TEXT_CURSOR in Vista the cursor is
displayed also in black, and it is not seen.
any idea???
- 9
- How does one combine the Adapter and Factory design patterns in a memory efficient way?I'm using the adapter pattern, and I've got a factory to generate adapters
for passed in adaptees. Let's call the class of the objects getting adapted
"Foo", and the Adapter class itself "Bar".
So here are some details specific to my situation:
1) the constructor for Bar is private and visible to the factory (Bar is
acting as its own factory), so I can completely control when and how Bar
gets instantiated.
2) If an existing adapter for a given instance of Foo doesn't exist yet,
I'll want to create a new Bar which matches with it.
3) If there already is a adapter h for Foo, I have to return that existing
match, and NOT generate a new one.
4) upon a request for an adapter for null, I return null.
This is relatively easy to do if I have infinite memory. I just create a
Map<Foo,Bar>, like so:
<code>
public class Bar {
private final Foo f;
private Bar(Foo f) {
this.f = f;
}
private final static Map<Foo,Bar> mapping = new
ConcurrentHashMap<Foo,Bar>();
public static Bar make(Foo f) {
if (impl == null) {
return null;
}
synchronized (mapping) {
if (!mapping.containsKey(f)) {
mapping.put(f, new Bar(f));
}
Bar b = mapping.get(f);
assert returnValue != null;
return returnValue;
}
}
public void newInterfaceWhichModifiesState() {
this.f.oldInterfaceWhichModifiesState();
}
public int newInterfaceWhichGetsState() {
return this.f.oldInterfaceWhichGetsState();
}
}
</code>
However, it's possible that the calling code is generating billions and
billions of instances of Foo, and then throwing them away after their first
use. My Map would prevent the garbage collector from being able to reclaim
those instances. I can safely delete those "throwaway Foos" and their
matches from my Map, because if there doesn't exist a reference to some
instance of Foo anywhere else in the JVM, then it can't possibly ever occur
that that instance will ever get passed into my make(Foo) method, in which
case, I would never need to return its corresponding Bar.
The two potential-solutions I looked at, Maps of WeakReferences and
WeakHashMap, turned out not to satisfy my requirements.
If instead of a Map<Foo,Bar>, I had a Map<Foo,WeakReference<Bar>>, then it's
possible that the matching Bar would get garbage collected, but the instance
of Foo would get passed in again, and there's no way for me to recover its
matching pair, thus violating condition (3) mentioned above.
If I replace Map<Foo,Bar> with WeakHashMap<Foo,Bar>, none of the keys will
get GCed, because instances of Bar contain a strong reference to Foo, via
the private field f. If I change that field to a weak reference, then it's
possible the instance of Foo that's being adapted will get GCed while the
corresponding adapter is still in use, resulting in the newInterface()
method failing.
I think what I need is some sort of special PairOfWeakReference class such
that if there are any references to either an instance of Foo or its
corresponding Bar, then BOTH remain uncollectable. However, once there do
not exist any strong references to either instances, then the pair become
collectable simultaneously.
Using a pair class, as in:
<code>
public class Pair<A,B> {
public final A a;
public final B b;
public Pair(A a, B b) {
this.a = a;
this.b = b;
}
}
</code>
WeakReference<Pair<Foo,Bar>> won't work either, because the WeakReference
class is checking against references to instances of the Pair class, rather
than references to the instances of Foo and Bar.
This adapter-factory combination doesn't sound like something that unusual
or obscure, so I figure I must be missing something simple from effectively
implementing it. Can anyone tell me what that is?
- Oliver
- 10
- DND using XML Hi,
I want to transfer a block of XML between 2 DND objects in java
What's the best way to do this?
steve
- 11
- java graph theoryi am a student tryingto write a progamme that will use graphs as their
starting point but dont know where to start. i have a small amount of
code to get me started but not much help.
If anyone can help me in anyway woulf be helpful.( places to look/
tips/ people to speak to/ books to look @ etc..)
- 13
- dynamic instanciation of a generic classHi,
I've a question about Tiger (Java 5), genercity and dynamic instanciation
of class.
I can't found tutorial or sample via the Net.
By exemple :
public class A<T>
{
private T v;
public A(T v)
{
this.v=v;
}
public void test()
{
System.out.println(v.getClass().getName());
}
}
I'd like instanciate the A class with something like that : Class.forName
("A").... but why can I specified T parameter ?
Please, send me some sample or URL.
Thanks,
Arnaud.
- 13
- sending a message from jboss to all java-clientsHi
I'm building a j2ee Application on top of j2ee and JBoss 4.0.
The Clients connected to JBoss are non-web Swing Clients.
If One Clients changes the data on a bean/in the db, all the other clients
connected to the jboss have to be informed about the change, so they can
update der UI.
On a standalone Java-App i would use listeners to solve this, but as im new
to j2ee i have no idea how to realize this with j2ee and jboss.
It would be great to get some ideas on how to solve this problem.
greets from Germany
Johannes Hermen
- 16
- Panel text not showing in JFrameI have a JFrame to display after a button is clicked. The following
JFrame appears, but the label text does not appear until the thread is
completed. Any advise:
public void run(){
JPanel panel = new JPanel(); // create pane content object
JLabel prompt = new JLabel("Processing log files");
panel.add(prompt);
Container contentPane = myFrame.getContentPane();
contentPane.add(panel);
myFrame.setTitle("Processing logs...Please Wait");
myFrame.setBounds(100,100,350,75); // position frame
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.toFront();
panel.add(prompt);
myFrame.setVisible(true);
myFrame.pack();
System.out.println ("pack1 done");
myFrame.repaint();
myFrame.pack();
}
|
| Author |
Message |
tom.parson

|
Posted: 2005-11-15 20:43:00 |
Top |
java-programmer, How to place a window in the center of the screen ?
I would like to open a window and to placed it at a certain position on a screen
(e.g. in the center).
How do I do this? My (simplified) current code looks like:
public class testGBL extends JFrame implements ActionListener {
JLabel lab1;
....
JButton butBack;
public testGBL() {
...
Container contentPane = getContentPane();
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
contentPane.setLayout(gridbag);
c.fill = GridBagConstraints.HORIZONTAL;
butBack = new JButton("Back");
c.gridx = 0;
c.gridy = 0;
c.weightx = 1.0;
c.gridwidth = 2;
gridbag.setConstraints(butBack, c);
butBack.setEnabled(false);
butBack.addActionListener(this);
contentPane.add(butBack);
....
}
public static void main(String args[]) {
testGBL window = new testGBL();
window.setTitle("test");
window.pack();
window.setVisible(true); }
}
|
| |
|
| |
 |
VisionSet

|
Posted: 2005-11-15 21:00:00 |
Top |
java-programmer >> How to place a window in the center of the screen ?
"Tom Parson" <email***@***.com> wrote in message
news:4379d7b5$0$21942$email***@***.com...
> I would like to open a window and to placed it at a certain position on a
screen
> (e.g. in the center).
window.setLocation(int x, int y);
or if you want it in the centre use this cludge:
window.setLocationRelativeTo(null);
should take a component as the argument and centre it on that
|
| |
|
| |
 |
carlos

|
Posted: 2005-11-15 21:18:00 |
Top |
java-programmer >> How to place a window in the center of the screen ?
>I would like to open a window and to placed it at a certain
>position on a screen (e.g. in the center).
>
>How do I do this? My (simplified) current code looks like:
>
>public class testGBL extends JFrame implements ActionListener {
> JLabel lab1;
> ....
> JButton butBack;
>
> public testGBL() {
>
> ...
> Container contentPane = getContentPane();
> GridBagLayout gridbag = new GridBagLayout();
> GridBagConstraints c = new GridBagConstraints();
> contentPane.setLayout(gridbag);
>
> c.fill = GridBagConstraints.HORIZONTAL;
>
> butBack = new JButton("Back");
> c.gridx = 0;
> c.gridy = 0;
> c.weightx = 1.0;
> c.gridwidth = 2;
> gridbag.setConstraints(butBack, c);
> butBack.setEnabled(false);
> butBack.addActionListener(this);
> contentPane.add(butBack);
> ....
> }
>
> public static void main(String args[]) {
> testGBL window = new testGBL();
> window.setTitle("test");
> window.pack();
> window.setVisible(true); }
> }
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
int X = (screen.width / 2) - (widthWindow / 2); // Center horizontally.
int Y = (screen.height / 2) - (heightWindow / 2); // Center vertically.
window.setBounds(X,Y , widthWindow,heightWindow);
|
| |
|
| |
 |
Igor Planinc

|
Posted: 2005-11-15 21:40:00 |
Top |
java-programmer >> How to place a window in the center of the screen ?
Tom Parson wrote:
> I would like to open a window and to placed it at a certain position on a screen
> (e.g. in the center).
>
> How do I do this? My (simplified) current code looks like:
>
>
>
> public class testGBL extends JFrame implements ActionListener {
> JLabel lab1;
> ....
> JButton butBack;
>
> public testGBL() {
>
> ...
> Container contentPane = getContentPane();
> GridBagLayout gridbag = new GridBagLayout();
> GridBagConstraints c = new GridBagConstraints();
> contentPane.setLayout(gridbag);
>
> c.fill = GridBagConstraints.HORIZONTAL;
>
> butBack = new JButton("Back");
> c.gridx = 0;
> c.gridy = 0;
> c.weightx = 1.0;
> c.gridwidth = 2;
> gridbag.setConstraints(butBack, c);
> butBack.setEnabled(false);
> butBack.addActionListener(this);
> contentPane.add(butBack);
> ....
> }
>
> public static void main(String args[]) {
> testGBL window = new testGBL();
> window.setTitle("test");
> window.pack();
> window.setVisible(true); }
> }
>
.setLocation() or .setBounds().
|
| |
|
| |
 |
Chris Smith

|
Posted: 2005-11-15 22:08:00 |
Top |
java-programmer >> How to place a window in the center of the screen ?
Tom Parson <email***@***.com> wrote:
> I would like to open a window and to placed it at a certain position on a screen
> (e.g. in the center).
There's an easy way to center a Swing JFrame on the screen:
f.setLocationRelativeTo(null);
Do this after the pack() or setSize(...) call.
--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
|
| |
|
| |
 |
Roedy Green

|
Posted: 2005-11-16 1:39:00 |
Top |
java-programmer >> How to place a window in the center of the screen ?
On 15 Nov 2005 12:42:30 GMT, email***@***.com (Tom Parson) wrote,
quoted or indirectly quoted someone who said :
>I would like to open a window and to placed it at a certain position on a screen
>(e.g. in the center).
>
>How do I do this? My (simplified) current code looks like:
see http://mindprod.com/jgloss/coordinates.html
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
|
| |
|
| |
 |
Carl

|
Posted: 2005-11-16 1:48:00 |
Top |
java-programmer >> How to place a window in the center of the screen ?
Tom Parson wrote:
> I would like to open a window and to placed it at a certain position on a screen
> (e.g. in the center).
>
> How do I do this? My (simplified) current code looks like:
>
>
>
> public class testGBL extends JFrame implements ActionListener {
...snip
Tom,
You can call the the setBounds method to fine tune the components
location and size on the screen.
A java.awt.Toolkit object will also be helpful if you want information
son the screen size in order to place your component in a relative location.
Carl.
|
| |
|
| |
 |
Hal Rosser

|
Posted: 2005-11-16 6:21:00 |
Top |
java-programmer >> How to place a window in the center of the screen ?
"Tom Parson" <email***@***.com> wrote in message
news:4379d7b5$0$21942$email***@***.com...
> I would like to open a window and to placed it at a certain position on a
screen
> (e.g. in the center).
>
> How do I do this? My (simplified) current code looks like:
>
>
>
> public class testGBL extends JFrame implements ActionListener {
> JLabel lab1;
> ....
> JButton butBack;
>
> public testGBL() {
>
> ...
> Container contentPane = getContentPane();
> GridBagLayout gridbag = new GridBagLayout();
> GridBagConstraints c = new GridBagConstraints();
> contentPane.setLayout(gridbag);
>
> c.fill = GridBagConstraints.HORIZONTAL;
>
Take a look at the Toolkit (getDefaultToolkit) and its getScreenSize()
method which returns a DImension object, which has width and height
properties of the screen.
Then you use the setBounds method of the JFrame to set the x,y,width,and
height from your meticulous calculations.
|
| |
|
| |
 |
Madguy

|
Posted: 2005-11-16 17:05:00 |
Top |
java-programmer >> How to place a window in the center of the screen ?
after packing the window, you must set the position of the window like
this.
left_of_window = (screen_resolution_width - width_of_the_window)/2
top_of_the_window = (screen_resolution_height - height_of_the_window)
/2
this would center the window in the screen.
/Cheers,
-MadGuy
|
| |
|
| |
 |
Alex Molochnikov

|
Posted: 2005-11-17 8:14:00 |
Top |
java-programmer >> How to place a window in the center of the screen ?
To place the JFrame at the desired origin, use setLocation() method. To
center JFrame on the screen, use setLocationRelativeTo(null).
Reading the JFrame docs could also help.
"Tom Parson" <email***@***.com> wrote in message
news:4379d7b5$0$21942$email***@***.com...
> I would like to open a window and to placed it at a certain position on a
screen
> (e.g. in the center).
>
> How do I do this? My (simplified) current code looks like:
>
>
>
> public class testGBL extends JFrame implements ActionListener {
> JLabel lab1;
> ....
> JButton butBack;
>
> public testGBL() {
>
> ...
> Container contentPane = getContentPane();
> GridBagLayout gridbag = new GridBagLayout();
> GridBagConstraints c = new GridBagConstraints();
> contentPane.setLayout(gridbag);
>
> c.fill = GridBagConstraints.HORIZONTAL;
>
> butBack = new JButton("Back");
> c.gridx = 0;
> c.gridy = 0;
> c.weightx = 1.0;
> c.gridwidth = 2;
> gridbag.setConstraints(butBack, c);
> butBack.setEnabled(false);
> butBack.addActionListener(this);
> contentPane.add(butBack);
> ....
> }
>
> public static void main(String args[]) {
> testGBL window = new testGBL();
> window.setTitle("test");
> window.pack();
> window.setVisible(true); }
> }
>
|
| |
|
| |
 |
red eff

|
Posted: 2005-11-17 9:53:00 |
Top |
java-programmer >> How to place a window in the center of the screen ?
email***@***.com (Tom Parson) wrote in
news:4379d7b5$0$21942$email***@***.com:
> I would like to open a window and to placed it at a certain position
> on a screen (e.g. in the center).
>
> public static void main(String args[]) {
> testGBL window = new testGBL();
> window.setTitle("test");
> window.pack();
> window.setVisible(true); }
> }
>
>
put this in ---
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int x = (int) ((d.getWidth() - this.getWidth())/ 2);
int y = (int) ((d.getHeight() - this.getHeight())/ 2);
this.setLocation(x, y);
//this.setAlwaysOnTop(true); // show on top - 5.0
this.setVisible(true);
|
| |
|
| |
 |
jonck

|
Posted: 2005-11-17 21:13:00 |
Top |
java-programmer >> How to place a window in the center of the screen ?
> I would like to open a window and to placed it at a certain position on a screen
If the variables "width" and "height" are the width and height of your
JFrame, something like this:
int width = 700;
int height = 233;
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
int x = (screen.width-width)/2;
int y = (screen.height - height)/2;
setBounds(x,y,width,height);
|
| |
|
| |
 |
tele2

|
Posted: 2005-11-20 6:32:00 |
Top |
java-programmer >> How to place a window in the center of the screen ?
I had to do this a few days ago.
This is the code I wrote :
public static void center(JFrame frame) {
frame.pack();
double height = frame.getHeight();
double width = frame.getWidth();
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
double x = screen.getWidth() - width;
double y = screen.getHeight() - height;
x = x / 2.0;
y = y / 2.0;
if ( x < 0) x = 0;
if ( y < 0) y = 0;
frame.setBounds((int)x,(int)y,(int)width,(int)height);
}
Hope this will help
Olivier
"Tom Parson" <email***@***.com> a 閏rit dans le message de news:
4379d7b5$0$21942$email***@***.com...
>I would like to open a window and to placed it at a certain position on a
>screen
> (e.g. in the center).
>
> How do I do this? My (simplified) current code looks like:
>
>
>
> public class testGBL extends JFrame implements ActionListener {
> JLabel lab1;
> ....
> JButton butBack;
>
> public testGBL() {
>
> ...
> Container contentPane = getContentPane();
> GridBagLayout gridbag = new GridBagLayout();
> GridBagConstraints c = new GridBagConstraints();
> contentPane.setLayout(gridbag);
>
> c.fill = GridBagConstraints.HORIZONTAL;
>
> butBack = new JButton("Back");
> c.gridx = 0;
> c.gridy = 0;
> c.weightx = 1.0;
> c.gridwidth = 2;
> gridbag.setConstraints(butBack, c);
> butBack.setEnabled(false);
> butBack.addActionListener(this);
> contentPane.add(butBack);
> ....
> }
>
> public static void main(String args[]) {
> testGBL window = new testGBL();
> window.setTitle("test");
> window.pack();
> window.setVisible(true); }
> }
>
|
| |
|
| |
 |
Brandon McCombs

|
Posted: 2005-11-21 5:48:00 |
Top |
java-programmer >> How to place a window in the center of the screen ?
Tom Parson wrote:
> I would like to open a window and to placed it at a certain position on a screen
> (e.g. in the center).
>
> How do I do this? My (simplified) current code looks like:
>
>
This puts the window itself in the middle of the screen and not just the
top left corner of the window.
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
frame.setSize(dim.width/4, dim.height/4);
frame.setLocation(dim.width/2 - dim.width/8, dim.height/2 - dim.height/8);
>
> public class testGBL extends JFrame implements ActionListener {
> JLabel lab1;
> ....
> JButton butBack;
>
> public testGBL() {
>
> ...
> Container contentPane = getContentPane();
> GridBagLayout gridbag = new GridBagLayout();
> GridBagConstraints c = new GridBagConstraints();
> contentPane.setLayout(gridbag);
>
> c.fill = GridBagConstraints.HORIZONTAL;
>
> butBack = new JButton("Back");
> c.gridx = 0;
> c.gridy = 0;
> c.weightx = 1.0;
> c.gridwidth = 2;
> gridbag.setConstraints(butBack, c);
> butBack.setEnabled(false);
> butBack.addActionListener(this);
> contentPane.add(butBack);
> ....
> }
>
> public static void main(String args[]) {
> testGBL window = new testGBL();
> window.setTitle("test");
> window.pack();
> window.setVisible(true); }
> }
>
|
| |
|
| |
 |
Raja

|
Posted: 2005-11-24 5:35:00 |
Top |
java-programmer >> How to place a window in the center of the screen ?
Here is the code to place ur frame in the center of the screen:
Dimension screenSize =
Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
int frameX = (screenSize.height - frameSize.height) / 2;
int frameY = (screenSize.height - frameSize.height) / 2;
frame.setLocation(frameX, frameY);
|
| |
|
| |
 |
Vova Reznik

|
Posted: 2005-11-24 5:36:00 |
Top |
java-programmer >> How to place a window in the center of the screen ?
Raja wrote:
> Here is the code to place ur frame in the center of the screen:
>
> Dimension screenSize =
> Toolkit.getDefaultToolkit().getScreenSize();
> Dimension frameSize = frame.getSize();
>
> if (frameSize.height > screenSize.height) {
> frameSize.height = screenSize.height;
> }
>
> if (frameSize.width > screenSize.width) {
> frameSize.width = screenSize.width;
> }
>
> int frameX = (screenSize.height - frameSize.height) / 2;
width????
> int frameY = (screenSize.height - frameSize.height) / 2;
>
> frame.setLocation(frameX, frameY);
>
|
| |
|
| |
 |
Kent Paul Dolan

|
Posted: 2005-11-24 8:59:00 |
Top |
java-programmer >> How to place a window in the center of the screen ?
Tom Parson wrote:
> I would like to open a window and to placed it at a certain position on a screen
> (e.g. in the center).
> How do I do this?
Specifically to place it in the center (but not some arbitrary other
place),
Sun shows a pretty way to do this in:
http://java.sun.com/docs/books/tutorial/uiswing/components/example-1dot4/FrameDemo2.java
The line in question says (for a JFrame):
frame.setLocationRelativeTo(null); //center it
and is line 263 in that example. That actually works, though why
it does is probably some Java internals defaults mystery.
Otherwise, the usual trick is to use setLocation, which uses the frame
top left corner coordinates (in some form) as its input.
HTH
xanthian.
|
| |
|
| |
 |
| |
 |
Index ‹ java-programmer |
- Next
- 1
- in PARAM NAME causing problemsHello,
I have an application that generates a HTML document that contains an
applet. It places raw data into the PARAM NAME tag. Well, once in a blue
moon it places the numeric character reference into the data. When my
Java applet imports this using the getParameter command, it reacts
differently between different version of IE. One solution is to not let my
original application generate the sequence , however, this does not fix
the HTML documents that have already been generated in the past. It would be
nice to just fix the Java applet so it can import this raw data without
having to recognize as a numeric character reference.
Is this possible?
Here is my HTML code:
<CENTER><APPLET CODE="graphics.class" ALIGN=RIGHT HEIGHT=420 WIDTH=220>
<PARAM NAME=Graphics VALUE="
>=>.>/(%<!<!>((6<0>%(#(!(+>%(!(>(!>%(%>%>!(&/<(*<&>*/6>0>((!>+(%(<()<&(!>!(=(><!>#/)(#(6>+/=(.>+/1>&>#16<!/>(*(#/!(*(!/!(//#((>=1(/=/01)(/1=/!(=1//61!!6/>!61=>1!>/+!(!./!!+1%//./1*1<.&//./%.1!.+%*..<#&.%/.%%1.1/.&//.*.>1=!01*1=!!(.!1!<(&!)/*/(1#(+1+1!/)1!<!(11>>./#1)(>/%/*(//!>%/#/#((1<(/(616(&/)/<>#>.(%(1(+(<(+>1>+(>(%>&(+>((0(%((((>+(0(6>>()(/>+(&>%<%>.(6>#>>(!<.()(%<!>((!>!<&(!()<!(=(<>/>1<#??
"></APPLET></CENTER>
- 2
- JTable refresh problemI have a JTable where I paint some of the cell backgrounds with
a custom renderer. getTableCellRendererComponent() paints with
drawLine() and fillRect() and that sort of thing. It's all working
fine but when I drag another window over the JTable, or when I
scroll it, sometimes it's not getting repainted correctly.
Any ideas how to make it refresh or repaint itself at the right
times?
I looked in the group archives and there were a lot of other messages
but they were about when a table's content changes dynamically.
I'm not changing the content, I'm just trying to get the table
to keep the correct content when scrolled and when other windows
pass over it.
Thanks in advance,
- Mitch Gart
- 3
- Compile error help (linking a class library)Hi,
I set the CLASSPATH correctly but still get the following compile error. I
also tried the "-classpath" alternative and get the same error message.
/home/yao/eh/jython-20.class(org/python/core/PyInteger.java):4:
class PyInteger is public, should be declared in a file named
PyInteger.java
(source unavailable)
But the class PyInteger is in a file PyInteger.java, just it is in the
class jython-20.class but no a .java source file. I use to do things like
this (linking with a class library and use the classes provided in it) a
lol before. But why this time, it's not working? Did I missing something?
Thanks,
Stan
- 4
- MIDP JVM for Palm and Pocket PCStephan Erlank wrote:
> I've heard of J9 that comes with Visual Age Micro Edition, but I don't have
> time to download 122mb over a 56k dial-up connection.
VAME is discontinued and has been replaced by WebSphere Studio Device
Developer.
http://www-3.ibm.com/software/wireless/wsdd/
But maybe that's what you meant.
--
Josef Garvi
"Reversing desertification through drought tolerant trees"
http://www.eden-foundation.org/
new income - better environment - more food - less poverty
- 5
- java.io.Exception: Too many open files on sun OS 5.6I'm operating on sun OS 5.6
I ping a host every 10 seconds to get knowlegde wheather it is running
or not. After about one and a half hour I get this Exception:
java.io.IOException: Too many open files
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:54)
at java.lang.Runtime.execInternal(Native Method)
at java.lang.Runtime.exec(Runtime.java:546)
at java.lang.Runtime.exec(Runtime.java:413)
at java.lang.Runtime.exec(Runtime.java:356)
at java.lang.Runtime.exec(Runtime.java:320)
at de.optimare.VRX8100.MainControl.pingRecorder(MainControl.java:226)
... and so on.
I set rlim_fd_max = 1024 in /etc/system
I tried to set the ulimit -n 1024 in my script
but it had no effect.
Here is my code: The pingRecorder is called every 10 seconds from a
swing.Timer.
runtime is the Runtime.getRuntime().
// used on unix system
private void pingRecorder() {
logDate = new Date();
try {
ping = runtime.exec(pingPath);
ping.waitFor();
exitValue = ping.exitValue();
switch (exitValue) {
case 0:
// stop the ping timer
if (pingTimer != null) {
pingTimer.stop();
}
if (! processIsInitialized) {
System.out.println(logDate.toString() + " Recorder alive. Now
wait " + initialWaitTime/1000 + " seconds to initialize");
//wait for recorder is booting
Thread.sleep(initialWaitTime);
// now init the VRX8100 process!
initProcess();
}
break;
case 1:
System.out.println(logDate.toString() + " Recorder off.
Memory: " + runtime.totalMemory());
break;
default:
System.out.println(logDate.toString() + " Recorder off.
Memory: " + runtime.totalMemory());
break;
}
} catch (IOException io) {
io.printStackTrace();
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
I would be very happy if anyone knows about anything due to this
problem.
Is it a specific sun problem? Is it possible to control the number of
open files by Java?
Many thanks for any help
Gert
- 6
- showdocument() problem (or bug?)I have a trusted applet that is supposed to generate a pdf file and
save it as a temporary file on the local machine, then display it.
Since display of such an animal can only happen with showDocument(URL,
type) I am trying to build the url using :
URL url = new URL("file:" + File.separator + File.separator +
"filename.pdf");
and this does indeed work IFF I run the applet locally. However, if the
bloody thing is running connected to the server, it does not. Then the
only command that works is
URL url = new URL(getCodeBase(), "filename.pdf");
so I had to bytestream the pdf down to server, have it install it in a
file and make the call. A real waste, considering I'm supposed to be
writing non-polluting software. Any ideas as to what's wrong with this?
Thanks.
- 7
- CLOB.createTemporary throws ClassCastException...I am also having the same problem. Does anyone have a solution? I read
that the workaround is to somehow extract the underlying connection to
give to the createTemporary method. Problem is I'm not sure how to do
that!
bartleby <email***@***.com> wrote in message news:<email***@***.com>...
> I got the same probleme, have you fond out how to solve the problem ?
- 8
- Non-text based source code. I don't have much to add above what's said at the blog entry, so I'll
just link to it:
http://www.realityinteractive.com/rgrzywinski/archives/cat_ideas.html
It's not a particularly novel idea (Roedy has mentioned it often in this
newsgroup, for example; calls it "SCID" I believe), but it's interesting
because (assuming those screenshots weren't doctored), it looks like we'll
be seeing IDEs with practical support for non-text based source code in the
near future (on the order of single-digit years?)
- Oliver
- 9
- 10
- Socket latenciesHi all,
I am working on a Java project and I am interested in measuring TCP
performance over long delay links. Do you know if there is any way to
simulate delay using Sockets, keeping the TCP protocol behaviour
intact? I wrote a FilterInputStream that waits for some time and then
reads from the stream, but I am afraid it could be not very accurate.
Thank you.
Daniela B
- 11
- Java NIO selector close idle connections?I have an object attached to all selectionKeys with a timestamp
showing last activity
on the connection. So how/when do i run through the keys to find
connections
that have been idle too long? Can i do it in the same main selector
loop that handles accept/read etc like this:
while (true) {
selector.select(sometimeout);
Iterator selectedKeys = selector.selectedKeys().iterator();
while (selectedKeys.hasNext()) {
..process the selected key... accept/read etc..
}
if (timeToCheckForIdleConnections) {
Set<SelectionKey> k = selector.keys();
.. get attachment of all keys and check their timestamp for last
activity.. if idle for too long then close them!
}
}
or should I use a timethread that signals back to the main selector
thread which connections to close?
- 12
- I'm looking for jsp hostingHello
I'm looking for reliable jsp web hosting. I won't be using mysql, I'm
more interested in postgres or other robust rdms
Thanks
Peter Mount
email***@***.com
- 13
- xmlencoder/decoder persistence delegateHi,
I am using the xmlencoder/decoder classes to save objects, some of
which do not have no-argument constructors. I understand that one could
use something like:
String[] prop = {"param1", "param2"};
MyEncoder.setPersistenceDelegate(MyClass.class
new DefaultPersistenceDelegate(prop);
My question is, what if one has a class like:
class A{
-----
B objB = new B(C c, D d);
-----
}
class B{
-----
E objE = new E(F f, G g);
-----
}
I need to save an object of A that contains an object of B that does
not have a default constructor and B in turn needs to store an object
of E which again does not have a default constructor. Assuming that all
attributes of all classes that need to be saved do have the required
setter and getter methods, I couldn't find a way to save all those
objects because I can set the persistence delegate for only one class.
Is it possible to save all the objects?
TIA.
--Jo
- 14
- Clarification of JavaLive delegates criticism?Back when the new J2SE1.5 features were announced, there was a
JavaLive community chat
(http://java.sun.com/developer/community/chat/JavaLive/2003/jl0729.html)
in which Neal Gafter explains the Sun stance on lack of support for
delegates:
... There are serious semantic problems with trying to add delegates
to a language in a consistent way. The main problem is that once you
call the delegate, the original class instance is no longer part of
the call chain, so overriding in the original class no longer takes
precedence over the delegate. ...
Could someone please explain this to me a little more fully? It
sounds like he's saying that delegates cannot follow a subclass call
chain if they are created from an object stored in a superclass
reference, but I can't tell if he means overriding delegates
themselves. I would like to know:
1) What exactly does he mean in less ambiguous terms? (Or maybe just
simpler terms; it's been a while since my college programming
languages course.)
2) What an example of delegates 'failing' like this would look like.
3) What is so significant about this problem.
I am already familiar with the 'Truth about Delegates' dialogue
between Sun and MS and the lack of true type safety of C# delegates,
but not understanding this criticism bothers me. Any help would be
greatly appreciated.
Thanks,
Jeff
- 15
- source compilation problem.when I try to compile a file called check.java which uses file.jar like this
c:\javac -classpath .;file.jar check.java
on PC using MS Windows XP it gives me the following crap:
An unexpected exception has been detected in native code outside the VM.
Unexpected Signal : EXCEPTION_ACCESS_VIOLATION occurred at PC=0x6D32797D
Function=ZIP_Open+0x369
Library=h:\jdk\jre\bin\zip.dll
Current Java thread:
at java.util.zip.ZipFile.getNextEntry(Native Method)
at java.util.zip.ZipFile.access$400(ZipFile.java:26)
at java.util.zip.ZipFile$2.nextElement(ZipFile.java:301)
at com.sun.tools.javac.v8.code.ClassReader.openArchive(ClassReader.java:
972)
at com.sun.tools.javac.v8.code.ClassReader.list(ClassReader.java:1199)
at com.sun.tools.javac.v8.code.ClassReader.listAll(ClassReader.java:1320
)
at com.sun.tools.javac.v8.code.ClassReader.fillIn(ClassReader.java:1340)
at com.sun.tools.javac.v8.code.ClassReader.complete(ClassReader.java:104
9)
at com.sun.tools.javac.v8.code.Symbol.complete(Symbol.java:332)
at com.sun.tools.javac.v8.comp.Enter.visitTopLevel(Enter.java:467)
at com.sun.tools.javac.v8.tree.Tree$TopLevel.accept(Tree.java:390)
at com.sun.tools.javac.v8.comp.Enter.classEnter(Enter.java:442)
at com.sun.tools.javac.v8.comp.Enter.classEnter(Enter.java:456)
at com.sun.tools.javac.v8.comp.Enter.complete(Enter.java:588)
at com.sun.tools.javac.v8.comp.Enter.main(Enter.java:574)
at com.sun.tools.javac.v8.JavaCompiler.compile(JavaCompiler.java:334)
at com.sun.tools.javac.v8.Main.compile(Main.java:520)
at com.sun.tools.javac.Main.compile(Main.java:36)
at com.sun.tools.javac.Main.main(Main.java:27)
Dynamic libraries:
0x00400000 - 0x00407000 h:\jdk\bin\javac.exe
0x77F50000 - 0x77FF6000 C:\WINDOWS\System32\ntdll.dll
0x77E60000 - 0x77F45000 C:\WINDOWS\system32\kernel32.dll
0x77DD0000 - 0x77E5B000 C:\WINDOWS\system32\ADVAPI32.dll
0x78000000 - 0x7806F000 C:\WINDOWS\system32\RPCRT4.dll
0x77C10000 - 0x77C63000 C:\WINDOWS\system32\MSVCRT.dll
0x6D330000 - 0x6D45A000 h:\jdk\jre\bin\client\jvm.dll
0x77D40000 - 0x77DC6000 C:\WINDOWS\system32\USER32.dll
0x77C70000 - 0x77CAE000 C:\WINDOWS\system32\GDI32.dll
0x76B40000 - 0x76B6C000 C:\WINDOWS\System32\WINMM.dll
0x629C0000 - 0x629C8000 C:\WINDOWS\System32\LPK.DLL
0x72FA0000 - 0x72FFA000 C:\WINDOWS\System32\USP10.dll
0x5CD70000 - 0x5CD77000 C:\WINDOWS\System32\serwvdrv.dll
0x5B0A0000 - 0x5B0A7000 C:\WINDOWS\System32\umdmxfrm.dll
0x6D1D0000 - 0x6D1D7000 h:\jdk\jre\bin\hpi.dll
0x6D300000 - 0x6D30D000 h:\jdk\jre\bin\verify.dll
0x6D210000 - 0x6D229000 h:\jdk\jre\bin\java.dll
0x6D320000 - 0x6D32D000 h:\jdk\jre\bin\zip.dll
0x76C90000 - 0x76CB2000 C:\WINDOWS\system32\imagehlp.dll
0x6D510000 - 0x6D58C000 C:\WINDOWS\system32\DBGHELP.dll
0x77C00000 - 0x77C07000 C:\WINDOWS\system32\VERSION.dll
0x76BF0000 - 0x76BFB000 C:\WINDOWS\System32\PSAPI.DLL
Local Time = Tue Aug 03 02:35:56 2004
Elapsed Time = 2
#
# The exception above was detected in native code outside the VM
#
# Java VM: Java HotSpot(TM) Client VM (1.4.1_01-b01 mixed mode)
#
# An error report file has been saved as hs_err_pid2592.log.
# Please refer to the file for further information.
#
what should I do to compile this code.
|
|
|