SEARCH YOUR SOLUTION HERE  

Request focus for the first cell of a jtable.



I'm having a JTable which is having text field in each of its cell. I would like to set the cursor in the first cell of each row in the Jtable. I'm trying to find some solution but not able to succeed.


Please provide me some working example.

Quick solution will be appreciated.

TableColumn column0 = jTable2.getColumnModel().getColumn(0); JTextField textField = new JTextField(); textField.requestFocus(); DefaultCellEditor cellEditorFristColumn = new DefaultCellEditor(textField); column0.setCellEditor(cellEditorFristColumn); DefaultTableCellRenderer defaultRenderer = new DefaultTableCellRenderer(); defaultRenderer.requestFocus(); jTable2.getColumnModel().getColumn(0).setCellRenderer(defaultRenderer);

Posted On: Friday 28th of September 2012 05:13:47 AM Total Views:  253
View Complete with Replies




Related Messages:

My second frame will not open and my first will not close   (143 Views)
Im having problems with the secong frame, I have put in capital letters a comment next to my problem. I can not get the old frame to disapear and open the new one (I dont know if login.dispose() is even java, it was just a guess). package isaacskelton.webs.com; import java.awt.Color; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextField; public class Login { public static void main(String[] args) { final JFrame login = new JFrame("Login to CheckMate"); login.setSize(600, 400); login.setLocation(375, 150); login.setVisible(true); login.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Login Content JLabel welcome_label = new JLabel(" Welcome to CheckMate!"); welcome_label.setFont(new Font("Lucida Handwriting", Font.BOLD, 36)); JLabel tagline_label = new JLabel(" Digital Roll System."); tagline_label.setFont(new Font("Lucida Handwriting", Font.BOLD, 20)); JLabel username_label = new JLabel("Username:"); final JTextField txfield_username = new JTextField(35); JLabel password_label = new JLabel("Password:"); final JPasswordField passfield_password = new JPasswordField(35); JLabel blank_space = new JLabel(""); JButton submit_login = new JButton("Login"); submit_login.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ //Strings String username = txfield_username.getText(); String password = passfield_password.getText(); //Conditional Statements if(username == "user" && password == "pass") { //THIS IS MY PROBLEM HERE, THE FRAME WILL NOT APPEAR login.dispose(); JFrame main_screen = new JFrame("Welcome" + username); main_screen.setForeground(Color.GRAY); main_screen.setSize(1024, 750); main_screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); main_screen.setVisible(true); //HERES THE END OF THE PROBLEM } else { ; } } }); //Panel JPanel panel = new JPanel(new GridLayout(8, 0)); //Add to Panel panel.add(welcome_label); panel.add(tagline_label); panel.add(username_label); panel.add(txfield_username); panel.add(password_label); panel.add(passfield_password); panel.add(blank_space); panel.add(submit_login); //Add Panel to Frame login.add(panel); } }
Dialog focus problem on java 7 and mac osx   (190 Views)
, I'm currently validating a swing application that runs on a browser (on an applets). I came across with a problem for the java JRE 7 released by Oracle - this was not happening on Applet JRE 6 (1.6.0_33) Every time I open a dialog within my application, that dialog comes to the front of the browser window but it does not request the focus as expected. THIS HAPPENS ONLY WHEN THE APPLET RUNS ON THE BROWSER. Here is a small program to show the problem: package com.macosx.tests; import java.awt.Color; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JApplet; import javax.swing.JButton; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.SwingUtilities; public class AppletDialogExample extends JApplet{ private JPanel panel; private JButton openDialogBtn; private void doStart() { panel = new JPanel(); panel.setPreferredSize(new Dimension(500,500)); panel.setBackground(Color.GRAY); openDialogBtn = new JButton("open dialog"); openDialogBtn.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent arg0) { Runnable run = new Runnable() { public void run() { JPanel dialogPanel = new JPanel(new GridLayout(1,1)); JTextField textField = new JTextField(); dialogPanel.add(textField); JOptionPane.showConfirmDialog(null, dialogPanel); } }; SwingUtilities.invokeLater(run); } }); panel.add(openDialogBtn); setContentPane(panel); } @Override public void start() { try { SwingUtilities.invokeAndWait(new Runnable() { public void run() { doStart(); } }); } catch (Exception e) { throw new RuntimeException(e); } } }
How to bring Pop up text area in a JTable cell   (143 Views)
I want help in one concept. When I click a cell in a column the cell should invoke a pop up dialog and should display the full content of the cell in the pop up dialog. This should be applicable to only column in the table. Can anyone please guide on this
Find out if there is a component in a specific cell of GridBagLayout?   (122 Views)
Hi , it's been awhile, but I'm glad to be back. I'm making a program to layout photos in a collage, grid style. I've created a custom component that extends JPanel, called PhotoPanel. Each PhotoPanel paints it's photo on the JPanel with a transparent background. I layout the PhotoPanels in a JPanel (called the PagePanel) using GridBagLayout. Each PhotoPanel has an instance of GridBagConstraints. When properties are changed for either the page or the selected PhotoPanel, the selected PhotoPanel's GridBagConstraints is updated, and the page layout is redone: e.g. all components are removed from the PagePanel and then readded using their own, updated GridBagConstraints object. The idea is that each photo can be set up to occupy multiple cells in the grid, using the GridBagConstraints 'gridwidth' and 'gridheight' properties. My method to add all of the PhotoPanels to the PagePanel keeps track of 'row', 'column', 'maxRows', and 'maxColumns' variables to control where the PhotoPanels are added. They are simply added in sequential order like so: set the PhotoPanel's constraints, including 'row' and 'column' page.add(PhotoPanel, PhotoPanel.myConstraints); column++ if(column >= maxColumns){ column = 0; row++; } What I want is to check the GridBagLayout to see if there is already a component occupying the cell at position "row,column" so that I can skip that cell. My current approach is that I create a boolean array of size [maxRows][maxColumns] and then set the "cells" where I add components to true. For components that occupy multiple cells on the PagePanel, multiple "cells" in the array are set to true in the proper positions. It works the way I want it to right now, but I feel like it would be more reliable to be able to "ask" the GridBagLayout if a certain cell is already occupied. Is there a way to do this or should I just stick with my boolean array
Update MySQL table by updating JTable's cell   (131 Views)
I have a JTable which is used to show and modify table from a MySQL database. I have no problem to retrieve/show data in JTable or insert new data into a blank table. However, I have problem to update any particular cell in the JTable and reflect the changes in the MySQL database. If there are hundreds of rows and I update couple cells within around 10 rows, and I create a save button to apply all the changes to MySQL's table. what's the proper way to do that It will include so many UPDATE statement for every row I changed, how do I detect any change I made in cells efficiently and save all the change back to the DB at once.
Update database by editing the cells in JTable   (101 Views)
i have created a form in which i added a jtable which i guess is of type defaulttable model DefaultTableModel model=(DefaultTableModel)jTable1.getModel(); model.setRowCount(0);
How to set Focus to the first row in a JTable which is inside a JScrollPane   (132 Views)
I have a JTable inside a JScrollPane. I have put this inside a panel. As soon the panel loads i want the first row in the JTable to get the focus BUT by default the focus goes to the JScrollPane and on pressing tab the focus enters into table's first row. I dont want to use setRowSelectionInterval(0,0) and setColumnSelectionInterval(0,0) as my requirement is different from that. Kindly help me.
Understanding cell editor in JTable   (81 Views)
I am trying to learn about cell editors in JTable (swing). I am now studying this example: http://docs.oracle.com/javase/tutorial/displayCode.htmlcode=http://docs.oracle.com/javase/tutorial/uiswing/examples/components/TableFTFEditDemoProject/src/components/IntegerEditor.java There are a few things I don't understand. I'd be happy if you could help me better understand this example. The first line of getCellEditorValue() is: JFormattedTextField ftf = (JFormattedTextField)getComponent();
Highlighting a JTable cell on a successful search for a value entered in a textfield.   (127 Views)
Hi , I want to have a feature with JTable wherein I'll provide a textfield to enter a value to be searched from the JTable and if this entered value matches with any of the cell values in the JTable then that particular cell should be highlighted & the cell font should be turned to BOLD. The values will be matched when the user presses Enter key after specifying the value in the text field. Now I am able to search a value from the JTable, & on a successful search I call the TableCellRenderer. But the thing is on a successful search I try & edit that particular cell so that it is selected. But the method editCellAt(row, column) returns a false. And also how do I give a focus on that particular matched cell. Due to this even if the cell renderer runs it reflects me no change in the JTable cell. Can anyone plz help me out with this problem. It is very urgent. help is appreciated. Thanx. Neha......
How to edit the first column in JTreeTable when the columns are freezed   (87 Views)
HI I had freezed some of the columns in JTreeTable. This is done implementing JTable and sharing the model of JTreeTable only. When some of the columns are freezed, the first column in the non scrollable area that is the first column in the rest of the columns in JTreeTable is not editable wherever area you click in the if its width is less than or equal to the width if the first column in the frozen area. But if i increase the width of the first column in JTreeTable to more than the width of the first column in the frozen area which is JTable and click on the increased ares of the column then it is editable. In the first case when width of first column if JTreeTable is less than or equal to the one in JTable then isCellEditable method is not called but in the second case when the width of the first column is greater than the first column of the frozen area then if we click on the increased width area then the isCellEditable method is called and the cell is editable. Please help me through this. I am stuck up on how to resolve this.
JTextField focus problem   (257 Views)
Hi , I have problem with active textfield when start the GUI. Got the JTextField component inside JTable, and the jTable inside another JTable ie. JTableOuter - > JTableInner - > JTextField. If window is displayed, I set up the focus for the first JTextField component, but nothing happened, the focus occurs and component is active when I do mouse clicking on it. I modified this source code: http://pekalicious.com/blog/custom-jpanel-cell-with-jbuttons-in-jtable/ Any ideas
JFileChooser - How to set imput focus.   (92 Views)
I'm using JFileChooser to select files that already exist. I'd like the imput focus to be on the displayed list of files so that the user may immediately select one of more of the listed files but when the filechooser is displayed the input focus is in the text box. How may the input focus be specified
FocusListener.focusLost() and/or InputVerifier.shouldYieldFocus() not called?   (116 Views)
I've been wanting to update database information from a panel field when the focus is lost. Turns out that the focusLost() method is called only if some other component gets the focus. Then I came across the shouldYieldFocus method of the InputVerifier class. Even though (in some cases) I did not need to verify the input, I could gain control (or remain in the field if the value was "bad"), I could at least update the database value with the changed value of the field. Well, it appears that the shouldYieldFocus() has the same issue as the focusLost() method: the focusLost() is not called if, say, the close window (the "X") is clicked. So, my question is: does the shouldYieldFocus() work the same way as focustLost() in this regard
disable witing in specific cells in JTable   (86 Views)
Hi all; I am a beginner in programming, and this course I suppose to create Sudoku generator by using java in NetBeans So far I did the code for generating numbers in a JTable and show the whole solution upon request But I am straggle in how to disable writing in the visible numbers or the visible cells but enable in the empty cells I used this code but as it is cleat it disable typing in the whole table workSheetTable.getCellEditor(); Also I cant think of a way that enables me to get the answer from the empty cells and compare it with the actual solution!!! Could you help me please in this !
JTable not reflecting cell edit   (96 Views)
After I manually edit a cell of my JTable, the cell wont display the new text. Instead, the cell reverts back to its original text. public class Table { JTextField filterText; TableRowSorter sorter; MyTableModel model; ArrayList arraymeeting = new ArrayList(); ArrayList arraycontact = new ArrayList(); String [] columnnamescontact = {"name", "email", "address", "phone"}; String [] columnnamesmeeting = {"start date", "start message", "end date", "end message"}; String [] columnnamesnote = {"start date", "end date", "message"}; String [][] data; public JTable createTableContact(ArrayList earray) { JTable table = new JTable(); arraycontact = earray; data = new String[arraycontact.size()][4]; for (int row = 0; row
How to create a custom cell renderer for a table?   (119 Views)
Hi folks! I'm having some issues when a try to create a custom cell renderer for a table and I was hopping you can help me out. So, I'm trying to make a table and on the second column of the table it should be displayed a date (it's a Calendar object actually) and I want to format the way the date appears in the table and for this I need to create a custom cell renderer for that particular column. Here is the CellRenderer class: public class TraseuDateCellRenderer extends JLabel implements TableCellRenderer{ DateFormat dateFormat = new SimpleDateFormat("dd/MM HH:mm:ss"); public void setDateFormat(DateFormat dateFormat) { this.dateFormat = dateFormat; } public Component getTableCellRendererComponent (JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { if(value != null) { Calendar dateValue = (Calendar) value; setText(dateFormat.format(dateValue.getTime())); } return this; } }
Lazy loading of image in table cell with custom renderer   (186 Views)
I have a table with a custom cell renderer made from a custom component that includes a JLabel with an ImageIcon. The images for the ImageIcons have to be downloaded from Amazon Web Services, and this takes awhile. I'd like the textual parts of the custom component to display right away, and the images to be added as they become available. But the obvious way of handling this (start a background thread to download the image; update the JLabel when it completes) doesn't work, presumably because of the peculiar way custom renderers are handled (same renderer used for multiple cells, so that renderer.repaint() does nothing). Currently I am just getting the image and updating the JLabel with it in the custom component's getTableCellRendererComponent() method. This means that all the images are downloaded before any of the table's cells are painted. I've spent a fair while struggling with this; I'd be very interested to learn what the solution might be, if there is one.
i created 2 frames, how to call another frame from the first.   (102 Views)
in a swing application i created 2 frames.in this two frames 1 frame has 3 buttons ok,cancel,new buttons.My task is that when i click on the ok button it has to open the second frame .Can you please advise me on the code for the button.
I cannot for the *life* of me set the focus to my desired window.   (88 Views)
I've been at this for hours. I've now distilled it down to a very simple test program. First, I'm running JRE 7 on Linux (I've heard that can make a difference). Here is what I'd like the following program to do: 1. Open 3 windows 2. Set the focus to "TestWindow 1" What it actually does: 1. Opens 3 windows 2. Sets the focus typically to "TestWindow 3" and sometimes to "TestWindow 2" After much reading, I've moved the focus request (and almost everything else gui related for that matter) into the event dispatch thread. Please let me know your ideas! Now I'm dying of curiosity as to the solution. Class TestWindow import javax.swing.*; public class TestWindow { JFrame jFrame; TestWindow(String name, int x, int y) { DefaultListModel defaultListModel = new DefaultListModel(); defaultListModel.addElement("one"); defaultListModel.addElement("two"); defaultListModel.addElement("three"); JList jList = new JList(defaultListModel); JPanel jPanel = new JPanel(); jPanel.add(jList); jFrame = new JFrame(); jFrame.add(jPanel); jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); jFrame.setTitle(name); jFrame.setSize(200, 100); jFrame.setLocation(x,y); jFrame.setVisible(true); } public JFrame getJFrame(){ return jFrame; } }
it draws firstly circle and then after a second the circle disappears   (73 Views)
In my code it draws firstly circle and then after a second the circle disappears can anyone explain why does it do so =/ import java.awt.BorderLayout; import java.awt.Color; import java.awt.Graphics; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.GridLayout; import java.awt.MouseInfo; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class Mandelbrot extends JPanel { double startR = -2; double endR = 2; double startI = 1.6; double endI = -1.6; Integer chooseNum = 70; final JPanel panel = new JPanel(); public int mand(Complex c, int max) { Complex z = new Complex(0, 0); for (int count = 0; count < max; count++) { z.square(); z.add(c); if (z.modulusSquared() > 4) return count; } return max; } public void init(JFrame frame) { frame.setSize(700, 700); JPanel pn = new JPanel(); pn.setLayout(new GridLayout(1, 2)); this.setSize(400, 400); pn.add(this); final Julia myJulia = new Julia(); myJulia.setSize(300, 300); myJulia.setWidth(this.getWidth()); myJulia.setHeight(this.getHeight()); myJulia.setMax(chooseNum); pn.add(myJulia); frame.getContentPane().add(pn, BorderLayout.CENTER); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } class Julia extends JPanel{ int amount; int max; double x; double y; double width; double height; public void setMax(int max){ this.max = max; } public void setX(double x){ this.x = x; } public void setY(double y){ this.y =y; } public void setWidth(double width){ this.width = width; } public void setHeight(double height){ this.height = height; } public int jul(Complex d, Complex c){ //Complex c = new Complex(-2, 1.6); for(int count =0; count< max; count++){ d.square(); d.add(c); if(d.modulusSquared()>4) return count; } return max; } public void paintComponent(Graphics g){ super.paintComponents(g); paintJulia(g); } public void paintJulia(Graphics g){ double startR = -2; double endR = 2; double startI = 1.6; double endI = -1.6; if(amount ==0){ Complex c = new Complex(0,0); for(int i =0; i< this.getWidth(); i++){ for(int j =0; j< this.getHeight(); j++){ double valueI = (double)i; double valueJ =(double)j; double coordinateX = startR +(valueI/this.getWidth())*(endR - startR); double coordinateY= startI -(valueJ/this.getHeight())*(startI - endI); Complex d = new Complex(coordinateX, coordinateY); if(jul(d,c)!= max){ System.out.println(" "+jul(d,c)); g.setColor(Color.pink);} else g.setColor(Color.green); g.drawLine(i, j, i, j); } } amount = amount+1; } else{ double coordinateX = startR +( x/width)*(endR - startR); double coordinateY = startI -(y/height)*(startI - endI); Complex c = new Complex(coordinateX, coordinateY); for(int i =0; i< this.getWidth(); i++){ for(int j =0;j< this.getHeight(); j++){ double valueI = (double)i; double valueJ =(double)j; double dRe = startR +(valueI/this.getWidth())*(endR - startR); double dIm = startI -(valueJ/this.getHeight())*(startI - endI); Complex d = new Complex(dRe, dIm); if(jul(d,c)!= max){ //System.out.println(" "+jul(d,c)); g.setColor(Color.pink); } else g.setColor(Color.green); g.drawLine(i, j, i, j); } } } } }