I would like to make an option dialog in my application. In this dialog I want to make kind of Areas surrounded with a border and with a title.
An example of what I want is in Firefox:
How can I do something like that in Java?
Here you can find all informations you need.
Basically you can use border factory to create a Border using types available in Swing:
Border lineBorder = BorderFactory.createLineBorder(Color.black);
JPanel panel = new JPanel();
panel.setBorder(lineBorder);
You can also define your custom borders implementing Border interface.
Related
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 making a simple swing application and I want to add some titled borders to my components. The border on both of my JScrollPanes work fine, but the JTextField and the JButtons don't. Allow me to share some screen shots.
I just have simple code for this. i.e
TitledBorder border = new TitledBorder("Border");
convert.setBorder(border); //convert is the JButton
I don't see why it would not work for one thing, and work for the other. Can anyone help me out?
A JTextField and JButton both use a Border already. So the titled border works but it changes the appearance of the component because you lose the default Border.
I also agree that normally you don't use a TitledBorder for an individual component but I suppose you could try to use a CompoundBorder to see if it looks any better:
CompoundBorder border = new CompoundBorder(titledBorder, button.getBorder());
button.setBorder( border );
but then the problem with the above approach is that you lose dynamic repainting of the border when you press/release the mouse on the button.
I have a JPanel of null layout called MainPanel. Onclick of a button I am adding JTextpane on mainPanel. On first click I am creating a textpane of background color white. On second click I am creating another textpane of color blue. What I want is to place the blue textpane upon the white textpane, but the blue textpane is going behind the white textpane. How can I place it on white pane?
Code is very simple here. Onclick I am creating a new JTextpane, setting dimensions to it and placing it on the mainPanel.
Placing a sample screenshot which describes the issue better. Here the blue textpane has gone behind white textpane. I want it above white texpane. How do I do that?
If you want to stick with your current Components, (I think you should use kellax's solution, but I don't know if there's an extra requirement that's forcing you to use your current approach) you can look into Container.setComponentZOrder(Component comp, int index) to directly determine the order in which Components are displayed.
You will have to replace your JPanel "MainPanel" with a LayeredPanel.
Then you can say:
JLayeredPane mainPanel = new JLayeredPane();
JTextPane whitePane = new JTextPane("White text pane on top");
JTextPane bluePane = new JTextPane("Blue text pane behind");
mainPanel.add(whitePane, 2, 0);
mainPanel.add(bluePane, 1, 0);
Edit:
You can read more about the LayeredPane here: LayeredPane
I'm building a GUI with several JPanels and with a BoxLayout with Y-axis. I also use borders with titles around the JPanels. I wonder if it's possible to control the space between the border of the JPanel and the window frame? I'm also wondering if it's possible to justify the content inside the JPanels to the left or right? Thanks!
You could add an EmptyBorder using CompoundBorder to wrap both, the current border and the emtpy one. The empty border would not be rendered but allow you to define the margin to the frame using insets.
The justification of the panel's content would depend on the layout manager used there. Do you have a BoxLayout inside the panel as well?
Edit:
A really good and easy to use layout manager is MigLayout. It allows defining custom insets, alignment etc. inline and using just a single layoutmanager.
I am not good with GUIs or User Interfaces in Java.
Would a Border or JPanel be good for something like the image below?
So, what would be the best option for me? Thank you.
Read the section from the Swing tutorial on Using Layout Managers. You can easily nest panels to get the desired effect.
Maybe start with a BorderLayout. Then you can add a panel that uses a GridLayout, which contains all your image tiles, to the CENTER of the BorderLayout. Then you can add the scrollpane containing the text area to the SOUTH. Then you can create another panel to add to the EAST.
Be creative and experiment.
You can make 4 seperate panels for a border, using BorderLayout.NORTH,BorderLayout.EAST,BorderLayout.SOUTH,and BorderLayout.WEST, This is the easiest way in my opinion.
By the way, in the top right of your picture, where you wanted the information panel, you should put an information LABEL (JLabel) instead, because they hold text. JLabel topRight = new JLabel(); then set the text, position, etc.
p.s. to erase the borders around every tile (if you want to do so), use setBorderPainted(false).