I have JFrame with a JTextArea inside of it.
Font font = new Font("monospaced", Font.PLAIN, 14);
textarea.setFont(font);
Since the font is monospaced, all characters are the same width and height.
I'd like to know what this width and height is in pixels.
For this, I could use font.getStringBounds but I have no Graphics context to pass to it. frame.getGraphics() returns null.
How can I find the size of a character? Can it be done without a Graphics instance? I don't want an instance of it anyway. I just want to know how big my characters are.
You can use JFrame#getFontMetrics since one of JFrame's Superclass is Component.
If this does not work, you can also use BufferedImage to get a Graphics object:
BufferedImage image = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
You can use the image object to get an instance of Graphics.
FYI, I am using a JFrame/JTextarea to render a text based game, so I'll use this info for scaling the text and getting the dimensions of the window in units of characters
It's probably not the best approach, it would be better to simply use JTextArea#setColumns and JTextArea#setRows which will use the font based information to make determinations about it's preferred size automatically
You can then make use of the LayoutManager APIs and simply call pack on the JFrame which will pack the window around the contents, based on it's preferred size
This will also affect the preferred size of JScrollPane
I have designed a java program that open in its main file a JFrame, where different panel are then opened. I found that using the program of different machine with different screen size it can be that some panel are "cut" and , for instance I loose some bottom in the panels.
What is the better designing solution to solve this issue?
Thanks in advance to who can help.
I dont know if this sounds like a 'better' solution (btw. better than what?), but like DreadHeadedDeveloper already said you can use toolkits.
Also, you can put everything on one panel and put that panel onto a scrollpane, which puts on scrollbars, if the screen is too small.
I dont know if you can make a final size or if you want to be able to resize it (which is often more trouble than it's worth).
What does: "where different panel are then opened" mean?
Should these panels be next to each other, or do they overlap? Do they change the size of your frame?
Quite frankly, use QT or something like JavaFX, maybe Grails or anything but swing really but this is besides the point.
Anyway, for this you use LayoutManagers to reposition your elements for you. By using:
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
double width = screenSize.getWidth();
double height = screenSize.getHeight();
you can get your screensize, after which you can give subpanels percentage based sizes. Say screenheight is 800, you can pass 800*0.40 if you want 40% of the parent height as the height for a child panel through the constructor of the panel.
Something like MyPanel extends Panel
public MyPanel(int width, int height){
this.setSize(width,height);
}
The downside of layoutmanagers is that they can squish your components somewhat. I sincerely hope this at least helped you on your way to finding your answer!
I have a JTextArea in my java code, I want to increase the size of the cursor which points the writing position/spot on the JTextArea, I am not talking about the cursor which points the position of mouse, but the cursor/pointer which indicates the spot of writing on JTextArea. Please tell how I can increase its size?
The text cursor is called Caret. You can change the caret by setting a new caret with the JTextComponent.setCaret() method (JTextArea extends JTextComponent).
Carets are responsible rendering themselves, so you can paint as big caret as you would like to. Basically what you should do is extend DefaultCaret and override its paint() method.
Here's an example of a custom caret:
http://java-sl.com/tip_overwrite_mode.html
one way is to simply increase the font size. Is this what you want? or do you want to keep the font small?
Font font = new Font("Verdana", Font.BOLD, 30);
txt.setFont(font);
Now what I am doing in my program is that I am using setundecorated = true and MAXIMIZED_BOTH So it makes it go full screen and the display looks very nice, But the problem is that there are images (border) on the left and the right side of my screen and also a blue background. What happens is that in changing screens and resolutions these get disturbed and are not shown properly. Those grey patches come up again
History:
I have a java program which I wanted to always open in full screen; I was not able to find a way to do it properly so I had adjusted the minimum to (1370, 727) and maximum size. Thus, it started opening properly on my laptop, but when I changed my laptop's display to LCD, it started giving problems:
It opens in a smaller window:
If I then click on the maximize button, a grey area comes on the side and bottom (I wanted the items on screen to get stretched or center themselves):
And here for example, there is a grey patch at the bottom. Instead, I want the background to cover the whole screen.
Update 1
If I change to stretchable gridbaglayout, this is the code I used and what happens:
Menu.setExtendedState(MAXIMIZED_BOTH);
GridBagLayout gbl = new GridBagLayout();
Menu.setLayout(gbl);
JButton component = new JButton("1");
gbl.layoutContainer(Menu);
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.NONE;
gbl.setConstraints(component, gbc);
Menu.add(component);
Menu.pack();
Menu.setVisible(true);
Question
How do I set "this" frame to setExtendedState(MAXIMIZED_BOTH) as I have done to others? (if I do this in main function, I get an error; even if I make a function for this and call it in main I get an error)
How do I get everything to stretch/rearrange themselves according to the extra grey space?
Update 2
My files in this project:
Update 3
This is the current file I am working on "FormTTS.java"
Search for "MAXIMIZED_BOTH" in there and you will find the code I think you will want to check.
Usually, as far as games go, it's preferable to use full screen mode instead of using a maximized window. You can do this in Java by using:
GraphicsEnvironment gfxEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gfxDev = gfxEnv.getDefaultScreenDevice();
Window window = new GameWindow();
gfxDev.setFullScreenWindow(window);
If you still want to use a regular frame and center the content panel, you need to define some of the GridBagLayout constraints. It's impossible to tell which without out seeing the code for the rest of the components on that screen, but consider the following:
GridBagConstraints.fill
GridBagConstraints.anchor
GridBagConstraints.weightx
GridBagConstraints.weighty
And finally, regarding setting the screen to the largest size, it is already addressed here:
Java JFrame Size according to screen resolution
I am also having same requirement as you have, below code works for me.
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(0,0,d.width,d.height); // i assume you have extended JFrame
try this, hope it works for you as well.
MyFrame mFrame= new MyFrame();
mFrame.setVisible(true);
mFrame.setExtendedState(mFrame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
I know this is a terrible answer because I don't have time to write any code. Have you tried creating a listener so you can get the proper maximum size once the window is actually created, and then setting the GridBagConstraints weightx and weighty properties accordingly?
Did you try this code
JFrame frame = new JFrame();
frame.setSize(Toolkit.getDefaultToolkit().getScreenSize());
frame.setVisible(true);
You can get full screen size of any device by "Toolkit.getDefaultToolkit().getScreenSize()" in java. Above code I set frame size to fullscreen.
int height = Toolkit.getDefaultToolkit().getScreenSize().height;
int width = Toolkit.getDefaultToolkit().getScreenSize().width;
You can get hight and width of screen to your code by using above codes. I think this will be a help.
You can easily call
setExtendedState(MAXIMIZED_BOTH); on jframe or
use bellow code to set screen size to any PC.
//size of the screen
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
//height of the task bar
Insets scnMax = Toolkit.getDefaultToolkit().getScreenInsets(getGraphicsConfiguration());
int taskBarSize = scnMax.bottom;
//available size of the screen
setSize(screenSize.width, screenSize.height - taskBarSize);
setLocation(screenSize.width - getWidth(), screenSize.height - taskBarSize - getHeight());
if u want you can remove taskbar size to get full screen anyway this is the code and this will help you.
Try setting image as a background to you JFrame. So it will adjust with frame size
How to set Jframe Background Image in GroupLayout Java
so even in full screen it will be adjusting..
if you use panel then you can resize according to panel, it shows in full panel size
yourinternalframe.setSize(mainPanel.getSize());
yourinternalframe.show();
this may be not seem as your real need, you may do something according to this
I took a look at the code that you attached for FormTTS.java, what I found out is that your screen was set as using the absolute layout hardcoded to some numbers of pixels.
Look at the following code:
Menu.getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
Menu.getContentPane().add(jPanel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(420, 230, 530, 320));
Your JFrame is not using the GridBagLayout, instead it's using AbsoluteLayout from Netbeans library. So I guess you generated these UI codes with the tools from Netbeans.
And then regarding your picture that does not fill all the screen when maximized:
jLabel9.setIcon(new javax.swing.ImageIcon(getClass().getResource("/freetts/equations.png")));
Menu.getContentPane().add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 1530, 990));
Same problem here, it's hardcoded to some numbers of pixels.
If you want everything to be centered when you maximized your screen, I think the only way to do is to use the gridbag layout for your JFrame and this requires you to update almost everything in your code. And you will need to fully understand how GridBagLayout works. Here is the place to start.
However if you only want the background image to fill the screen you can follow the steps here to let the picture scaled to fill the size of JLabel:
Resize a picture to fit a JLabel
If it still doesn't work, you should also get the size of the screen (from one of the answers here) and then set the prefferedSize of the JLabel with those values in addition of scaling the image.
To add on to #eitanfar's answer, the best way of enabling fullscreen in Java is using the FSEM (FullScreen Exclusive Mode) API.
As he stated, this is achieved by setting the windows as fullscreen on the GraphicsDevice you want the window to appear fullscreen on, usually the default one. Even if your device does not support FSEM (id est isFullscreenSupported() returns false), setting the window as fullscreen will still partially work as the API will emulate fullscreen. The only safety check is to verify whether the GraphicsEnvironment is headless (isHeadless()). If it is, then there are no devices to display to.
The advantage FSEM gives you is that all graphics processing is run on the GPU (the GraphicsDevice is the GPU, not the monitor), therefore making it faster on most systems. In your program's options, you can allow the user to choose to enable or not FSEM so that they can run at optimal performance.
However, the system's repaint events are undefined when in FSEM, you're better off using active rendering, therefore you're better off ignoring repaint (setIgnoreRepaint(true)) and then using a custom thread for drawing.
I am having a similar problem with my application. the nearest I have come is to set all components that reside on top to either component.setOpaque(false), or component.setBackground(new Color(255,255,255,0)). you could also try panel.setVisible(false) for the unused panels.
its hard to offer up code with out the entire program but give this a whirl:
Menu.setBackground(new Color(255,255,255,0);
How can I elongate sliders length and also the width if possible. Most importantly, the length.
Below is just pieces of codework on Jsliders.
slider = new JSlider(0,180);
slider.setMajorTickSpacing(30);
slider.setMinorTickSpacing(15);
slider.setPaintTicks(true);
slider.setPaintLabels(true);
slider.setValue(0);
JPanel panel = new JPanel();
panel.add(slider);
I tried doing slider.setSize(100,100); something like that, but that does not seems to be working.
Any suggestions?
p.s (I was also wondering if the color encoded on the slider bar can be filled in with a different color like cyan, and if it exceeds some value, possibly change to red. Right now it is set to default, it gets filled with light blue.)
To change the size of a Swing component you use setPreferredSize(), not setSize(). The layout manager will either use the "suggested" preferred size or ignore it.
Some LAF will use the UIManager to determine the colors used for painting components. If you want to change the color for all JSliders then check out the UIManager Defaults for a list of properties you might be able to change.