Swing/AWT/SWT :: Custom JTable Renderer And Cell Editor
Jul 23, 2014
What I'm trying to do is make an inventory type program that includes a jtable. In the Jtable in the last column there will be 2 buttons and a textfield to register the value of additions or subtractions to the overal stock amount. (See screenshot in attachment).
As of now I am able to increment or decrement the numbers when the buttons are clicked on however the problem is that if I add 2 to the first row, and then go to add 2 to the next row or any other row for that matter, the value of the previous row is added to the next row, and the previous row reverts back to a value of 0. So basically its like there is only one editor that keeps track of one value no matter what row you are editing.
I need getting it so that I can add different values to each different row depending on the stock being used or received. The following is my code:
Main class
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 155, 643, 233);
IMG.add(scrollPane);
center = new centerJustify();
ts = new tableCellRenderer();
tm = new DefaultTableModel(){
[Code] .....
View Replies
ADVERTISEMENT
Jan 24, 2014
Now my issue is I have a JTable. The first column will contain Date objects all the way to the bottom. I want the JTable to display only the month and year. I created a custom cell renderer, but it appears the cell renderer method "getTableCellRendererComponent" is never called. (I tested it using System.out.println();) Here is my code for setting the cell renderer and the contents of the first column:
MonthYearTableCellRenderer renderer = new MonthYearTableCellRenderer();
tblView.setDefaultRenderer(Date.class, renderer);
for(int i = 0; i < 60; i++){
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.add(Calendar.MONTH, i);
tableModel.setValueAt(cal.getTime(), i, 0);
}
The Date objects are set into the cells of column 0 correctly. The table shows the result of the "toString()" method for each cell in that column.
View Replies
View Related
Jul 10, 2014
I've come across an interesting problem when using a Jcombobox as a custom cell editor(and renderer) in a jtable. I was hoping to add a keybinding in order to display the dropdown of the combobox instead of having to click on it however, when I make a call to showPopup() I get:
Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: component must be showing on the screen to determine its location Which is strange as my jtable is visible and the editor/renderer seems to be working fine.
Here's the cell editor + renderer I'm using:
class MyComboBoxRenderer extends JComboBox implements TableCellRenderer {
public MyComboBoxRenderer(String[] items) {
super(items);
} public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
if (isSelected) {
setForeground(table.getSelectionForeground());
[code]....
View Replies
View Related
Aug 25, 2014
I'm making a program that selects classes from a database via a jcombo box and then populates a time table with the class times.
I've used a custom cellrenderer that extends DefaultTableCellRenderer to text wrap the information and change the background of the cells.
It works but when i minimize the program it goes all black and if I double click I get a new JTextArea opening up on the timetable and when I minimize the program and go to open it again i get an "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException" and the cell render seems to be called again even though i haven't rerun the program.
This is the cell renderer that I built
public class TimesTableCellRenderer extends DefaultTableCellRenderer {
private int minHeight = -1;
private final JLabel label = new JLabel();
private final JTextArea textArea = new JTextArea();
public TimesTableCellRenderer(){
super();
textArea.setLineWrap(true);
[Code] ....
View Replies
View Related
Nov 2, 2014
I would like to ask about JTree custom renderer. What could be wrong with this. Here's the deal:
I have a Jtree which suppose to view the structure of given folder, so far so good its working. But my problem is that i dont want it to show the full path as name, only folder name and file name. I wrote a custom JTree renderer in hope that this will solve my problem but it didn't.
Here is my custom Jtree renderer:
private static class MyTreeCellRenderer extends DefaultTreeCellRenderer {
@Override
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
if( value instanceof DefaultMutableTreeNode){
[Code] .....
and here is where I am trying to implement it:
tree.setCellRenderer( new MyTreeCellRenderer());
View Replies
View Related
May 28, 2014
I have a number of cells which are Dates and need to have them appear in the same format. The samples I have seen usually create a new cell renderer object for each cell, even if its the same type. Since I have a number of Date cells I thought it best to use a single object for each of the date cells, but after seeing the different samples I am wondering if there is something I may be missing where using the same cell renderer object could potentially be an issue.
View Replies
View Related
Mar 18, 2015
I am trying to set the font of the editor as the same for the renderer of JTable. I am just a bit confused on how to do this. I don't want any code but maybe a walkthrough on what to do. Here is my abstract table model ....
public class ProductTableModel extends AbstractTableModel {
private ArrayList <Product> prodList = new ArrayList <Product> ();
private String [] columnNames = {"ID", "Description", "Inventory", "Cost", "Order Quantity"};
public ProductTableModel (ArrayList <Product> productList) {
this.prodList = productList;
[Code] .....
View Replies
View Related
May 29, 2014
My latest issue was that I wanted to only get a double value into a JTable Cell.
So first I Overrode the getCellEditor(int row,int col) method of my JTable, so if I was in a cell i knew I needed a double value I returned my Editor.
My editor filters out anything but numbers. I also check to make sure we get one and only one decimal point.
Other solutions Ive seen use keyboard Listeners, but to me that doesn't work. What if the user cuts and pastes data? My solution handles that as well.
It doesn't handle if they try to cut and paste something like 1234abcd.bob.123.23 , this would result in 1234..123.23 pasted in. I'll handle faulty values on the getValue method of my AbstractTable, but as long as the user is using mostly valid data this works well.
public TableCellEditor getCellEditor(int row, int col) {
if (row==TheRowWant && col==TheColumnI want) {
return (new DefaultCellEditor(createDoubleTextField()));
}
}
private JTextField createDoubleTextField() {
final JTextField field = new JTextField();
[Code] .....
View Replies
View Related
Aug 20, 2014
I am facing some peculiar issue with JTable.
Change value of a cell, without clicking on anywhere else, directly click on column (or) try to expand column size, value you entered in that cell vanishes. But if you click on any other cell instead of expanding column size, value will remain in cell. Not really sure how to fix this issue.
class SimpleTableExample
extends JFrame
{
// Instance attributes used in this example
privateJPaneltopPanel;
privateJTabletable;
privateJScrollPane scrollPane;
[Code] ....
View Replies
View Related
May 7, 2014
I'm having a small problem using a jTable.
After calling the .clearSelection() methode of the table object, the cell clicked in when selecting the table row, keeps it's focus.
How do I remove the focus of the cell?
View Replies
View Related
Feb 10, 2014
My code is
Double qty = 0.0;
for ( int i = 0; i < ingTable.getRowCount(); i++){
qty = (Double) ingTable.getValueAt(i,2);
if( qty < 30.0){
ingTable.setBackground(Color.red);
} else {
ingTable.setBackground(Color.white);
}
}
View Replies
View Related
May 29, 2014
I have a ComboBox I'm using for a cellEditor. The list of items in the comboBox might have colors behind them, which I've managed to render. What I haven't figured out is a good way to keep the color in the cell after the item is selected.I don't' want to have a persistent comboBox in the cell, i only want to see it when editing that cell.
public TableCellEditor getCellEditor(int row, int col)
{
if (row==theRow && col==theCol)
{
JComboBox<?> combo = new JComboBox<Object>(getPickListEntries());
combo.setRenderer(new PickListRenderer(combo));
combo.setBorder(BorderFactory.createEmptyBorder());
}
[code]....
View Replies
View Related
Feb 18, 2014
I have a jtable that I would like to copy from a cell that I select to clipboard. How could I do these.
View Replies
View Related
Jan 5, 2015
I have a program where i want to indicate the user when i have completed a task which i am running inside a for loop. Below is my code.
for(Map.Entry<Double,SimplestCopyInstance> entry : myList.entrySet()){
double key = entry.getKey();
SimplestCopyInstance scp = entry.getValue();
Copy cp = new Copy();
cp.putLocations(scp.getSrc(), scp.getDes());
scp.setStatus(cp.baseCopy()+"");
[Code] ....
I have used netbeans to build my app. So there creating jTable is out of my control as that part was inside auto-generated code. I have just used the jTable.setValue().
My problem is, above method is inside a button click event. Updated values not displaying until the loops ends.
View Replies
View Related
Jan 20, 2015
I'm working on a spreadsheet-like table and I was able to set it so that it would highlight the first cell in the relevant row(titled 1,2,3.. etc) when a cell is clicked. (Just like in MS Excel - preview attached) Now I want to do the same to the column header(i.e. A,B,C,D....etc) . How can I do it?
private void formWindowOpened(java.awt.event.WindowEvent evt) {
setLocationRelativeTo(null);
int rowNum = sheet.getRowCount();
for(int i=0; i<rowNum; i++){
sheet.setValueAt(i+1, i, 0);
[Code] .....
Untitled.jpg
View Replies
View Related
Jun 24, 2014
Tried creating a simple sample but all works as expected.
The following code prevents the cursor from moving when inside a cell of a JTable.
public void keyPressed(KeyEvent e) {
if ( (e.getKeyCode() == KeyEvent.VK_KP_LEFT) || (e.getKeyCode() == KeyEvent.VK_KP_RIGHT)
|| (e.getKeyCode() == KeyEvent.VK_RIGHT) || (e.getKeyCode() == KeyEvent.VK_LEFT) )
{
//Do nothing
e.consume();
}
}
});
When editing a cell, the existing code would use the right/left cursor keys to move from cell to cell as opposed to from character to character when editing a cell. I planned to override the functionality by tossing in the above code as a test to see if it stops the functionality before I override it.
After placing in the above code, the above functionality no longer occurs, but now the cursor moves within the cell as I wanted which is to move from character to character instead of cell to cell. Its great it works, but it really shouldn't. Essentially the default behavior has been restored when it should have really disabled the left/right keys.
I assume there is some underlying class someplace that is doing something to affect the behavior. Since a sample can't be provided I am wondering in what scenarios would the e.consume() restore default functionality?
View Replies
View Related
Aug 19, 2014
How do i parse a String of text in a JTable Cell to work as a button?For example lets say a row of IDs such as 1,2,3,4,5,6 id like the whole column to parse as JButtons that are titled 1,2,3,4,5,6..Id like to then use these buttons to call a function with the title as a parameter.Its just a simple row of entries where the button will open up an edit screen to edit the corresponding clicked entry.So how do i parse them as buttons for a start?
View Replies
View Related
Jun 6, 2014
I want perform some calculations whenever a specific cell block is focused in JTable, I've tried using focusListener on JTable but looks like my table isn't triggering the event. I don't know why, what to do?
View Replies
View Related
Jul 8, 2014
I have an editable JTable that has a lot of JComboBox's as cell entries. I would like to be able to "arrow over" to said cell, and with a pressing of either a numerical button or by typing the first letter of the String selection (possibly followed by a second), be able to select the appropriate selection from the cell's JComboBox. I have tried to add a key listener to the JComboBox itself, which works given that I click on said cell and show its menu.
How would I go about ensuring that, when focus is on the cell itself, and I start typing, the appropriate selection is chosen?? (I think this might get into key binding, but I don't know if I have to try it from the scope of JTable, or from that JTable's TableModel (which I have made my custom version of).
Here is what I have so far (everything but SandwichNumber, SandwichName, and Oregano uses JComboBox): [URL] ...., how would I do such key binding?
View Replies
View Related
Feb 18, 2014
I searched a lot and got some answers for this Q. but many of them referred to links which give 404 error. I want to make table like this:
Can I make this in Java?
View Replies
View Related
May 31, 2014
I am trying to do the fallowing ,
Jtable cell with search (% like) depends on the value entered
if i enter "mi" it should show all data begin with "mi" from my data set
View Replies
View Related
Mar 22, 2015
I would like to create a JTable.
Each cell contains an array of 4 objects:
[0] ImageIcon
[1] ImageIcon
[2] Number
[3] Boolean
1) Once JTable is displayed, each cell display only [0] .
2) Later, based on user mouse click on cell, cell should display [1]
How can I use an array with 4 values in Table Cell?
View Replies
View Related
Mar 19, 2014
I'm want to write a program to design single page documents. It needs the following features.Drag text and place it anywhere on the page, ideal I would like to add a 'snap to grid' optionDraw boxsAdd simple images (logos)
I plan to do it using Swing, but I've not done anything with this since finishing Uni. how to structure this and what swing components will be best suited for this task?
View Replies
View Related
Feb 2, 2015
I am trying to make a custom table for a DB and i am stuck.
I want only the first column to have a checkbox and others not.
Is there a better way to insert the values instead using the getValueAt ?
@Override
public Object getValueAt(int row, int col)
{
if (col == 0)
[Code].....
View Replies
View Related
May 14, 2014
I'm trying to create a table where the Header is a custom object.
The custom object would be something along the lines of
public MyColumnObject
{
String myLabel;
ArrayList dataForDropdowns;
int defaultColumnIndex;
int modifiedColumnIndex;
String tooltip;
}
This is simpler then what I want to do, but it's the basic concept. I want the column header to render the myLabel for the visual (at least at first). I want this render to be applied to however many columns I have, which will differ from table to table, but I always want the column header of each column to be of type myColumnObject.
I thought somethinglike this would work, but I'm getting java.lang.ClassCastException: java.lang.String cannot be cast to myColumnObject which makes sense, but I thought that I would be getting back my object.
p.s. I re-labeled all my code from my actual project, so if something looks off, it was probably just the re labeling.
public NewTestTable(MyColumnObjects[] fields)
{
setModel(new TestObjectTableModel(
new Object [][] {
},
fields
) {
private static final long serialVersionUID = 1L;
public boolean isCellEditable(int rowIndex, int columnIndex) {
[Code]...
View Replies
View Related
Nov 9, 2014
I have a JList<Object> in my code (it actually takes JLabels) called imageList, and I've given it a custom CellRenderer using the following code:
cellRenderer = new DefaultListCellRenderer() {
private static final long serialVersionUID = 1L;
@Override
public Component getListCellRendererComponent(JList<?> list,
Object value, int index, boolean isSelected,
boolean cellHasFocus) {
[Code] .....
However, when I click anywhere on the JList, nothing gets printed to the console, which suggests that the mousePressed() method isn't being invoked. I think it's a problem with the CellRenderer, because I have similar code elsewhere that works, the only difference being I don't use a custom CellRenderer.
View Replies
View Related