JTable problem: not visible


i can't seem to get this example working. the code is as follows:
import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Toolkit; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTable; import javax.swing.JScrollPane; public class junk extends JFrame{ dbTableModel dbtm; public junk(){ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setPreferredSize(new Dimension(680, 330)); add(new myJPanel()); pack(); setVisible(true); } public class myJPanel extends JPanel{ Image image; public myJPanel(){ super(); setLayout(null); try { dbTableModel dbtm = new dbTableModel("com.mysql.jdbc.Driver","jdbc :-o dbc:nicholas","","","select * from spiderman"); } catch(Exception e){ e.printStackTrace(); } JButton x = new JButton("Climax"); x.setSize(new Dimension(100,50)); JTable b = new JTable(dbtm); b.setSize(new Dimension(300, 100)); b.setPreferredScrollableViewportSize(new Dimension(300,100)); JScrollPane p = new JScrollPane(b); p.setSize(new Dimension(300, 100)); add(p); add(x); b.setLocation(50,50); p.setLocation(50,50); x.setLocation(50,180); image=Toolkit.getDefaultToolkit().getImage("java_logo_full.png"); } public void paintComponent(Graphics g){ super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; //g2.scale(2.0, 2.0); g2.drawImage(image, 0, 0, this); } } public static void main(String[] args) { new junk(); } }
Posted On: Monday 22nd of October 2012 02:33:49 AM Total Views:  100
View Complete with Replies

RELATED TOPICS OF Swing / AWT / SWT PROGRAMMING LANGUAGE




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);
VIEWS ON THIS POST

336

Posted on:

Friday 28th September 2012
View Replies!

Inserting single column values of database table into multiple columns of jtable and do comparison

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(rs1.next()) { 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(rs2.next()) { 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
VIEWS ON THIS POST

289

Posted on:

Wednesday 3rd October 2012
View Replies!

Updating Database by editing jtable

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:\t"; // 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
VIEWS ON THIS POST

291

Posted on:

Wednesday 17th October 2012
View Replies!

Jbutton inside a jtable wont fire action

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(); } }
VIEWS ON THIS POST

305

Posted on:

Wednesday 17th October 2012
View Replies!

display all data from database into jtable

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
VIEWS ON THIS POST

263

Posted on:

Wednesday 17th October 2012
View Replies!

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.
VIEWS ON THIS POST

267

Posted on:

Wednesday 17th October 2012
View Replies!

printing a jtable

I've got a swing JTable of about 20 rows ans 40 columns that I want to print on paper. I tried the table.print() method but that doesn't work, I just get one big empty cell on the paper. I tried an example from another site and that one works perfectly (http://java.sun.com/docs/books/tutorial/uiswing/misc/printtable.html). Does anyone know what's going wrong Is my table too large for the paper and do I need to resize it, and if so how
VIEWS ON THIS POST

516

Posted on:

Wednesday 17th October 2012
View Replies!

inserting jtable into database

hi friends..i m using javax.swing.JTable..with defalutSetModel..it has three columns..i want to insert it into the mysql database.can i insert whole table in one column of database..please help me.. i m new to usiing the swings components
VIEWS ON THIS POST

360

Posted on:

Wednesday 17th October 2012
View Replies!

jtree to jtable (need help in coding)

Hai all, i was build an jtree from an xml,now for each node there are child elements and the child elements consists of static and dynamic fields,when i select a node in the jtree the child elements should be displayed in jtable format (and one more thing is that my tree consists of 1 supernode and it consists of 4 subnodes and each subnode consists of 3 child nodes).can anyone suggest or give a code to display jtable from jtree. and i should able to edit the values and save to jtree as well as jtable.
VIEWS ON THIS POST

217

Posted on:

Wednesday 17th October 2012
View Replies!

how run tim data in jtable or show data in jtable from my database table

i use DefaultTableModel. or swing jtable some body give me idea i use dumy class and use getColumnClass() and isCellEditable() class for modify the swing defult jtable or swing jtable give class because swing jtable give me defult data that read only but i want show data in jtable form my database table i change the title of table form give propery bu i have lot of row how i run time or dynamicly give data to jtable form my database table in jtable such as my table have data this type sname sno class address marks fahim 34 4 xxxx 545 aamir 43 4 ljfd 55 kashif 33 5 gd 444 and swing defult jtable give me this type data private String[] columnNames = {"First Name", "Last Name", "Sport", "# of Years", "Vegetarian"}; private Object[][] data = { {"Mary", "Campione", "Snowboarding", new Integer(5), new Boolean(false)}, {"Alison", "Huml", "Rowing", new Integer(3), new Boolean(true)}, {"Kathy", "Walrath", "Knitting", new Integer(2), new Boolean(false)}, {"Sharon", "Zakhour", "Speed reading", new Integer(20), new Boolean(true)}, {"Philip", "Milne", "Pool", new Integer(10), new Boolean(false)} }; or null data please give me ida how i show my database table data in jtable and how i set the jpanel location such as setlocation(12,14,3,5) but this code not give me right result please give right code
VIEWS ON THIS POST

266

Posted on:

Thursday 18th October 2012
View Replies!

get correct values from sorted jtable

hi! maybe you can help me get the correct row value from a sorted jtable i am able to be a value using getValueAt() but when i sort the table the value i get dont match the one on display. i believe there is an extra step to retrieve the real row, but i dont know how.
VIEWS ON THIS POST

207

Posted on:

Thursday 18th October 2012
View Replies!

Removing a jtable from a jpanel/Changing the table

.. I've written this code, which creates a table with a scrollpane, and loads the results of a search, when a button is pressed. The problem is that each time i press the button a new jtable is created. How can i make it either: a) refill the same table b) remove the table and make another one which displays the new results. My code: searchBt.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { pd.contains(qTxt1.getText(),true); System.out.println(pd.dictSearchStore.size()); if (pd.toObject()!= null && pd.toObject().length!=0) { JTable resTable = new JTable(pd.toObject(),tableTitles); JScrollPane scrollPane = new JScrollPane(resTable); resTable.setPreferredScrollableViewportSize(new Dimension(170, 160)); panel.add(scrollPane); panel.validate(); } } });
VIEWS ON THIS POST

236

Posted on:

Thursday 18th October 2012
View Replies!

jtable and jscrollpane question

hey, I have a JTable in a JScrollPane. My application is designed to have this table take up most of the screen. Sometimes the table has hundreds of rows of data in it and everything works well. However, sometimes it only has three rows in it and it looks very bad inside of the scrollPane. I would like my table to be full screen all the time regardless of the actual number of rows of data it has. Is there an easy way to do this or do I have to add "blank" rows to my table whenever I detect this condition
VIEWS ON THIS POST

227

Posted on:

Thursday 18th October 2012
View Replies!

jtable, table model, column headers

Dear all, I am using jbuilder 10 for an application and I have bumped on a glitch. I create my own tablemodel (which inherits from the AbstractTableModel) do the necessary work (implementing the abstract methods) and I also override getColumnName method. However, when the application starts no column headers appear (even though when I pass my tabled model in teh JTable constructor getColumnName is called). At first I believed that I had made a mistake with my table model class but when I used the FileTableModel class from O'Rilley's JFC book same thing happened. Does anyone have any idea why this is happening thanks in advance dimitris ps this is my table model -------------------------------------------------------------------------- package testapp; import javax.swing.table.*; import java.util.Vector; public class DataModel extends AbstractTableModel { protected String[] columnNames; protected Vector data; protected Class[] columnClasses = new Class[] {String.class, Boolean.class}; public DataModel(Vector data, String[] columnNames) { this.data = data; this.columnNames = columnNames; fireTableStructureChanged(); } public int getRowCount() { return data.size(); } public int getColumnCount() { return columnNames.length; } public Object getValueAt(int rowIndex, int columnIndex) { switch (columnIndex) { case 0: return data.elementAt(rowIndex); case 1: return (Object) new Boolean(true); default: return null; } } public void removeValueAt(int rowIndex, int columnIndex) { data.removeElementAt(rowIndex); fireTableRowsDeleted(rowIndex, columnIndex); } public void insertElementAt(Object o, int index){ this.data.insertElementAt(o, index); fireTableRowsInserted(index, index); } public void append(Object o) { this.data.add(o); fireTableRowsInserted(data.size()-1, data.size()-1); } public String getColumnName(int columnIndex) { return columnNames[columnIndex]; } public Class getColumnClass(int col) { return columnClasses[col]; } } ---------------------------------------- and this is how i call it from the mainFrame Vector data = new Vector(); String[] columnNames = new String[] {"column 1", "column 2"}; DataModel dm = new DataModel(data, columnNames) JTable table = new JTable(dm) , yeap that was it. I needed either a scrollpane or explicitly get the headers there. Thanx for the help. good day
VIEWS ON THIS POST

213

Posted on:

Thursday 18th October 2012
View Replies!

Can we set an image in a cell of a jtable.

[color=red][/color][size=24][/size][size=18][/size][size=24]Can we set an image in a cell of a jtable.[/size]
VIEWS ON THIS POST

211

Posted on:

Sunday 21st October 2012
View Replies!

update rows in jtable

I can update rows in database, but in jTable I see the changes only after I close the application and rerun it. What should I do This is my code: private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) { try { Class.forName("org.apache.derby.jdbc.ClientDriver"); Connection con = (Connection) DriverManager.getConnection("jdbc:derby://localhost:1527/Proiect","aneresia","aneresia"); int rowNumber=jTable1.getSelectedRow()+1; // Statement stmt1 = con.createStatement(); // int rowsUpdated; con.setAutoCommit(false); stmt1.executeUpdate("Update Carti set nume_autor='"+jTextField2.getText()+"' , pren_autor='"+jTextField3.getText()+"', nume_carte='"+(String)jTextField4.getText()+ "' , nr_car_stoc="+jTextField5.getText()+" ,editura='"+jTextField6.getText()+"' , an_aparitie="+jTextField7.getText()+ " , pret_v="+jTextField8.getText()+" where cod_carte="+rowNumber); model.fireTableRowsUpdated(rowNumber, rowNumber); con.commit(); con.close(); } catch (SQLException ee) { ee.printStackTrace(); } catch (Exception f) { } }
VIEWS ON THIS POST

242

Posted on:

Sunday 21st October 2012
View Replies!

Ordering jtable

Hi all, I have a JTable with just one column. Every cell contains a picture thumbnail with a name and a timestamp below. The pictures themselves are represented by JPA Entities, which have tags associated to them (a comma separated String, which does not appear in the table). Now I want my user to be able to sort the table based on the timestamp or the tag String. I would like a popup menu that lets the user select the way he want the ordering to take place. What would be the best way to implement this I can compare Strings or timestamps, but just making my picture Entity implement Comparable won't do the trick, because that won't let me choose what field should be used to compare the pictures.
VIEWS ON THIS POST

159

Posted on:

Monday 22nd October 2012
View Replies!

Calculate sum in jtable and display the sum in jtextfield

Hi all,i got a table that contains codes,names,qty,price and totals.What i want to ask is how to calculate the qty*price and the total was putted in the the totals column and also putted in the JTextField..When there are new rows,or deleted rows,it will automatically edited in the JTextField.
VIEWS ON THIS POST

194

Posted on:

Monday 22nd October 2012
View Replies!

jcombo box and jtable

, I'm fairly new to Java (so please bear with me ) and am learning to use swing. I've created a simple JFrame. I have a JComboBox in the frame and below this a JTable. I have some values (a series of name) added to the JComboBox and when I select a value, such as the name 'Mary' then I want their information (grade etc) to appear in the columns of the table. So far, I've made little or no progress. I can get the chosen value from the JComboBox to appear in a textfield, but not in the table. Do you think I am going to right way Any advice would be great.
VIEWS ON THIS POST

162

Posted on:

Monday 22nd October 2012
View Replies!

color in jtable cell

i have this code (to be entirely frank i cant recall where i found it), that paints every other line with a diferent color Color cor = table.getBackground(); if((row%2) == 0 && cor.getRed() > 20 && cor.getGreen() > 20 && cor.getBlue() > 20) setBackground(new Color(cor.getRed() - 20, cor.getGreen() - 20, cor.getBlue() - 20)); else setBackground(cor)
VIEWS ON THIS POST

153

Posted on:

Monday 22nd October 2012
View Replies!