I would like to create an interactive JTable. For this, I would like to add JPanels in the cells of the table. Once the JPanels are in the cells, I can add my various components to the JPanels thus making the table interactive. Each JPanel could have different components. Would it be possible to accomplish this and only have to create 1 table cell editor and 1 table cell tenderer. Does anyone know of a better way to do it?
Thanks
EDIT: Thanks for the responses. I actually already have a framework I am using. I just needed a JTable that users could drag and drop images in, play movies, display graphs, etc... I already have the functionality to do those things, I just needed a JPanel to add them too. I wanted it to be displayed in a JTable so the cells could be sorted, moved, add/delete rows/col, and well structured. I couldn't get it to work using the JTable, so I went ahead an created my own. Its just a JPanel that contains smaller JPanels (the table cells) using the GridLayout. It works well enough for my puposes. Just a pain to rewrite all of the functionality from scratch that a table has.
This is hard. JTable actually uses the cell renderers only for painting the cell content. I would recommend to check if a gridlayout packaged into a scrollpane would be the easier solution.
It sounds like you're trying to use JTable as a docking framework. Assuming this is the case you're better off using something like MyDoggy or JDock which allow you to decompose your GUI into multiple split pane areas.
JSplitPane may be an alternative in this context: one pane would hold the JTable, while the other displays expanded details of the selected row. A compete example using GridLayout is shown here.
Related
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.
I have a GUI with a few JTextFields and JTables, and I would like to get the possibility to dynamically re-size the tables after running the application, so the user can increase the size of the tables by clicking on the border and dragging it.
I am considering this option or this post, but not sure if that´s what I need.
JTables are combined with table scrollers, and the GUI uses a JPanel with JGoodiesFormLayout, since it makes very easy to work with rows and columns.
Use JSplitPanes to divide the GUI where you want to allow the user to specify the component limits.
I write a lesson plan/timetable.
I just want to make like this:
On the right of the table it will be form to add a lesson.
Do you think it would be best to use the control JTable?
On the right of the table it will be form to add a lesson. Do you
think it would be best to use the control JTable?
JTable isn't proper JComponents for timetable, sheduler
use JPanel layed by SpringLayout, GridBagLayout, custom TableLayout
use AbsoluteLayout in the case that any of elements will be expansible, or draggable
I have a JFrame in which i have to insert JLabels, textfields and JButtons. I am able to these but how can i adjust them to the required position, i want to add one label and textfield in one row and then nxt label and textfield in the next row but they are coming in the same horizontal line. i have used flowLayout with the JFrame. please tell me how to adjust them accordingly. thanks
The key to distributing components in a Container in Swing is the Layout Manager. There are various types out there. To do what you are looking for, you might want to consider the GridLayout. It is pretty easy to set up. You first need to create the layout. The following will create a two columned layout with as many rows as you provide:
GridLayout gl = new GridLayout(0,2);
Then you apply it to your panel:
JPanel panel = new JPanel(gl);
Then you add your items:
panel.add(textfield1);
panel.add(button1);
panel.add(textfield2);
panel.add(button2);
The GridLayout will handle moving from row to row after you fill in the columns with components.
had a look at gridbaglayout? Should serve your purpose.
A GridLayout may be what you want, or a combination of a GridLayout and a FlowLayout. Look at the LayoutManager tutorial to get a better idea of when and how to use and combine the various layout managers.
You need to study the various types of layouts swing provides.
Also you can have a look at FormLayout,provide by JGoodies. I prefer to use this than swing layouts as i find it easy to code and less lines of code
You are using the default Swing Layout Manager. If you want different behaviour (which is very reasonable) then you need to use another LayoutManager. Several exists both from Sun and "out there".
In order for you to be able to choose, you need to know how they work. I can strongly recommend using the Java Tutorial for this:
http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html
Let us know if you need more than provided by e.g. nested BorderLayout's or TableLayout.
If you want to be able to position the UI elements in your application (nearly) absolutely, consider using a decent GUI builder like Matisse in NetBeans or Swing UI Designer in IntelliJ IDEA.
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.