Hi guys/girls i am trying to make a box which will take a user input of a number. Now i have made a text field but how do i change the size of the text feild.
This is what i got so far
public class example extends JFrame {
private JTextField example1;
example() {
box1 = new JTextField(10);
add(example1);
Now its just iam trying 2 get like a square size text feild which will allow the user to write a number in there. It will be a one diget number. Any help would be great. Thanks
You state:
iam trying to make a caluclater. So i am using the box to hold a number.
Most calculators that I know of don't have a small field for displaying the current number but rather they have a text component that covers the entire width of the top of their GUI, and the key to achieving this with Java Swing is in using the right layout managers and the right placement of your components. For instance, this GUI design could be solved by using a container that uses BorderLayout, and placing your display JTextField (not JTextArea) in the BorderLayout.PAGE_START position and the buttons in another JPanel held in the main container's BorderLayout.CENTER position.
For example, please look at the code in my answer here. It creates a GUI that looks like so: . This shows two calculators that only differ based on the size of fonts chosen.
Key code for this is:
textField.setFont(textField.getFont().deriveFont(BTN_FONT_SIZE));
mainPanel.setLayout(new BorderLayout(gap, gap));
mainPanel.setBorder(BorderFactory.createEmptyBorder(gap, gap, gap, gap));
mainPanel.add(textField, BorderLayout.PAGE_START);
mainPanel.add(buttonPanel, BorderLayout.CENTER);
JTextField uses the number of characters(# of columns) rather than a pixel size. If you're looking for a text box which you can specify the size of, use JTextArea.
Related
So firstly, I added the text fields and their labels into separate panels as I wanted them to be in line with each other, I added them via Flow layout;
//add them in Flow Layout
panel.add(label_1);panel.add(file_name);
pane2.add(label_2);pane2.add(h_link);
pane3.add(label_3);pane3.add(pages);
What i wanted to do next is to add them using BorderLayout in a way that all of them will be in the center of the window. Additionally, I wanted to add submit button of the details;
frame.getContentPane().add(BorderLayout.CENTER, panel);
frame.getContentPane().add(BorderLayout.CENTER, pane2);
frame.getContentPane().add(BorderLayout.CENTER, pane3);
frame.getContentPane().add(BorderLayout.SOUTH, submit);
frame.setVisible(true);
As I am writing this post, I did not achived the result that i wanted , the screenshot attached shows the resulting frame which is not I wanted.
By all means, if you have also any suggestions for how to do it more efficiently please do share.
The key to solving this is to nest JPanels, each using its own layout, thereby allowing you to effectively nest layouts:
Place a JPanel in the BorderLayout.CENTER position
Give the JPanel a GridLayout, one allowing multiple rows and one column, e.g., new GridLayout(0, 1)
Add your JTextFields to the JPanel
Another approach is to simply use a JTable, one with a single column and multiple rows.
Another approach -- if you're trying to gain input from a user in a JLabel/JTextField grid -- is to use GridBagLayout to space the components nicely together
Im new in Java Swing, and want to make my layout, but can't do this
Look Now :
Look I want :
Code Now :
JPanel MainPanel = new JPanel(new GridBagLayout());
JLabel MoneyLabel = new JLabel(MoneyIcon);
MoneyLabel.setHorizontalTextPosition(JLabel.CENTER);
MoneyLabel.setVerticalTextPosition(JLabel.BOTTOM);
MoneyLabel.setText("Money:" + CarMain.Money);
JLabel MoneyClicksLabel = new JLabel();
MoneyClicksLabel.setHorizontalTextPosition(JLabel.CENTER);
MoneyClicksLabel.setVerticalTextPosition(JLabel.BOTTOM);
MoneyClicksLabel.setText("Money Clicks: " + CarMain.MoneyClicks);
JLabel BoxesLabel = new JLabel(BoxLv9_10Icon);
BoxesLabel.setHorizontalTextPosition(JLabel.CENTER);
BoxesLabel.setVerticalTextPosition(JLabel.BOTTOM);
BoxesLabel.setText("Boxes: " + CarMain.Boxes);
JLabel BoxesClicksLabel = new JLabel();
BoxesClicksLabel.setHorizontalTextPosition(JLabel.CENTER);
BoxesClicksLabel.setVerticalTextPosition(JLabel.BOTTOM);
BoxesClicksLabel.setText("Boxes Clicks: " + CarMain.BoxesClicks);
MainPanel.add(MoneyLabel);
MainPanel.add(MoneyClicksLabel);
MainPanel.add(jbtnMoney);
MainPanel.add(BoxesLabel);
MainPanel.add(BoxesClicksLabel);
MainPanel.add(jbtnBoxes);
This is simple example of, what i want, becouse i'm building ingame shop, with 13 labels like these, in each tabbedpane window. How can i make it look, like in second picture, what I want?
Im new in Java Swing, and want to make my layout, but can't do this
Probably no single layout can suit everyone's needs. But combining several layouts can usually handle most scenarios.
From the image you showed in the question. There is no need to write your own layout. You can always use sub panels to hold your components and set a specific layout for each sub panel to handle what you need for those individual areas.
The reason for the alignment in your first attached image is because:
JPanel uses FlowLayout as its default layout. Hence all the components added will appear in a linear fashion and tries to fill up the row as much as possible the panel's width can hold. Once exceeded the panel's width, the components will be pushed to the next row.
If you want to achieve the alignment in the second attached image:
You may create a main panel to contain several sub-panels (see image below).
The red box is your main panel and you may continue to use the default FlowLayout.
Then add your components into sub-panels (orange boxes) before adding it to the main. You may then use BoxLayout, FlowLayout or even GridBagLayout for the sub panels (orange boxes).
Artis Uljanovs, at night after work i will give a look at this to help you.
I recommend you already to read the following: https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
You need some foundations on Java Layouts.
I am new on using Swings
my requirement is to align components within the jpanel(panel2)
I have taken 2 JPanels (panel1, panel2) and added to the jframe
panel1.add(panel2);
panel2.setLayout(new flowLayout());
panel2.setBounds(80,120,100,100);
getContentPane() .add(Panel1);
and I have created a "Create" button that will generate text area dynamically in panel2
now my problem is if the created textarea is reaching out of panel2 it has to show an error
message "You reached the boundaries of the jpanel so the textarea cant be created "
Thanks in advance
Set the layout before you add any elements to the panel. No not call setBounds as with layout manager present it likely has no effect at all.
I cannot explain how to layout your elements as from your question seems not possible to figure out that do you want to do. Best, post the drawing with elements as they should look like. GridLayout maybe would be good if you want to align multiple elements as in the table.
I'm trying to create a JDialog like the Symbol dialog in Microsoft Word that you get by choosing Symbol... from the Insert menu. Basically, it's an n x m (n and m are not known until runtime) grid of small buttons. I've got a first version of this working nicely using a GridLayout. The problem is that when you resize the dialog (and there is a requirement that you should be able to resize it), the size of the buttons changes. I need the size of the buttons to remain constant.
But I want the dimensions of the grid containing the buttons to change. For example, if the dialog gets wider, but stays the same height, the number of rows should lessen, while the number of columns increases.
I've thought of a couple of ways to fix this:
When the dialog is resized, create a new GridLayout and repopulate it with the buttons. I'm going to try this and see how it looks, but it seems like a clumsy way of doing it.
Use some other type of layout such as a FlowLayout. I took a stab at this, but it put all n x m buttons in one row. I do not want to use horizontal scroll-bars and the buttons ran off the right edge. Anyway, it's supposed to be a 2-dimensional grid of buttons.
What is the best way to solve this layout problem?
Create a buttons panel with GridLayout and set a fixed size (could be calculated at runtime of course) to it. The buttons panel should be contained in a panel of BoxLayout.
Check out the BoxLayout Tutorial
Very Very basic example:
public static void main(String[] args) throws Exception
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel buttonPanel = new JPanel();
JPanel containerPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(2,2));
buttonPanel.add(new JButton("1"));
buttonPanel.add(new JButton("2"));
buttonPanel.add(new JButton("3"));
buttonPanel.add(new JButton("4"));
buttonPanel.setPreferredSize(new Dimension(300, 400));
containerPanel.add(buttonPanel);
frame.getContentPane().add(containerPanel);
frame.pack();
frame.setVisible(true);
}
if the dialog gets wider, but stays the same height, the number of rows should lessen, while the number of columns increases.
Wrap Layout might be what you are looking for.
I had a similar issue with a single column of buttons, and found that MiGLayout (third-party, available here) was simple and effective for this. It helped both with making a grid and with setting button sizes, although it took me a day or two to get used to its syntax.
But the key is really setting button sizes; GridLayout certainly seems like the way to go for a layout that is, well, a grid. I haven't tested, but I suspect that the built-in setXSize() methods would work just as well. The GridBagLayout tutorial has examples of some things you can do with sizing/positioning.
FlowLayout would be the way to go but you might have some configuration problems. What layout manager does the parent component use?
Is it possible to add a JLabel on top of another JLabel? Thanks.
The short answer is yes, as a JLabel is a Container, so it can accept a Component (a JLabel is a subclass of Component) to add into the JLabel by using the add method:
JLabel outsideLabel = new JLabel("Hello");
JLabel insideLabel = new JLabel("World");
outsideLabel.add(insideLabel);
In the above code, the insideLabel is added to the outsideLabel.
However, visually, a label with the text "Hello" shows up, so one cannot really see the label that is contained within the label.
So, the question comes down what one really wants to accomplish by adding a label on top of another label.
Edit:
From the comments:
well, what i wanted to do was first,
read a certain fraction from a file,
then display that fraction in a
jlabel. what i thought of was to
divide the fraction into 3 parts, then
use a label for each of the three.
then second, i want to be able to drag
the fraction, so i thought i could use
another jlabel, and place the 3'mini
jlabels' over the big jlabel. i don't
know if this will work though..:|
It sounds like one should look into how to use layout managers in Java.
A good place to start would be Using Layout Managers and A Visual Guide to Layout Managers, both from The Java Tutorials.
It sounds like a GridLayout could be one option to accomplish the task.
JPanel p = new JPanel(new GridLayout(0, 1));
p.add(new JLabel("One"));
p.add(new JLabel("Two"));
p.add(new JLabel("Three"));
In the above example, the JPanel is made to use a GridLayout as the layout manager, and is told to make a row of JLabels.
The answer to your original question is yes for the reasons given that any Component can be added to a Container.
The reason you don't see the second label is because by default a JLabel uses a null layout manager and the size of the second label is (0, 0) so there is nothing to paint. So all you need to do is set the bounds of the second label and away you go.
You can't use a layout manager if you want to drag components around because as soon as you resize the frame etc, the layout manager will be invoked and the components will be repositioned based on the layout manager of the component.
it's a matter of layout.
you can do that using null layout (with hard coded locations) or with a custom layout.
you can use a JLayeredPane and set it's border to No Border.
you can add put them above each others by using the horizontal or vertical gap (hgap,vgap) the attributes of the layout
JPanel p = new JPanel(new GridLayout(2, 1,-40,0));
//the 40 is the hgap , make it the same with the label height .