I have a JTextField that I'm trying to get to automatically view from the Right (Not align to the right), so if the text is to long for the JTextField it will display the last characters in the String instead of the beginning.
Ive been searching for ages trying to locate an answer but keep coming up with aligning.
The 2 images below show what i get and what I'm after, the text is "123456789_123", the JTextField is only big enough to contain the "123456789" but i want to see the "56789_123" instead without having to focus on the field. (i can use something other than a JTextField if needed, tried a JTextArea but had the same issue).
What i Get
What I'm after
I can not just make the Field bigger as I'm restricted by other Objects in my program. Usually the text fits fine but every now and then its too big.
Found a work around.
you create a JScrollPane, make its vertical and horizontal bars invisible by setting there dimensions to 0,0. then scroll to end using 'setValue' to max.
hope this helps anyone trying to do something similar.
JTextArea editArea2 = new JTextArea(5,5);
editArea2.setText("123456789_12345");
editArea2.setAlignmentX(CENTER_ALIGNMENT);
asd = new Dimension();
asd.height = 20;
asd.width = 70;
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView(editArea2);
scrollPane.setMinimumSize(asd);
scrollPane.setMaximumSize(asd);
scrollPane.getVerticalScrollBar().setPreferredSize (new Dimension(0,0));
scrollPane.getHorizontalScrollBar().setPreferredSize (new Dimension(0,0));
scrollPane.getVerticalScrollBar().setMinimumSize(new Dimension(0, 0));
scrollPane.getVerticalScrollBar().setMinimumSize(new Dimension(0, 0));
scrollPane.getVerticalScrollBar().setMaximumSize(new Dimension(0, 0));
scrollPane.getVerticalScrollBar().setMaximumSize(new Dimension(0, 0));
scrollPane.getHorizontalScrollBar().setValue( scrollPane.getHorizontalScrollBar().getMaximum() );
Related
I'm trying to fix the height of the "amountField" text field, but I can't.
I would like the height of amountField to have the same height as the JComboBox that it's above, so it looks better.
Right now, the JTextField looks very tall compared with the rest of design.
I've tried everything that I've read in this forum, but nothing seems to work.
I don't know if it's relevant, but this whole JPanel (WithdrawalScreen) is inside another JPanel with BorderLayout. This panel is the center part of it
Thanks
PictureHere
public class WithdrawalScreen extends JPanel {
Public JPanel init() {
this.setLayout(new GridLayout(0,1));
account = new JLabel("account");
accountSelect = new JComboBox(labels);
amount = new JLabel("amount");
amountField = new JTextField("");
submit = new JButton("SUBMIT");
this.add(account);
this.add(accountSelect);
this.add(amount);
this.add(amountField);
this.add(submit);
return this;
}
}
Try creating the Grid Layout with 5 rows and 1 column. I think the height is messed up because you are not setting the constructor arguments properly.
new GridLayout(5,1);
Grid layout will stretch the component and give the same size to all of its components. In order to keep the "default" size of each component, you can use BoxLayout with BoxLayout.Y_AXIS parameter in its constructor. Another way would be to use a dummy-nested JPanel with another layout. Let's say FlowLayout.
JTextField textField = new JTextField(10);
JPanel nestedPanel = new JPanel(new FlowLayout());
nestedPanel.add(textField);
gridLayoutPanel.add(nestedPanel);
JTextField will not be stretched. nestedPanel will be. Do some experiments yourself and you will find the way that fits your needs.
A link that will help you: A visoual guide to Layout Managers.
I have this simple program where I upload images into a java db database (just add their location to the data base as well as their type) then I show them in the program in a scrollpane the problem is the scrollpane isn't scrolling down,
this is my code
`
JPanel cont = new JPanel();
cont.setPreferredSize(new Dimension());
cont.setLayout(new FlowLayout(FlowLayout.LEFT));
while(sports_re.next()){
String location=sports_re.getString("LOCATIONN");
for(int i=0;i<100;i++){
JLabel picLabe = new JLabel();
picLabe.setIcon(new ImageIcon(new
ImageIcon(location).getImage().getScaledInstance(100, 100,
Image.SCALE_SMOOTH)));
cont.add(picLabe);
}
}
jScrollPane1.getViewport().setView(cont);
`
I tried to add width and height to the dimension and it worked but I have to put a huge number for it to work (if I put a small number it won't allow me to scroll ) It's not right
this is what it looks like without the "cont.setPreferredSize(new Dimension());"
I want to make it dynamic, please help, thank you
cont.setPreferredSize(new Dimension());
Remove this to let LayoutManager calculate the size. TGhen scrollpane reflect the calculated size rather than fixed you provided.
Please consider this Java code fragment:
Box buttonBox = Box.createHorizontalBox();
buttonBox.add(new JCheckBox("Select all"));
buttonBox.add(Box.createHorizontalGlue());
buttonBox.add(new JLabel("Filter: "));
buttonBox.add(new JTextField());
Box paneBox = Box.createVerticalBox();
paneBox.add(buttonBox);
paneBox.add(new JScrollPane(jList));
For some reason i don't get, the JTextField takes up most of the screen. The jList isn't even visible anymore. I would like to know why and how to fix it.
When i comment the line with the JTextField, it looks fine (except no JTextField, of course). Why does the JCheckBox and the JLabel not get ridiculously big? What is the purpose of a one-line JTextField being able to take up almost the whole screen?
Most people suggest to set the size of the JTextField field to my needs. However, i read that i should not call these methods, but let the LayoutManager take care of it. Now what is the most elegant solution to prevent the JTextField from becoming so big?
Try using the setPreferredSize method like this:
textFieldName.setPreferredSize(new Dimension(x, y));
Obviously input the values for x and y that you wish the JTextField size to be
You can avoid this problem by using a simple JPanel() instead of a Box for the horinzontal one. And after just set the prefered size to your JTextField.
//Box buttonBox = Box.createHorizontalBox();
JPanel buttonBox = new JPanel();
buttonBox.add(new JCheckBox("Select all"));
buttonBox.add(Box.createHorizontalGlue());
buttonBox.add(new JLabel("Filter: "));
JTextField jt = new JTextField();
jt.setPreferredSize(new Dimension(100,25));
buttonBox.add(jt);
EDIT
Btw the defaut Layout of a JPanel is the FlowLayout which place components from left to right and with centered alignement by default. You can still change the alignement.
For example:
JPanel buttonBox = new JPanel(new FlowLayout(FlowLayout.LEFT));
You can also change the gap between each components. There is some other Layout like the BorderLayout or GridLayout / GridBagLayout. I let you search on Google for more information about them.
See in yellow the JPanel, in red the VerticalBox
Box box = Box.CreateVerticalBox();
Use box.setMaximumSize(Dimension(160,80))
and box.setMinimumSize(Dimension(160,80))
then it will work :D
If you need a strut for your layout, put it in between boxes.
So I've been learning Java for the very first time and it's time for me to attempt my first project. And I'm stuck at the "first hurdle" haha.
The issue I have is the fact that I don't actually know how to space J Items apart.
I have a 250,350 window for a Log In form with a JLabel, a JTextField for username and JLabel JPassword for Password with a JButton at the bottom.
What I want to do now is style it so that the spacing between the top and the bottom of the form makes it so that the form is centered as well as adding a line's height space between the JLabel and the JTextField. (Basically a \n type deal but that isn't working.)
Hopefully this makes sense, if not, I apologise and I'll try to rephrase/add code!
public Game() {
this.setSize(250,350);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Sticket Cricket - Login");
JPanel loginMenuPanel = new JPanel();
loginButton = new JButton("Login");
usernameField = new JTextField();
usernameField.setColumns(10);
passwordField = new JPasswordField();
passwordField.setColumns(10);
passwordField.requestFocus();
usernameLabel = new JLabel("Username: ");
passwordLabel = new JLabel("Password: ");
this.add(loginMenuPanel);
loginMenuPanel.add(usernameLabel);
loginMenuPanel.add(usernameField);
loginMenuPanel.add(passwordLabel);
loginMenuPanel.add(passwordField);
loginMenuPanel.add(loginButton);
this.setVisible(true);
}
Short Answer:
Create a JPanel, set the layoutmanger of the panel (some examples, GridLayout, BorderLayout, Check out the tutorial here where more of these are explained)
Then add your components to this panel accordingly
For the layout you are looking for it would possibly be easier to use an IDE to create this, I find Net Beans to be the easiest for doing this.
My recommendation would be for you to create a JPanel with a grid layout of 2 columns and 2 rows, to this add you JLabels and Text fields for the logon name and password.
Then create another JPanel possibly BorderLayout or Flow Layout and add the above panel to this then add this parent panel to the frame.
I've a big problem with Swing in Java, I used BoxLayout for this but still it looks bad.
Any suggestions about my usage of layouts, or how to change it to look like in assumptions? (here are assumptions)
Container main = new Container();
Container left = new Container();// here goin buttons
Container right = new Container(); // here goin tabs + more buttons, textfields and other stuff
BoxLayout lewyL = new BoxLayout(left, BoxLayout.Y_AXIS);
left.setLayout(lewyL);
left.add(rastrowa); //radiobutton
left.add(wektorowa);//radiobutton
left.add(apDwuliniowa);//checkbox
left.add(wczytaj);//button
left.add(zapisz);//obutton
left.add(wyczysc);//button
BoxLayout prawyL = new BoxLayout(right, BoxLayout.Y_AXIS);
right.setLayout(prawyL);
right.add(zakladki);// tabs (mostly i use BoxLayout but for last one i need something more "complicated")
EDIT: I almost solve this problem, I need to move all elements to left (how it look like)but I have no idea how ;/ Here is constructor of this class.
JLabel label = new JLabel("O wektor");
JLabel labelA = new JLabel("a:");
JLabel labelB = new JLabel("b:");
JButton wykonaj = new JButton("Wykonaj");
JTextField a = new JTextField(5);
JTextField b = new JTextField(5);
add(label);
add(labelA);
add(a);
add(labelB);
add(b);
add(wykonaj);
There's nothing wrong with the way it looks (in my opinion), but if you want it to look a little better, why don't you convert the left panel (which is 6x1) into a 3x2 panel, with the checkboxes/radiobuttons on the left, and buttons on the right? Sounds like a job for GridLayout - one of my favorite classes...
JPanel leftPanel = new JPanel(new GridLayout(3,2));
leftPanel.add(rastrowa); //radiobutton
leftPanel.add(wczytaj); //button
leftPanel.add(wektorowa); //radiobutton
leftPanel.add(zapisz); //obutton
leftPanel.add(apDwuliniowa); //checkbox
leftPanel.add(wyczysc); //button
Note that the 3,2 defines the number of rows,columns. When adding panels, they are added to the grid from left-to-right, and top-to-bottom. GridLayout also auto-sizes the components, so all the buttons etc will be the same width and height, making it look more consistent.
The GridLayout documentation might be useful, and the Visual Guide to Layout Managers is a great place to see other layout managers that might work better for your different situations. I personally find BorderLayout and GridLayout to be the most useful, and cover about 95% of the situations I ever need for my GUIs.