Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > Java Programming > Java Forum > เคลียข้อมูลจากjtableให้แสดงข้อมูลใหม่ยังไงหรอครับช่วยที



 

เคลียข้อมูลจากjtableให้แสดงข้อมูลใหม่ยังไงหรอครับช่วยที

 



Topic : 128463



โพสกระทู้ ( 2 )
บทความ ( 0 )



สถานะออฟไลน์
Facebook



Code
package newpackage;
import java.awt.EventQueue;
import javax.swing.JLabel;
import javax.swing.JFrame;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import javax.swing.JRadioButton;
import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.*;
/**
/**
 *
 * @author Aong
 */
public class NewJFrame extends javax.swing.JFrame {

    /**
     * Creates new form NewJFrame
     */
    //public NewJFrame() {
   //     initComponents();
   // }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                 NewJFrame frame = new NewJFrame();
                frame.setVisible(true);
            }
        });
    }
public NewJFrame() {
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 1000, 700);
		setTitle("เลือกราคา");
		getContentPane().setLayout(null);
		
                JLabel lblSearch = new JLabel("โปรดระบุช่วงราคา  :");
		lblSearch.setBounds(150, 60,200, 14);
		getContentPane().add(lblSearch);
                
                JLabel lblSearch1 = new JLabel("โปรระบุอุปกรณ์ที่ต้องการ  :");
		lblSearch1.setBounds(150,180,200, 14);
		getContentPane().add(lblSearch1);
                
                JLabel lblSearch2 = new JLabel("โปรระบุยี่ห้อที่ต้องการ  :");
		lblSearch2.setBounds(150, 290,200, 14);
		getContentPane().add(lblSearch2);
		
		// Radio Button
		final JRadioButton radio1 = new JRadioButton("ราคาน้อยกว่า1000");
		radio1.setBounds(119, 90, 150, 23);
		getContentPane().add(radio1);
		
		final JRadioButton radio2 = new JRadioButton("ราคาไม่เกิน2000");
		radio2.setBounds(119,120, 150, 23);
		getContentPane().add(radio2);
		
		final JRadioButton radio3 = new JRadioButton("ราคามากกว่า2000");
		radio3.setBounds(119,150, 150, 23);
		getContentPane().add(radio3);
                
		
                final JRadioButton radio4 = new JRadioButton("Harddisk");
		radio4.setBounds(119, 200, 150, 23);
		getContentPane().add(radio4);
                
                final JRadioButton radio5 = new JRadioButton("Display Card");
		radio5.setBounds(119, 230, 150, 23);
		getContentPane().add(radio5);
                
                    final JRadioButton radio6 = new JRadioButton("Ram");
		radio6.setBounds(119, 260, 150, 23);
		getContentPane().add(radio6);
                
                
                     final JRadioButton radio7 = new JRadioButton("Ram");
		radio7.setBounds(119,310, 150, 23);
		getContentPane().add(radio7);
                
                     final JRadioButton radio8 = new JRadioButton("Ram");
		radio8.setBounds(119, 340, 150, 23);
		getContentPane().add(radio8);
                
                     final JRadioButton radio9 = new JRadioButton("Ram");
		radio9.setBounds(119, 370, 150, 23);
		getContentPane().add(radio9);
                
                  final JRadioButton radio10 = new JRadioButton("Ram");
		radio10.setBounds(119, 400, 150, 23);
		getContentPane().add(radio10);
                

		// Set Group
		ButtonGroup group = new ButtonGroup();
		group.add(radio1);
        group.add(radio2);
        group.add(radio3);
        
        	ButtonGroup group2 = new ButtonGroup();
		group2.add(radio4);
        group2.add(radio5);
        group2.add(radio6);
        
        	ButtonGroup group3 = new ButtonGroup();
		group3.add(radio7);
        group3.add(radio8);
        group3.add(radio9);
           group3.add(radio10);
           
           // ScrollPane
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(300,100, 600, 400);
		getContentPane().add(scrollPane);
		
		// Table
		JTable table = new JTable();
		scrollPane.setViewportView(table);
		
                	DefaultTableModel model = (DefaultTableModel)table.getModel();
	        model.addColumn("id");
		model.addColumn("name");
		model.addColumn("price");
             
                
              
		// Button
		JButton btn = new JButton("OK");
                 
		btn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
                     
               
				    Connection connect = null;
		Statement s = null;
               
    	if(radio1.isSelected()){
              try {
                    
    
			Class.forName("com.mysql.jdbc.Driver");

			connect =  DriverManager.getConnection("jdbc:mysql://localhost/store" +
					"?user=root&password=1234");
			
			s = connect.createStatement();
			
			String sql = "SELECT * FROM materiel WHERE price between 0 and 1000 ";
			
			ResultSet rec = s.executeQuery(sql);
			int row = 0;
			while((rec!=null) && (rec.next()))
            {			
		model.addRow(new Object[0]);
				model.setValueAt(rec.getString("id"), row, 0);
				model.setValueAt(rec.getString("name"), row, 1);
				model.setValueAt(rec.getString("price"), row, 2);
			
				row++;
                            
                                }
                           model.removeRow(0);
          connect.close();
                        rec.close();
     
		}
              catch (Exception e) {
			// TODO Auto-generated catch block
			JOptionPane.showMessageDialog(null, e.getMessage());
			e.printStackTrace();
		}
		
              
              
            
} else if (radio2.isSelected()) {
    
       try {
                   
			Class.forName("com.mysql.jdbc.Driver");

			connect =  DriverManager.getConnection("jdbc:mysql://localhost/store" +
					"?user=root&password=1234");
			
			s = connect.createStatement();
			
			String sql = "SELECT * FROM materiel WHERE price between 1000 and 2000 ";
			
			ResultSet rec = s.executeQuery(sql);
			int row = 0;
			while((rec!=null) && (rec.next()))
            {			
		model.addRow(new Object[0]);
				model.setValueAt(rec.getString("id"), row, 0);
				model.setValueAt(rec.getString("name"), row, 1);
				model.setValueAt(rec.getString("price"), row, 2);
			
				row++;
                                
                                }
                       
connect.close();
                        rec.close();
                        model.removeRow(0);
                      
           
		} catch (Exception e) {
			// TODO Auto-generated catch block
			JOptionPane.showMessageDialog(null, e.getMessage());
			e.printStackTrace();
                        
		}
		
       
       
       
       
	
   
				} else if (radio3.isSelected()) {
		} else {
					JOptionPane.showMessageDialog(null,
							"You not select.");	
				}
		}
		});
		btn.setBounds(125, 450, 89, 23);
		getContentPane().add(btn);	
            
		
	}
}


