What nested panel combination should I use to achieve this - java

It just involves a JFrame and a JComboBox object.
I want to have a JComboBox aligned onto the center of a frame and it must not stretch to fill the entire width.
If I directly add it onto the frame, it will expand and stretch. I think I need to add it to a panel and add that panel to the frame. But how should I add it to that panel?
If I use flow layout for the panel and add the JComboBox, it will appear to the top of the frame and not center. :(

You can use a GridBagLayout:
JComboBox comboBox = new JComboBox(...);
frame.setLayout( new GridBagLayout() );
frame.add(comboBox, new GridBagConstraints());

Related

How to add JLabel in a JPanel Vertically?

I'd like to add JLabels dynamically in a JPanel vertically like the image that I've attached. After loading all images, I need to select an image, then selected the image should appear in another JPanel. I am reading Images from an ArrayList which contains the paths.
I used Jpanel with GridLayout in a JScrollPane, but the result is not the same that I want.
This is the code that I've used to add Jlabels:
for(String file: files) {
JLabel JLabelPicture = new JLabel(new ImageIcon(file));
panel_images.add(JLabelPicture);
}
I used Jpanel with GridLayout in a JScrollPane, but the result is not the same that I want.
A GridLayout will allow you to display the components vertically. When you create your panel you just use:
JPanel imagePanel = new JPanel( new GridLayout(0, 1) );
This will resize all the images to the same size.
Another option is to use a vertical BoxLayout. In this case you can use:
Box imagePanel = Box.createVerticalBox();
In this case the images will retain their preferred size.
In both case you add to the panel to a scroll pane which is added to your frame:
frame.add( new JScrollPane( imagePanel ) );
Read the Swing tutorial on Layout Managers for more information and working examples of each layout.
Edit:
after listing the images I need to select one of them,
Well where was that requirement in your original question. The complete requirement should be defined in the question so all the information is in one place for everybody to see.
So I would suggest you should be using a JList to display an Icon. Read the section from the Swing tutorial on How to Use Lists for more information.

Putting a Panel on top of a JLabel backGround (Java)

I've been fruitlessly searching the internet and nothing that people suggest seems to have any effect for me.
I have a JFrame which I'm trying to put a JPanel in. That JPanel ideally would have a JLabel with an imageicon as the background and a set of buttons in its own Jpanel in the foreground. The issue is every type of layout manager I've seen suggested just does not work as advertised for me. The best I've gotten to work so far is this approach:
public MenuBackgroundPanel(AsteroidsFrame frame)
{
this.gameFrame = frame;
this.setLayout(new OverlayLayout(this));
ImageIcon image = new ImageIcon(getClass().getResource("/resources/background1.gif"));
imageLabel = new JLabel(image, JLabel.CENTER);
mp = new MainMenuPanel(gameFrame);
mp.setMaximumSize(new Dimension(300,200));
this.add(mp);
this.add(imageLabel);
this.setVisible(true);
}
Unfortunately, I'm getting really strange alignments and trying to set location on the background (to actually get it to start at the JFrame's (0,0) or moving the button panel just seems to have no effect. Printing the location of each object says they're both at (0,0) but the image I'll link shows this is just not the case. My point is, I've tried things like JLayeredPane or setting the JLabel as the contentpane of the Jframe and making it transparent but nothing seems to do anything. One or the other of the two objects just covers the other completely.
As you can see the objects are not at all aligned.
Could anyone help me with this?
That JPanel ideally would have a JLabel with an imageicon as the background and a set of buttons in its own Jpanel in the foreground
Easiest way for something like this when the child panel is fully contained in the label image is to just set the layout manager of the JLabel and then add your components to the label.
JLabel background = new JLabel( new ImageIcon(...) );
background.setLayout( new GridBagLayout() );
JPanel buttons = new JPanel();
buttons.setOpaque( false );
buttons.add(...);
background.add(buttons, new GridBagConstraints() );
Now the button panel will be centered on the label.
As you can see the objects are not at all aligned
If you want to use the OverlayLayout then you need to play with the alignmentX/Y properties of each component. You would probably want to set them both to .5. Check out: Java Layout with Component always in Top Right for an example of how changing these values can affect the layout.

Java how to display line number on the right side of text components

I want to display line numbers in a text component. I found this link
https://tips4java.wordpress.com/2009/05/23/text-component-line-number/
And it worked. But I want to display line number on the right side of textarea. How can I do it. Thank!
TextLineNumber is a Swing component. How do you display multiple components in a scroll pane? You add the components to a panel and then add the panel to the viewport of the scroll pane. One way might be to use a panel with a BorderLayout:
JPanel panel = new JPanel( new BorderLayout() );
panel.add(textArea, BorderLayout.CENTER);
TextLineNumber lineNumber = new TextLineNumber(textArea, 3);
panel.add(lineNumber, BorderLayout.EAST);
JScrollPane scrollPane = new JScrollPane( panel );
Or you could use the existing code and change the orientation of the scroll pane:
scrollPane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
The line number will be on the right, but the scrollbar will now be on the left.

JPanel Format Problems

I have a 2 JPanels, 1 a button Panel and one a Graphic Panel. I would like the button panel to situated right below the graphic panel but the button panel cuts off the Graphics Panel in the middle. I've been trying the box layout which seems from discussions seems like the best format for what I am trying to do. Can anyone please give me some advice on my formatting problem.
JFrame canvas = new JFrame("Baseball Strike K");
JFrame canvas = new JFrame ("GraphicBoard");
canvas.setVisible(true);
canvas.setSize(1000,1000);
canvas.setDefaultCloseOperation(EXIT_ON_CLOSE);
//create two panels
//add them to contentPane
//set Layout
JPanel buttonPanel = createButtons();
JPanel mainPanel = new Graphic(); //extends JPanel and writes the paint method
mainPanel.setSize(1000, 1000);
Container content = canvas.getContentPane();
content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
content.add(mainPanel);
content.add(buttonPanel);
mainPanel.setSize(1000, 1000);
The job of the layout manager is to determine the size of the component, so you would never invoke the setSize() method of a components.
Instead you give hints to the layout manager on what the size should be. You would do this by overriding the getPreferredSize() method to return an appropriate value. Also, I would pick a more reasonable size (1000, 1000) is a little big to display on most screens. If you really want your painting area this large then I would add the paint panel to a JScrollPane and then add the scrollpane to the frame.
Try getting your code to work using a BoxLayout. Then I would suggest a better layout manager would be to use a BorderLayout. Then you add the paint panel to the CENTER and the buttons to the SOUTH. Now as you resize the frame the paint panel will be adjusted in size.
canvas.setVisible(true);
Also, the placement of that line of code is wrong. You should add all your components to the frame first, before making the frame visible.

How to add scrollbar to panel?

I need help. I have one panel which can need to have width 1000px. I need to add lot of buttons with different size ( I add with flow layout and it works fine). Problem is that I have height on screen example 500px but when I add buttons panel has bigger size. How to add scrollbar to panel ?
Add your panel to scrollpane and add that pane where you are adding your panel instead of panel
JScrollPane jScrollPane = new JScrollPane(panel);

Categories

Resources