Results 1 to 11 of 11
  1. #1
    brojowl is offline Member
    Join Date
    Mar 2013
    Location
    Jakarta
    Posts
    5
    Rep Power
    0

    Unhappy how to create a button that has a certain condition

    I apologize for my english is not good, in stating the question

    I am just learning java using netbeans and I want to make a booking application using netbeans

    how to create a button that has a certain condition-java3.jpg

    if the button is in the press, into the room booking form ... and have conditions such as:
    1. empty room color = green button
    2. room filled with color = blue button

    funding conditions and filled the empty rooms on the status of the MySQL database tables

    I apologize if the question is poorly understood, because I am just learning java programming

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,376
    Rep Power
    5

    Default Re: how to create a button that has a certain condition

    From the onset it looks like you have two things to solve. Working with buttons and working with a MySQL database. First, you should try some test examples with buttons. See JFrame, JPanel, JButton, AbstractButton, Layout Managers as well as the actionListeners. I believe NetBeans with its GUI assist may do a lot of that for you but should still know what is going on. Check out the tutorial in my signature on this. Once you feel comfortable with the buttons and the frames and panels that support them, try setting and retrieving DB entries using preset commands in a Java app and printing out the results. Once you feel comfortable with that, you can integrate the two together.

    During each part of the above you can post any code you are having problems with and folks in the forum will be glad to assist.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: how to create a button that has a certain condition

    Use JToggleButtons.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    brojowl is offline Member
    Join Date
    Mar 2013
    Location
    Jakarta
    Posts
    5
    Rep Power
    0

    Default Re: how to create a button that has a certain condition

    I tried in php programming and successful results, and I can be a chore campus about java programming ... and I want to implement it into the java programming

    in this case my expertise in java programming is still very low

    Can you provide references or anything about my question

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: how to create a button that has a certain condition

    Quote Originally Posted by brojowl View Post
    Can you provide references or anything about my question
    1. There's a link in Jim's signature. Try clicking it.
    2. Bookmark this: Java Platform SE 7

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    brojowl is offline Member
    Join Date
    Mar 2013
    Location
    Jakarta
    Posts
    5
    Rep Power
    0

    Default Re: how to create a button that has a certain condition

    I am trying to create a code like this, please correct my code

    Java Code:
    package latihan2;
    
    import java.sql.Connection;
    import java.sql.SQLException;
    import java.sql.PreparedStatement;
    import java.sql.Statement;
    import java.sql.ResultSet;
    
    import java.awt.FlowLayout;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    
    public class brojowl extends JFrame {
    
        private Connection koneksi;
    
        //data
        private void loadData() {
            try {
                koneksi = koneksiDB.getConnection();
                String query = "SELECT * FROM kamar";
    
                Statement statement = koneksi.createStatement();
                ResultSet resultSet = statement.executeQuery(query);
    
                //mengisi tabel dengan data yang di seleksi dari database
                while (resultSet.next()) {
                    Object[] kamar = new Object[3];
                    kamar[0] = resultSet.getString("id_kamar");
                    kamar[1] = resultSet.getString("nm_kamar");
                    kamar[2] = resultSet.getString("status");
                }
    
                resultSet.close();//menutup result set
                statement.close();//menutup statement
            } catch (SQLException ex) {
                System.out.println(ex.getMessage());
            }
        }
         
        //sample data
        int[] dataRoom = {1,0,2};
    
        final JButton buttonRoom1 = new JButton("Room 1 (S)");
        final JButton buttonRoom2 = new JButton("Room 2 (V)");
        final JButton buttonRoom3 = new JButton("Room 3 (F)");
    
        public brojowl() {
            setSize(300, 150);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setLayout(new FlowLayout());
            add(buttonRoom1);
            add(buttonRoom2);
            add(buttonRoom3);
    
            int status1 = dataRoom[0];
            int status2 = dataRoom[1];
            int status3 = dataRoom[2];
    
            if( status1 == 0 ){
                //warna default
            } else if (status1 == 1) {
                buttonRoom1.setBackground(new java.awt.Color(51, 255, 51)); //warna hijau
                buttonRoom1.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent ev) {
                        //event disini
                    }
                });
            } else {
                buttonRoom1.setBackground(new java.awt.Color(255, 0, 0)); //warna merah
            }
    
            if( status2 == 0 ){
                //warna default
            } else if (status2 == 1) {
                buttonRoom2.setBackground(new java.awt.Color(51, 255, 51)); //warna hijau
                buttonRoom2.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent ev) {
                        //event disini
                    }
                });
            } else {
                buttonRoom2.setBackground(new java.awt.Color(255, 0, 0)); //warna merah
            }
    
            if( status3 == 0 ){
                //warna default
            } else if (status3 == 1) {
                buttonRoom3.setBackground(new java.awt.Color(51, 255, 51)); //warna hijau
                buttonRoom3.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent ev) {
                        //event disini
                    }
                });
            } else {
                buttonRoom3.setBackground(new java.awt.Color(255, 0, 0)); //warna merah
            }
    
            setVisible(true);
        }
    
        public static void main(String[] args) {
            new brojowl();
        }
    }

    I want this data results from a database table

    Java Code:
    //sample data
    int[] dataRoom = {1,0,2};


    Regards,
    Brojowl

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default Re: how to create a button that has a certain condition

    Where are you stuck?
    I can see code that reads rows from a database table.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    brojowl is offline Member
    Join Date
    Mar 2013
    Location
    Jakarta
    Posts
    5
    Rep Power
    0

    Default Re: how to create a button that has a certain condition

    this code I would like to take the data one by one

    Java Code:
    //data
        private void loadData() {
            try {
                koneksi = koneksiDB.getConnection();
                String query = "SELECT * FROM kamar";
     
                Statement statement = koneksi.createStatement();
                ResultSet resultSet = statement.executeQuery(query);
     
                //mengisi tabel dengan data yang di seleksi dari database
                while (resultSet.next()) {
                    Object[] kamar = new Object[3];
                    kamar[0] = resultSet.getString("id_kamar");
                    kamar[1] = resultSet.getString("nm_kamar");
                    kamar[2] = resultSet.getString("status");
                }
     
                resultSet.close();//menutup result set
                statement.close();//menutup statement
            } catch (SQLException ex) {
                System.out.println(ex.getMessage());
            }
        }
    what can it be? use an array like this

    Java Code:
    //sample data
    int[] dataRoom = {1,0,2};

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default Re: how to create a button that has a certain condition

    That code, at the moment, does nothing with the data it reads.
    Java Code:
    while (resultSet.next()) {
                     Object[] kamar = new Object[3];
                     kamar[0] = resultSet.getString("id_kamar");
                     kamar[1] = resultSet.getString("nm_kamar");
                     kamar[2] = resultSet.getString("status");
                 }
    It is populating an Object[] which only lives inside the loop.
    That array is gone at the end of the loop.
    So you need to populate a different array in there.
    One that you can then use elsewhere in your code.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    brojowl is offline Member
    Join Date
    Mar 2013
    Location
    Jakarta
    Posts
    5
    Rep Power
    0

    Default Re: how to create a button that has a certain condition

    example of the contents of the data in the column "status", there are 5 rows contents

    1
    2
    0
    0
    1

    and the data that I create each variable, how should I take the data and variables used

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: how to create a button that has a certain condition

    brojowl, are you even reading the responses? If you don't understand some of the advice and remarks, ask for clarification -- don't waffle on and on about things that are irrelevant to your stated goal.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. How do you create a save button?
    By christopherq398 in forum New To Java
    Replies: 4
    Last Post: 04-04-2012, 04:39 PM
  2. Create a Search Button - Java
    By scurvyjones in forum New To Java
    Replies: 7
    Last Post: 12-08-2010, 12:40 PM
  3. How to create Java Button Link
    By Louis in forum New To Java
    Replies: 3
    Last Post: 11-17-2010, 07:29 AM
  4. How to create a Ploygon Button
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-26-2008, 07:39 PM
  5. Dynamically create a button, but what happened?
    By love2java in forum AWT / Swing
    Replies: 1
    Last Post: 02-17-2008, 12:01 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •