JTable related problems



I am using JTable with 3 columns. I have added table to JScrollPane. I have also added scrollbars to table, and I add rows dynamically ,now My problems are -

1) I want my column should take size equal to highest width of column contents.

2) I want to select column when I click on column and press control key.
I have added selection model to table.

3) My table should have white background. I have tried to set background of
table as well as scrollpane but its not working.

any help....
Posted On: Thursday 18th of October 2012 01:23:49 AM Total Views:  151
View Complete with Replies

RELATED TOPICS OF Swing / AWT / SWT PROGRAMMING LANGUAGE




create a jtable which can combine and split and the image change also

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

231

Posted on:

Wednesday 19th September 2012
View Replies!

Is ActionListener and Key Listener related?

I coded an application where there is 2 textfields having keylistener and a submit button with action listener. For key and action listener, here is the coding public void keyPressed(KeyEvent e) { if(e.getSource()==currentPaymenttext){ if(e.getKeyCode()==KeyEvent.VK_ENTER){ //on pressing enter in the textfield perform some code related to currentPaymenttext } } if(e.getSource()==enrollmentNotext){ if(e.getKeyCode()==KeyEvent.VK_ENTER){ //on pressing enter in the textfield perform some code related to enrollmentNotext } } } public void actionPerformed(ActionEvent ae) { if(ae.getActionCommand().equals("Pay")){ //on action through pay, perform this } }
VIEWS ON THIS POST

49

Posted on:

Saturday 22nd September 2012
View Replies!

jtable, defaulttable model

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

232

Posted on:

Monday 24th September 2012
View Replies!

Generating pdf from jtable

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 java.io.FileOutputStream; 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")); document.open(); 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(); } }
VIEWS ON THIS POST

216

Posted on:

Monday 24th September 2012
View Replies!

Create and dynamically change buttons in jtable column

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

287

Posted on:

Monday 24th September 2012
View Replies!

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

342

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

291

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

295

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

308

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

267

Posted on:

Wednesday 17th October 2012
View Replies!

Scroll panel problems

I am having alot of problems getting this scroll panel to work. Specifically I have the [panel on my GUI, but I cant figure out how to get the text area to populate it. Can someone please tell me what I a,m doing wrong here. These are the code portions that pertain to the scroll panel only. private JTextArea jtextarea1;//define textarea private JScrollPane scrollpane1;
VIEWS ON THIS POST

228

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

271

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

520

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

371

Posted on:

Wednesday 17th October 2012
View Replies!

problems the program

I am having problems with this program. When I run javac I am getting e errors about illegal start of expression. import java.awt.*; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Calculator2 extends JFrame implements ActionListener { private JPanel panelAdder; private JLabel labela; private JLabel labelt; private JLabel labelr; private JTextField textFieldAmount; private JTextField textFieldTerm; private JTextField textFieldRate; private JTextField textFieldResult; private JButton buttonCalc; private JButton buttonClear; public Calculator2() { super("Calculator"); initComponents(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); pack(); // Add Listeners buttonCalc.addActionListener(this); buttonClear.addActionListener(this); } public void initComponents() { //Initialize Components panelAdder = new JPanel(); labela = new JLabel("Amount"); textFieldAmount = new JTextField(); labelt = new JLabel("Term"); textFieldTerm = new JTextField(); labelr = new JLabel("Rate"); textFieldRate = new JTextField(); textFieldResult = new JTextField(); button = new JButton("Calculate"); JTextFieldClear = new JTextField(); button = new JButton("Clear"); button = new Jbutton("Exit"); } public void actionPerformed(ActionEvent event) { Object source = event.getSource (); if (source == buttonCalc); { setResultValue(); } if (source == buttonClear); { newJTextField (""); } //Set Object Attributes textFieldResult.setEditable(false); textFieldResult.setColumns(8); textFieldAmount.setColumns(6); textFieldTerm.setColumns(2); textFieldRate.setColumns(2); Container contentPane = getContentPane(); contentPane.setLayout(new FlowLayout()); // add the components to the panel panelAdder.add(labela); panelAdder.add(textFieldAmount); panelAdder.add(labelt); panelAdder.add(textFieldTerm); panelAdder.add(labelr); panelAdder.add(textFieldRate); panelAdder.add(textFieldResult); contentPane.add(panelAdder); setVisible(true); [B]public void setResultValue(); [/B]{ double amount = Double.parseDouble (textFieldAmount.getText()); double rate = Double.parseDouble (textFieldRate.getText()); double term = Integer.parseInt(textFieldTerm.getText()); double result = amount * ( rate * Math.pow ( ( 1 + rate ), term ) ) / ( Math.pow( ( 1 + rate ), term ) - 1 ); textFieldResult.setText(Double.toString(result)); } public static void main(String[] arguments) { Calculator frame = new Calculator(); } }
VIEWS ON THIS POST

137

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

219

Posted on:

Wednesday 17th October 2012
View Replies!

JTabbedPane problems - not loading correctly

I think i don't have something set correctly, but when I try to use JTabbedPanes it never loads correctly. If I have 3 tabs, tab 3 loads up first and I have to select all the tabs at least once before it works correctly. Here is an example of what i mean. I used this program written by Dave Marshall to see where i was messing up, but i get the same problem when i run it.m Any help would be great! -------------------------- import java.awt.*; import javax.swing.*; class TabbedPaneExample extends JFrame { privateJTabbedPane tabbedPane; privateJPanelpanel1; privateJPanelpanel2; privateJPanelpanel3; public TabbedPaneExample() { setTitle( "Tabbed Pane Application" ); setSize( 300, 200 ); setBackground( Color.gray ); JPanel topPanel = new JPanel(); topPanel.setLayout( new BorderLayout() ); getContentPane().add( topPanel ); // Create the tab pages createPage1(); createPage2(); createPage3(); // Create a tabbed pane tabbedPane = new JTabbedPane(); tabbedPane.addTab( "Page 1", panel1 ); tabbedPane.addTab( "Page 2", panel2 ); tabbedPane.addTab( "Page 3", panel3 ); topPanel.add( tabbedPane, BorderLayout.CENTER ); } public void createPage1() { panel1 = new JPanel(); panel1.setLayout( null ); JLabel label1 = new JLabel( "Username:" ); label1.setBounds( 10, 15, 150, 20 ); panel1.add( label1 ); JTextField field = new JTextField(); field.setBounds( 10, 35, 150, 20 ); panel1.add( field ); JLabel label2 = new JLabel( "Password:" ); label2.setBounds( 10, 60, 150, 20 ); panel1.add( label2 ); JPasswordField fieldPass = new JPasswordField(); fieldPass.setBounds( 10, 80, 150, 20 ); panel1.add( fieldPass ); } public void createPage2() { panel2 = new JPanel(); panel2.setLayout( new BorderLayout() ); panel2.add( new JButton( "North" ), BorderLayout.NORTH ); panel2.add( new JButton( "South" ), BorderLayout.SOUTH ); panel2.add( new JButton( "East" ), BorderLayout.EAST ); panel2.add( new JButton( "West" ), BorderLayout.WEST ); panel2.add( new JButton( "Center" ), BorderLayout.CENTER ); } public void createPage3() { panel3 = new JPanel(); panel3.setLayout( new GridLayout( 3, 2 ) ); panel3.add( new JLabel( "Field 1:" ) ); panel3.add( new TextArea() ); panel3.add( new JLabel( "Field 2:" ) ); panel3.add( new TextArea() ); panel3.add( new JLabel( "Field 3:" ) ); panel3.add( new TextArea() ); } // Main method to get things started public static void main( String args[] ) { // Create an instance of the test application TabbedPaneExample mainFrame= new TabbedPaneExample(); mainFrame.setVisible( true ); } }
VIEWS ON THIS POST

153

Posted on:

Wednesday 17th October 2012
View Replies!

JScrollPane and paint method problems

Currently, I'm trying to make a program to handle double elimination brackets for a team tournament. One of my classes involves overwritting the paint method in order to display the brackets. The code is as follows: public void paint(Graphics g) { super.paint(g); drawPortion(bracket.getBracket(), getWidth()/2, 10, bracket.getDegree(), g); } private void drawPortion(BinaryTreeNode b, int x, int y, int degree, Graphics g) { drawPortionRecursive(bracket.getBracket(), getWidth()/2, 10, degree, g, 1); }
VIEWS ON THIS POST

157

Posted on:

Thursday 18th 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

268

Posted on:

Thursday 18th October 2012
View Replies!

Swing related

I am using drawRect method in the Graphics to draw a rectangle. No I want to put text inside the rectangle. Is there any way to do that so that the text gets wrapped up automatically when it finds the end of the rectangle
VIEWS ON THIS POST

38

Posted on:

Thursday 18th October 2012
View Replies!