I need to build a GUI in java with the following format:
Without providing any large blocks of code I want to know how to get started.
After reading countless info on swing I am utterly confused. I'm wondering if I would need multiple Jpanels / frames or a gridlayout or flowlayout. Also a very general idea of how to get started on the GUI would help. I know what components I need (list, label etc..) and have example code to help. The data I will load myself.
I'm assuming I will need a separate class for each component and maybe only one for labels to make it easier to debug.
You will need to mix containers and layouts, knowing what properties different layouts have, and different parts of those layouts, and the different options of the layouts will help a lot in knowing how to nest components together to get what you want.
Without providing code that shows this, I would do the following container/layout nesting:
The root panel could have a BorderLayout, with two JPanels on the east and west sides of it.
West most JPanel could have a BorderLayout, with the JList being in the center so it expands and takes up as much room as possible, and the 4 labels and 4 text fields could be in another panel which has a GridLayout of 4 rows by 2 columns.
The far east JPanel could have a BorderLayout as well, with a JPanel in the north that has a GridLayout of 3 rows by 1 column, where each of those rows has another panel of a FlowLayout. The text area or whatever it is could be in a JPanel in the center position, and you could place the bottom text field and label in a JPanel with a FlowLayout in the south position of the far east JPanel.
Related
I'm relatively new to developing GUI's within java so this may well be a stupid question or quite simply not possible to achieve but here we go.
I've created 1 single JPanel with no border layout set up or anything like that and I intended to paint a GUI on top of it using the graphics class. The JPanel is just plain black and then I've drawn a huge box over it leaving the black just as a border, and painted the whole GUI within this white box.
I want to add buttons within that white box GUI as well but I've no idea how. In fact they don't even have to be traditional buttons JButtons, if I could just draw a shape and have that act as a button then add an event handler to just that shape that would work also but I don't know how I'd do that either.
I have so much code for my whole program (it's a school coursework project) that I'm not sure which parts would even be worth sharing to assist with this question since there's so many GUI aspects I've already drawn so I've tried to just explain my issue in words.
Honestly I have no clue what I'm doing so any help would be appreciated.
EDIT: Here's a screenshot of my current GUI with a 'sketch' of how and where I'd like to be able to add buttons.
GUI Image
As with any suitably complex UI, you need to start by breaking it down into manageable chunks, focusing on areas of mutual interaction and functionality.
For example...
Says to me that you have two primary UI elements, the left and the right.
This could easily be established with a GridLayout, but, if the two sides are not equal in width, a GridBagLayout might be more appropriate
The right side to me says simply, JTable. You could place this within a container using a BorderLayout, allowing the table to occupy the CENTER position.
The key information would then a component laid out with either a GridLayout (top and bottom) or a GridBagLayout if the requirements are more complex. This component would then be added to the SOUTH position of the BorderLayout.
Again, this is pretty simple. The primary layout would probably be a BoderLayout, with the title in the NORTH position, the graph in the CENTER and the buttons wrapped in a component in the SOUTH.
You could use either a FlowLayout or GridBagLayout to layout the buttons depending on your how you want them to appear
Recommendations
Have a look at:
Laying Out Components Within a Container
How to Use Tables
And for the "border", I'd recommend you have a look at LineBorder. Take a look at How to use Borders more details
Sorry for not putting specific code in previous post. I realized I was doing something wrong, but now am having another problem. I am trying to place a JPanel at a specific place on a JFrame after clicking a button but nothing is happening, here is my code for my Actionperformed method.
public void actionPerformed(ActionEvent e)
{
JPanel pa=new JPanel();
frame.getContentPane().setLayout(null);
pa.setBackground(Color.yellow);
pa.setLocation(50,150);
pa.setSize(150,100);
pa.setBounds(50,150,150,100);
pa.setOpaque(true);
frame.add(pa);
}
There are many ways you can do this.
Use setLocation(x, y) and setSize(length, width)
Use setBounds(x, y, length, width)
If 1 and 2 don't work, look into layout Managers:
I highly recommend that you take a look at the Layout Managers JavaDoc. below are links for the each one, and the ones I would recommend for your case have a asterisk(*) next to them.
BorderLayout* is for general directions(NORTH, SOUTH, EAST, WEST)
BoxLayout puts components in a single row or column. It respects the components' requested maximum sizes and also lets you align components.
CardLayout lets you implement an area that contains different components at different times, often controlled by a combo box, with the state of the combo box determining which panel (group of components) the CardLayout displays.
FlowLayout is the default layout manager for every JPanel. It simply lays out components in a single row.
GridBagLayout is a sophisticated, flexible layout manager. It aligns components by placing them within a grid of cells, allowing components to span more than one cell. The rows in the grid can have different heights, and grid columns can have different widths.
GridLayout* simply makes a bunch of components equal in size and displays them in the requested number of rows and columns.
GroupLayout is a layout manager that was developed for use by GUI builder tools, but it can also be used manually. GroupLayout works with the horizontal and vertical layouts separately. The layout is defined for each dimension independently. Consequently, however, each component needs to be defined twice in the layout.
SpringLayout is a flexible layout manager designed for use by GUI builders. It lets you specify precise relationships between the edges of components under its control. For example, you might define that the left edge of one component is a certain distance (which can be dynamically calculated) from the right edge of a second component. SpringLayout lays out the children of its associated container according to a set of constraints.
Disclaimer: Most of these are direct quotes for the Oracle JavaDoc regarding Layout Managers.
I have a JPrame and it it set to borderLayout. Then i created another JPnael which uses BoxLayout. Now i have added labels(which contain text) and textFields to the JPanel. I have also setLayout of JPanel to WEST. Now i dont know why, but I have two problems with what is being displayed.
1) The labels are all indented 4-5 tab spaces, this is so random I dont even know why it is tabbed. All the labels in this panel are like this. Please note that this only happens if I add more stuff to the JPanel. If I only have 1 label in the JPanel it is correctly alligned to the west corner of the screen. Does anyone know why they get indented when i add more elements to the JPanel?
2)The Textfields size are huge, as in they take up many lines. I have set the size of the text fields only to 20. Yet they take up like 5 lines. Why is it not just a single line?
Im sorry guys, I have been trying to fix this for the past 2 hours and dont know what is causing this issue. I would post code, but this is an assignment and I dont feel comfortable posting it on to the internet. I hope you understand.
Just to make things clear, I have a JPanel for example called "aPanel" which is set to BorderLayout then I have another JPanel called 'subPanel' which uses boxlayout and set the layout of that to be west. After this I add stuff to this 'subPanel' expecting the elements to stack over eacher other towards the left border of the JFrame.
1) Read the section from the Swing tutorial on Fixing Alignment Problems. When you add different components to the panel they may have different "X alignment" so you get a different layout than you expect.
2) The box layout will expand to fill the available space. So you need to override the getMaximumSize() method of you text fields to return the preferred height of the text field and the maxiumum width of the text field.
Edit:
Just read the last part where you mention a FlowLayout. If this is what you are using then read the FlowLayout API. It has a parameter that controls left/center/right alignment of components added to the panel.
I'm having trouble organizing the contents of a JPanel. I've tried different layout managers but haven't gotten anything to work.
The JPanel contains 3 other JPanels and should look like this:
Does anyone know how I can achieve this layout?
Thanks in advance.
What you want is similar to a BorderLayout, but using one natively will cause the second pane to fill the bottom space (rather than the 3rd pane.)
However, you could use two in tandem to get this effect - The first BorderLayout pane would contain the second BorderLayout pane (in its centre) and panel 3 (on its east.)
In the second BorderLayout pane, you could then just set pane 1 to be the centre, and pane 2 to be the south.
Simplest, but not best solution, is to use null Layout Manager with manually set components bounds. It is called Absolute Positioning
Other approach is to use horizontal SplitPane with nested Vertical SplitPane: left one will contain vertical splitPane with Panel1 and Panel2, and right will be just Panel3.
For a project I have an almost working code, but I do not have the GUI. I want to make a screen that consists of clickable labels and has the following design:
I was thinking about first making the middle GridBagLayout with with a dimension of 6 by 2. Then 'wrapping' that up and adding the two buttons to the side, and then 'wrapping' that and adding the two buttons below.
I am inexperienced with Swing, and I have no idea how to start. I hope someone can give me some hints in the right direction.
Several approaches to a very similar layout using GridBagLayout and/or nesting are shown here. Consider using JButton for each clickable area, rather than JLabel. If you go with a nested layout,
Use BorderLayout for the enclosing panel.
Add buttons to EAST and WEST for the leftmost and rightmost areas.
Add a GridLayout(1, 2) of buttons to SOUTH for the bottom row.
Add a GridLayout(2, 6) of buttons to CENTER for the central twelve areas.
Addendum: A critical issue will be what you want the resize behavior to be.
as said in the comments above you could (should ?) use the WYSIWYG Window Builder plugin available for Eclipse; it's simple to use.
However, that doesn't answer your question, so to do so, here is how I would structure the UI if I were to make one like that :
http://www.hostingpics.net/viewer.php?id=902716gZHkK26.jpg
I basically use BoxLayout because that's the one I'm most familiar with. Every Rectangle is a JPanel. I think the image is pretty self eplanatory.