I am working on my homework assignment and I have to achieve the following layout. Can anyone guide me as to how to achieve the right side of the view? I have already coded the left part, it's just the right side that I don't know what to use?
Calendar GUI
Should I just use paintComponent or a JTable?
JTable doesn't seems to suit your needs. You can consider using an array of JTextArea which will be added into a JScrollPane.
In this case, you can make use of the existing behaviours from these JComponents, such as setting them as editable/non-editable. Auto scrolling for JTextArea. JTextArea also allows displaying of multiple lines of records.
Should I just use paintComponent
I supposed you meant by custom painting. Using custom painting will give you alot of freedom to do your own customizations, however if the current JComponents are able to fulfill you needs, then I think you shouldn't try to reinvent the wheel, especially when you need to deal with printing text. Aligning the text properly in custom painting could cost you alot more time than learning how to use various existing JComponents.
Related
I am a self-taught java developer and I use IntelliJ IDEA for Java. Recently I saw a video on youtube in which the guy was using NetBeans and in his JFrame form, he was able to freely place his objects like JButton, JTextFeild, JLabel, etc. I am not able to figure out how to do that. In IntelliJ IDEA I found several layout managers such as Border, Card, Grid, Bag, etc. but none of them gave the desired result. can somebody please tell me how can I get a layout manager in which I can freely place all my objects and also freely resize them without any restrictions? Thank you in advance for any help.
In swing every component extends Container. That means that every component can have nested components (they all have add(Component) method). However, not all of them support the layout-ing of nested components.
What I want to say is, that you can add a component to a JButton, but a JButton is not capable of showing its nested components.
So, in order to have nesting, we use the components - containers that support the orientation of their nested components.
These components are all windows (JFrame, JDialog, etc...) and JPanels. There some others that support layout-ing a specific type of nested components. For example a JMenu is capable of showing JMenuItems properly.
Now, these "top-level"/empty containers are using Layout Managers in order to align-show their nested components. Based on the container's layout manager, the components are shown.
This is why you can't "freely" place the components into a JFrame. Because its Layout Manager is taking care of the components will be placed. So, what you are looking for, is to change its LayoutManager (use setLayout method) to one that allows you to freely place the components.
Guess what? There is no such layout manager. Simply because, it would have nothing to do/calculate since you are taking care the layout of the components. So, in order to achieve the "free" component layout, you must use jframe.setLayout(null);. In order to layout the components after it, you will have to use componentInsideJFrame.setBounds(...) and give it constant coordinates /dimension.
This is bad practice. A very bad one when it comes to UI. Giving a component static coordinates and dimension is bad. There are some questions you have to ask yourself.
What if user resizes the window? If the window is 301x301, the center of it, is at (150,150). So you place a component at (150,150). Ok it works. Now user resizes the window and makes it 501x501. The center is now standing at (250,250). But the component is staying at (150,150). There is the solution of setResizable(false), to this kind of problems, but how often have you used "uncapable of resizing" applications? What if user wants to resize it?
I hope you get it and understood what I am trying to say.
By using layout managers, you are solving this kind of problems easily, since the layout manager will take care of the resize and calculate the new center.
Yes. I know it feels weird, but all these youtube tutorials are not teaching you the correct way to make Swing GUIs. (This is a conversation for another day, I guess)
I truly suggest you read the tutorials of Swing documentation in order to get some ideas of how layout managers work. You will really benefit from those.
Finally, I suggest you to leave outside the whole "gui-builder-tool" thing. They seem to help you building your GUI, but they are adding so much additional/useless code and most of the times they are "bad UI creation" prone. Try to code the GUI by yourself.
At first, this sounds a bit harsh, but you can always run your application and see the result of the GUI. After some mistakes, you will finally be able to imagine the GUI result by only seeing the container.setLayout(..) and container.add(...) lines.
I am trying to create a GUI for a program with an undefined number of rows but stay with two columns. Currently the only way I can get it to look how I want is using FlowLayout but the window of course needs to be smaller than desired. It needs to happen within one panel as well because it is being added to a tabbed pane.
What would be the best layout manager to solve the problem that is in the JDK? Or would just kind of brute forcing it with AbsoluteLayout be the best approach (as the user shouldn't really be resizing the window)?
I have attached the desired appearance.
Thanks for any help in advance.
Several layouts can do what you want. I'd suggest GridLayout (easy to use, but columns will be equal width) or GridBagLayout (harder to use, but you have lots of control). You could also use a BorderLayout and put all the fields in a sub-panel on the WEST and all the drop-downs in a sub-panel on the EAST. The difficulty with that is ensuring that the rows have the same height, since they won't be constrained by the layout itself.
The best thing to do would be to go through the Java tutorial on layouts and get up to speed on what the various layout managers can do.
Also, since you're using Swing, you could just use a JTable (as Gilbert Le Blanc suggests in his comment).
I'm programming this level creator for a game me and a few of my friends are doing but as of right now the GUI is using a null layout, which I don't want to. It works fine for now, but I'm still against it and I know everyone else also encourages you to ALWAYS use a LayoutManager. I'm not really willing to compromise the design as it is right now, so I pretty much want to know if there's a LayoutManager that allows me to create a GUI that looks like this:
IT HAS TO BE IN THE STANDARD JAVA API! :)
This looks like a good job for a BorderLayout. Put the buttons inside a nested container as the NORTH element. Add the JScrollPane as the CENTER component. The grid itself looks like it is a good candidate for a GridBagLayout or perhaps a GridLayout.
Short answer, yes: GridBagLayout. But that'll be a pain to work out and debug.
Long answer: It looks to me like you could do this best with a BorderLayout, a JPanel for the JButtons, and a JTable with custom TableCellRenderers and TableCellEditors.
Check the excellent documentation available for Java by Sun itself:
http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
Can you spot the GridLayout and GridBagLayout? If you put it into a scrollable container, that should do the trick.
Use GroupLayout for the overall panel and a custom paint method for the map.
I don't think many people here would recommend GroupLayout because it's more complicated than the other layout managers. I like it because it produces great scalable results, so I invested the time in understanding it. Now, I hardly use anything else - especially for user interaction panels with buttons and text fields.
For the map, though, I would create a custom MapPanel and overwrite paintComponent(). Sure you have to write your own custom scrolling algorithm, but I think that's a small benefit for not having to deal with scroll bars. You could make it so someone could just drag the mouse around and move the map. Use the mouse wheel to zoom, and make the interface very intuituve. If you want to paint scrollbars, you can do that too.
I've built several interfaces using models like this. I've built several maps for games using this model, as well as a financial market charting package. It makes it very easy to add custom functionality to do some great things that would be a nightmare to try to do in a JTable.
I want to draw a line between different components in a JPanel, but the line should be a component, ie, it can be clicked, highlighted, selected and deleted instead of just painting a line in a panel, is there anything like this or I must implement it by myself. And if I must implement it, how?
You could use a JSeparator. But you'll have to implement the click, highlighting, selection and deletion yourself. A JSeparator is just use to... separate sections in a panel.
If you mean that all these operations should be available when designing your GUI in a wysiwyg editor like NetBeans Matisse, then JSeparator is just what you need.
I tried to use prepared things like JSeparator, But I found the best way by myself and I implement it. I used a JLayeredPane for my container. I add my own JPanel behind the all layers and override its paint() method. in paint() method I used Java2D to draw a curve between Components on higher layers in JLayeredPane. You can see the result in below.
I’m working on an in-house app that tracks a bunch of tasks. I wanted to have a simple task monitor that would list the task name and the task’s status. I need this to look just a little nice, I’m no designer so whatever I do is going to suck, but a basic text display won’t work for the project requirements.
What I am essentially attempting to do is show something similar to the Firefox download window, the I-Tunes download window, and well I could name more but they all look basically the same. In each of these apps, each of the ‘progress panels’ is selectable. So to implement this I thought it would be simple to just use a list of JPanels each with a JProgressBar and a JLabel, each of which can just accept focus to determine if it and others are selected. I thought this was going to be an easy task, but if I use a JList it just displays text. I then just figured I would show all the task panels in a larger panel, but I cannot get the inner panels to recognize focus.
Is there a pattern for this? Is there a rolled standard solution that I just have not found? Or is there a better method for doing this? I don’t want to re-invent the wheel, but I thought this was just going to be simple.
It sounds like what you may be looking for is an JList.
You can add your items to the JList's by first adding your "task" to the JList object's ListModel (see the Create a Model section from The Java Tutorials), and then you'll want to assigned a custom ListCellRenderer which will accept your "task" and render on the JList as a JPanel in the list itself. The key here is to make your custom ListCellRenderer be able to display your "task" in the JList the way you want to have it show on the JList.
Take a look into the Writing a Custom Cell Renderer section from the How to Use Lists page of The Java Tutorials. It will describe how to make your custom ListCellRenderer so you can represent your "task" as anything you want.
To keep it short, you will implement the ListCellRenderer interface by implementing the getListCellRendererComponent which will return a Component which is the representation of your task in the JList. You'll probably want to either construct or instantiate your JPanel in this method and return it as the Component.
The standard way of doing this kind of things is to use JTable (or JList) as a container.
You don't have to use default renderes fot table cells, but you can specify your own renderer for specific cells. Take a look at CellRenderer
How about a JTable (which you can set to allow multiple rows to be selected) with an internal JPanel occupying the single cell in each row, which contains a JProgressBar and a JLabel. Or you could use a JList with the same structure as I just described.