สอบถามหน่อยครับจะทำการเคลียข้อมูลเก่ายังไงหรอครับ พอกดปุ่มราคาไม่เกิน2000บาทแล้วกลับมากดราคาไม่เกิน1000มันมีข้อมูลที่หลงเหลืออยู่4-5ข้อมูลที่ราคาเกิน1000ขึ้นไปเข้ามาแสดง ช่วยทีคับ

ไฟลที่1

ไฟลที่2



Tag : Java, MySQL, JAVA







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2017-08-03 01:51:24 By : 1520169821336560 View : 1242 Reply : 2
 

 

No. 1



โพสกระทู้ ( 2 )
บทความ ( 0 )



สถานะออฟไลน์
Facebook

พึ่งลองหัดjavaขอความช่วยเหลือทีครับ






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2017-08-03 01:53:10 By : 1520169821336560
 


 

No. 2



โพสกระทู้ ( 2 )
บทความ ( 0 )



สถานะออฟไลน์


Code (Java)
       DefaultTableModel dtbl = (DefaultTableModel) tableContact.getModel();
       dtbl.setRowCount(0);





Edit:
//ในที่นี้ tableContact คือ JTable ที่มีข้อมูลตาราง Model ครบอยู่แล้ว
//เราไม่สามารถเปลี่ยนข้อมูล ใน Model ได้ตรงๆ
เลยต้องใช้ Class - DefaultTableModel มารับ Model ใน JTable
เพื่อที่จะเปลี่ยนข้อมูลตารางได้


ประวัติการแก้ไข
2017-08-22 16:26:04
2017-08-22 16:30:34
2017-08-22 16:30:54
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2017-08-18 11:10:04 By : trinio
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : เคลียข้อมูลจากjtableให้แสดงข้อมูลใหม่ยังไงหรอครับช่วยที
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 04
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่