I have small app that has two buttons on the first TAB: CONNECT and DISCONNECT. On intial entry, the app needs to CONNECT, and there is nothing to DISCONNECT. Once connnected, I want to allow it to DISCONNECT, but not connect. Once disconnected, it can CONNECT again, and so on.
On the second TAB, there is a SUBMIT button and a JTextArea. With my current logic, if "connected", then SUBMIT is enabled.
I want to add additional logic here as well. If there is no text in the JTextArea, then the SUBMIT button should be disabled, regardless of the connection status.
Now, if you look at my code, you'll see I made all concerned JButtons class variables. I'm sure there is a better way, but I'm not sure how to access each button in the Adapter class's actionPerformed methods I'm using at the bottom of the listing. If someone could point me in the right direction here, I could probably figure out the rest.
Thanks again for your time.
Todd
Here's the code (file name MySLO.java)
On the second TAB, there is a SUBMIT button and a JTextArea. With my current logic, if "connected", then SUBMIT is enabled.
I want to add additional logic here as well. If there is no text in the JTextArea, then the SUBMIT button should be disabled, regardless of the connection status.
Now, if you look at my code, you'll see I made all concerned JButtons class variables. I'm sure there is a better way, but I'm not sure how to access each button in the Adapter class's actionPerformed methods I'm using at the bottom of the listing. If someone could point me in the right direction here, I could probably figure out the rest.
Thanks again for your time.
Todd
Here's the code (file name MySLO.java)
Code:
// My Simpler Lay Out Test.
public class MySLO extends javax.swing.JFrame {
static javax.swing.JTextArea myText ;
static javax.swing.JButton buttonSubmit ;
static javax.swing.JButton buttonConnect;
static javax.swing.JButton buttonDisconnect ;
// Create a window with all the GUI stuff, and display it.
public static void main(String[] args) {
javax.swing.JFrame win = new MySLO() ;
win.setVisible(true) ; // make window appear
}
// This is the GUI window maker. It builds the buttons, does the layout,
// registers for events, etc...
public MySLO() {
super("JTabbedLayout") ; // This is the window title
// Get the JFrame's container.
java.awt.Container content = this.getContentPane() ;
this.setSize(700,400) ;
// Connect and Disconnnect Buttons.
buttonConnect = new javax.swing.JButton("Connect") ;
buttonConnect.addActionListener(new ButtonConnect(this) ) ;
content.add(buttonConnect) ;
buttonDisconnect = new javax.swing.JButton("Disconnect") ;
buttonDisconnect.addActionListener(new ButtonDisconnect(this) ) ;
content.add(buttonDisconnect) ;
myText = new javax.swing.JTextArea(10,45) ;
myText.setLineWrap(false) ;
myText.setTabSize(3) ;
myText.setText("") ;
javax.swing.JScrollPane jsc = new javax.swing.JScrollPane(myText) ;
jsc.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS) ;
jsc.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED) ;
javax.swing.JButton buttonClear = new javax.swing.JButton("Clear") ;
buttonClear.addActionListener(new ButtonClear(this) ) ;
content.add(buttonClear) ;
buttonSubmit = new javax.swing.JButton("Submit") ;
buttonSubmit.addActionListener(new ButtonSubmit(this) ) ;
content.add(buttonSubmit);
this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE) ; // Close app when RED X is clicked.
javax.swing.JPanel pan1 = new javax.swing.JPanel() ;
pan1.add(buttonConnect) ;
pan1.add(buttonDisconnect) ;
javax.swing.JPanel pan2 = new javax.swing.JPanel() ;
pan2.add(jsc) ;
pan2.add(buttonSubmit) ;
pan2.add(buttonClear) ;
//javax.swing.JPanel pan3 = new javax.swing.JPanel() ;
//pan3.add(new javax.swing.JLabel("Output...") );
javax.swing.JTabbedPane jtp = new javax.swing.JTabbedPane() ; ;
jtp.addTab("Connections" , pan1 ) ;
jtp.addTab("Edit SQL", pan2 ) ;
content.add(jtp) ; // add the JTabbedPane to the container
this.pack() ; // Resize the window as needed.
// Center the Dialog on the screen.
java.awt.Dimension screen = java.awt.Toolkit.getDefaultToolkit().getScreenSize() ;
int x = (screen.width - this.getWidth() ) / 2 ;
int y = (screen.height - this.getHeight() ) / 2 ;
setBounds(x,y,this.getWidth(),this.getHeight()) ;
}
// Adapter Classes - to separate the GUI from the application logic (not that there IS any appl. logic yet...
class ButtonClear implements java.awt.event.ActionListener {
MySLO data ;
ButtonClear (MySLO data ) {
this.data = data ;
}
public void actionPerformed( java.awt.event.ActionEvent ae) {
data.myText.setText("") ;
System.out.println("Input data has been cleared") ;
buttonSubmit.setEnabled(false) ;
}
}
class ButtonSubmit implements java.awt.event.ActionListener {
MySLO data ;
ButtonSubmit (MySLO data ) {
this.data = data ;
buttonSubmit.setEnabled(false) ;
}
public void actionPerformed( java.awt.event.ActionEvent ae) {
System.out.println("Submit Button") ;
}
}
class ButtonConnect implements java.awt.event.ActionListener {
MySLO data ;
ButtonConnect (MySLO data ) {
this.data = data ;
buttonConnect.setEnabled(true) ;
}
public void actionPerformed( java.awt.event.ActionEvent ae) {
System.out.println("Connect Button") ;
buttonDisconnect.setEnabled(true) ;
buttonConnect.setEnabled(false) ;
buttonSubmit.setEnabled(true) ;
}
}
class ButtonDisconnect implements java.awt.event.ActionListener {
MySLO data ;
ButtonDisconnect (MySLO data ) {
this.data = data ;
buttonDisconnect.setEnabled(false) ;
}
public void actionPerformed( java.awt.event.ActionEvent ae) {
System.out.println("Disconnect Button") ;
buttonConnect.setEnabled(true) ;
buttonDisconnect.setEnabled(false) ;
buttonSubmit.setEnabled(false) ;
}
}
}