jtable column size

i have a jtable,i want to have all columns Width to be the size of contents in rows.
if anything added in row then the column width should increase to fit that value.
any suggestion please.

Posted On: Wednesday 17th of October 2012 06:31:00 AM Total Views:  230
View Complete with Replies

Related Messages:

create a jtable which can combine and split and the image change also   (197 Views)
hi newbie here and would like to know how to create a jtable with default images and with specific column and rows that if you combine the cells the image enlarge and when split the image decreases.
jtable, defaulttable model   (195 Views)
hello how can i remove column in JTable i have already made a button that when triggered it will add column through DefaultTableModel tbl = new DefaultTableModer(): tbl.addColumn(columnList.get(x)) now what im trying to do is make a column vanish T T and im surprised i did not see a removeColumn method xD is there anyway i can remove columns through runtime and if JTable.remove(component) is the answer how how can i use it xD
Generating pdf from jtable   (184 Views)
I am developing a tool in java to generate pdf from jtable. I tried to use itext but I am getting black pdf. Below is the code that I used. import java.awt.BorderLayout; import java.awt.Graphics2D; import java.awt.Shape; import; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTable; //import com.itextpdf.awt.PdfGraphics2D; import com.itextpdf.text.Document; import com.itextpdf.text.PageSize; import com.itextpdf.text.pdf.PdfContentByte; //import com.itextpdf.text.pdf.PdfTemplate; import com.itextpdf.text.pdf.PdfWriter; public class Pdf extends JFrame { /** * */ private static final long serialVersionUID = 1L; private JTable table; public Pdf() { getContentPane().setLayout(new BorderLayout()); createTable(); } private void createTable() { Object[][] data = {{ "a", "b", "e", new Integer(5), new Boolean(false) } }; String[] columnNames = { "A", "B", "C", "D", "E" }; table = new JTable(data, columnNames); JPanel tPanel = new JPanel(new BorderLayout()); tPanel.add(table.getTableHeader(), BorderLayout.NORTH); tPanel.add(table, BorderLayout.CENTER); getContentPane().add(tPanel, BorderLayout.CENTER); } private void print() { Document document = new Document(PageSize.A4.rotate()); try { PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("jTable.pdf"));; PdfContentByte cb = writer.getDirectContent(); cb.saveState(); Graphics2D g2 = cb.createGraphicsShapes(500, 500); Shape oldClip = g2.getClip(); g2.clipRect(0, 0, 500, 500); table.print(g2); g2.setClip(oldClip); g2.dispose(); cb.restoreState(); } catch (Exception e) { System.err.println(e.getMessage()); } document.close(); } public static void main(String[] args) { Pdf frame = new Pdf(); frame.pack(); frame.setVisible(true); frame.print(); } }
How to edit the first column in JTreeTable when the columns are freezed   (102 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.
Create and dynamically change buttons in jtable column   (253 Views)
I am having a lot of problems fully grasping tablecellrenderer and tablecelleditor. I have researched a lot, looked in a lot of forums, but still can't solve my problem. Without posting an sscce (because my code is probably very incorrect), how would I do the following: Create a jtable with a column that has ButtonX in it. Then when another column in a specific row gets a certain value, ButtonX will change to ButtonY, but other rows will remain what they are already. In the following example, all rows started with ButtonX, but when Progress changed from Uploading to Complete, ButtonX became ButtonY. Row Name Progress Abort/Complete 0 File 1 Complete ButtonY 1 File 2 Complete ButtonY 2 File 3 Uploading ButtonX The buttons need to update accordingly if a row is deleted. For example if row 0 is deleted, it should look like: Row Name Progress Abort/Complete 0 File 2 Complete ButtonY 1 File 3 Uploading ButtonX
How to add a columns with a button set to a Jtable built with database result set   (138 Views)
hi, i have to build a gui to display account numbers and account balances of a particular person, after he logged with the correct user name and password. and when he get the account number and balances in a table, there should be another button to get further details of a account such as transactions, amount of each transaction etc. now i have stucked in the point of adding that button column to the table which is built with database result set. i would
Adding columns inside another column in JTable   (173 Views)
, I have created my own TableModel and it is working fine. I have 4 columns already in it. I want to add 3 sub columns inside the column number 3 and 4. I went through an article in which i can insert a column in the table but couldn find any help in inserting sub columns. Can anyone please help me. The column name is "Heap Memory" and the three sub columns that i want to add in this column are "Used","Commited" and "Maximum".
SWT Table - sum one column and save all content in MySQL table.   (101 Views)
!!! I want to do two things: 1. Get the sum of the column "Price". 2. Save all table's content in a MySQL table. (almost done but wrong way) For the second issue I try this: for(int row = 0; row < vista.tdetalle.getItemCount(); row++) { //product = item.getText(0); //price = item.getText(1); product = item.getText(row); // How can I set the first column here price = item.getText(row);// How can I set the second column here facade.RegistrarBitacora(product,price);//Here save on DB }
Change table column sizes and horizontal scoll not working   (142 Views)
I've got the following table going. I would like to reduce the size of the first column, and give that extra space to the second column. Also, the horizontal scroll does not work, but the is stuff off screen to scroll to.
set the column width of Jtable dynamically   (131 Views)
my problem is in JTable Column width , the content data length is more than the column width, how can i set column width dynamically
Request focus for the first cell of a jtable.   (286 Views)
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);
in a JTable, changing column header text alignment for specific columns only   (177 Views)
, At what classes should one look at (research) when trying to change the alignment of the header text for a specific column in a JTable I figured out how to change the entire JTableHeader text aligment through the getDefaultRenderer(), but I cannot figure out how to make certain column headers to be aligned left, while others are right.
Inserting single column values of database table into multiple columns of jtable and do comparison   (246 Views)
all, I am struck ed in inserting single column values of database table into multiple columns of jtable and doing comparison for selecting greater values in each row. Is their any query that splits column values. here i am attaching the code please give me the solution. public void disp( final String n, final String p, final String f, final String t){ try{ rs1=st.executeQuery("SELECT factname,monval FROM tsi where frm='"+p+"'"); //IN('"+p+"',"+"'"+f+"',"+"'"+t+"')"); while( { a = rs1.getString("factname"); b= rs1.getString("monval"); //c= rs.getString("mon"); rows1++; }System.out.println(rows1); rs2=st.executeQuery("SELECT monval FROM tsi where frm='"+f+"'"); while( { b= rs2.getString("monval"); rows2++; }System.out.println(rows2);/*// data2=new Object[rows2][1];int i2=0; rs2=st.executeQuery("SELECT monval FROM tsi where frm='"+f+"'"); for(int i1=0;i1
Table cell renderer called, but not when set on column   (107 Views)
I have a table cell renderer. When I set it using JTable.setDefaultRenderer(class, TableCellRenderer), it works fine. However, when I set it using TableColumn.setCellRenderer(TableCellRenderer) it never gets called. I have several columns that are String, and it would be nice to apply the renderer to just the column that needs it, rather than all String typed columns. What am I missing Works: table.setDefaultRenderer(String.class, new DefaultTableCellRenderer() { @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) { JLabel label = (JLabel)super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col); int mCol = table.convertColumnIndexToModel(col); if (mCol == 0 && allBranch) label.setText(""); return label; } });
custom table model and column model for JTable   (78 Views)
I have an extension of AbstractTableModel that I built partially from examples at ; it contains a collection of columns which I initialize at instantiation. Now I want to alter my code so that the number and content of columns can vary according to user selection, so I need to be able to add and remove columns from the display. My AbstractTableModel has its own additional method, resetTableColumns, that goes through my list of objects representing columns; it takes a parameter of type TableColumnModel, because it needs to obtain each TableColumn in the TableColumnModel and set its width and renderer. In my only call to this method so far, I obtain the TableColumnModel from the JTable. My current working code instantiates my table model, then adds objects describing each column to the table model, then instantiates the JTable, passing in my custom AbstractTableModel. My problem is that the column information seems to have to exist in the AbstractTableModel at the time the JTable is instantiated, or my method to reset the columns does not work. I have tried putting my column data into my AbstractTableModel and then calling JTable.setModel() , but that doesn't work: when I try to iterate through the columns in the TableColumnModel after doing that, I get an error indicating that my TableColumnModel doesn't have any columns in it. On looking at it, it does seem odd that I'm creating a collection of columns in the TableColumnModel when there is a separate ColumnModel object; perhaps I am confused over just what these two things represent. Can someone please point me to (preferably) an explanation of the concepts behind TableModel and TableColumnModel and related classes, or at least an example that adds and removes columns at runtime, including setting attributes like width and renderer Do I need a custom TableColumnModel class How do I split the responsibilites of my custom TableColumnModel and my custom AbstractTableModel rc
Updating Database by editing jtable   (226 Views)
all, I am new to java and i have a problem which i want to resolve. I recently saw a code which basically lists the values of a database in a jtable, so i modified it and used it in my own app. The problem which i have now come across is the updating of the database by editing the jtable. I have no idea how to do this, the only thing i know is that i have to call an event like firecellupdated but i dont know how to call it and what to call in it and also where to call it. Can anyone help me on this issue. Any help would be appreciated. Harry. Note: Here is the listing code in case you require it // Connect to an MySql Database String driver = "com.mysql.jdbc.Driver"; // String url = "jdbc:odbc: "; // if using ODBC Data Source name String url = "jdbc:mysql://localhost:3306/tpos"; String userid = "root"; String password = "root"; Class.forName( driver ); Connection connection = DriverManager.getConnection( url, userid, password ); // Read data from a table pstat = connection.prepareStatement("Select * from supplier where SupplierId="); pstat.setString(1,search); //String sql = "Select * from supplier where SupplierId="; //Statement stmt = connection.createStatement(); ResultSet rs = pstat.executeQuery(); ResultSetMetaData md = rs.getMetaData(); int columns = md.getColumnCount(); // Get column names for (int i = 1; i
Jbutton inside a jtable wont fire action   (264 Views)
Hi I have a big problem...Im trying to fire an action for a button inside a table, the problem....the button wont fire anything.... I just copyed a jtable button example and adapted it to my model. I hope you can help me , thanks a lot! the editor import java.awt.Component; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.DefaultCellEditor; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JOptionPane; import javax.swing.JTable; public class ButtonEditor extends DefaultCellEditor { protected JButton button; private String label; private boolean isPushed; public ButtonEditor(JCheckBox checkBox) { super(checkBox); button = new JButton(); button.setOpaque(true); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println(label + ": Ouch!"); fireEditingStopped(); } }); } public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { if (isSelected) { button.setForeground(table.getSelectionForeground()); button.setBackground(table.getSelectionBackground()); } else { button.setForeground(table.getForeground()); button.setBackground(table.getBackground()); } label = (value == null) "" : value.toString(); button.setText(label); isPushed = true; return button; } public Object getCellEditorValue() { if (isPushed) { // // JOptionPane.showMessageDialog(button, label + ": Ouch!"); System.out.println(label + ": Ouch!"); } isPushed = false; return new String(label); } public boolean stopCellEditing() { isPushed = false; return super.stopCellEditing(); } protected void fireEditingStopped() { super.fireEditingStopped(); } }
display all data from database into jtable   (225 Views)
i want to display all data from database into Jtable but my some cell consist of more characters of data because of which we have to scroll jtable a lot so i thought of displaying all data in jtextarea(by displaying it in nextline by using("\n")where ever necessary ) & then insert jtextarea in each cell of jtable but i dont know code please post a code which would help me