Button.java import java.awt.FlowLayout; import java.awt.Graphics2D; import java.
ID: 3754847 • Letter: B
Question
Button.java
import java.awt.FlowLayout;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.*;
import java.awt.color.*;
import javax.swing.Icon;
import javax.swing.JPanel;
import javax.swing.JLabel;
/**
* Class for the Application of this program including the createButton method
* for returning new buttons with action listeners
*
*/
public class Button {
/**
* @param args the command line arguments
*/
/**
* Creates and returns a JButton with an ActionListener which is an
* anonymous class
*
* @param i index in outer for loop
* @param label label used to call repaint() to initiate color change
* @param icon Custom Icon object used to display circle and change colors
* @return JButton object used to change icon to color designated by the
* button
*/
public static JButton createButton(final int i, final JLabel label, final CustomIcon icon) {
final String[] colors = new String[]{"RED", "GREEN", "BLUE"};
JButton button = new JButton(colors[i]);
button.addActionListener(new ActionListener() {
/**
* The function called when a button is clicked, will store the
* selected color and then repaint the icon
*
* @param event ActionEvent object holding the data describing the
* event
*/
public void actionPerformed(ActionEvent event) {
icon.fillPaint(colors[i]);
label.repaint();
}
});
return button;
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setLayout(new FlowLayout());
final int FIELD_WIDTH = 20;
final JTextField textField = new JTextField(FIELD_WIDTH);
JButton green = new JButton();
JButton red = new JButton();
JPanel panel = new JPanel();
JButton blue = new JButton();
JButton btn[] = {green, red, blue};
CustomIcon icon = new CustomIcon(50);
JLabel label = new JLabel(icon);
panel.add(label);
Color c;
for (int i = 0; i < 3; i++) {
btn[i] = createButton(i, label, icon);
panel.add(btn[i]);
}
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
frame.setLayout(new FlowLayout());
}
}
CustomIcon.java
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import javax.swing.Icon;
/**
* Class that implements Icon and overrides its paintIcon method along with
* having a fillPaint method for changing selected colors
*/
public class CustomIcon implements Icon {
private int size;
private Color color;
/**
* Constructs a Icon object initializing size with the parameter x and
* initializes color with Red
*
* @param x the int containing the size
*/
CustomIcon(int x) {
size = x;
color = Color.RED;
}
/**
* Designs the Icon for displaying using the desired size and color
*
* @param c the Component to be used
* @param g the Graphics object to have its color set
* @param x int representing desired width
* @param y int representing desired height
*/
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
Graphics2D g2 = (Graphics2D) g;
Ellipse2D.Double custom = new Ellipse2D.Double(x, y, size, size);
g2.setColor(color);
g2.fill(custom);
}
/**
* Assigns the paint color to the color property of the Icon object
*
* @param s the String containing the desired color
*/
public void fillPaint(String s) {
if (s == "RED") {
color = Color.RED;
} else if (s == "GREEN") {
color = Color.GREEN;
} else if (s == "BLUE") {
color = Color.BLUE;
}
}
/**
* gets size of icon
*
* @return int representing icon size
*/
@Override
public int getIconWidth() {
return size;
}
/**
* gets size of icon
*
* @return int representing icon size
*/
@Override
public int getIconHeight() {
return size;
}
}
RED GREEN BLUE import import java.awt.GraphicS2D: import java.awt.event.ActionEvent: import java. awt.event.ActionListener; import javax. swing.JButton java.awt . FlowLayout; 8 import javax. swing. JFrame; 9import javax.swing. JTextField: 0import java. awt. *; import java. awt.color.*; import javax. swing.Icon; 3import javax.swing.JPanel; OutputExplanation / Answer
DatabaseContentDemo.java
import com.mysql.jdbc.Connection;//jdbc connection package
import java.awt.BorderLayout;//layout packages
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;//action listentenr packages
import java.awt.event.ActionListener;
import java.sql.DriverManager;//sql packages
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import javax.swing.JButton;//jcomponent packages
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
class DatabaseContent extends JFrame implements ActionListener {
private Connection dbConnection;
private JTextField tableNameField = new JTextField(30);
private JTable resultsTable = new JTable(new DefaultTableModel());
// Initialize the layout
public DatabaseContent() {//constructor
setTitle("Exercise35_03");
setSize(800, 500);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JPanel inputPanel = new JPanel(new FlowLayout());
add(BorderLayout.NORTH, inputPanel);
inputPanel.add(new JLabel("Table Name "));
inputPanel.add(tableNameField);
JButton showContentsButton = new JButton("Show Contents");
showContentsButton.addActionListener(this);
inputPanel.add(showContentsButton);
add(BorderLayout.CENTER, new JScrollPane(resultsTable));
// Connect to the database
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
dbConnection = (Connection) DriverManager.getConnection( "jdbc:mysql://localhost/CODINGGROUND", "root", "root");
} catch (Exception e) {
e.printStackTrace(System.out);
System.exit(0);
}
}
// Handle the event of the button
@Override
public void actionPerformed(ActionEvent e) {
String tableName = tableNameField.getText().trim();
if (tableName.isEmpty()) {
JOptionPane.showMessageDialog(this, "Please enter a table name.");
return;
}
try {
// Query all the details of the table
PreparedStatement ps = dbConnection.prepareStatement("SELECT * FROM " + tableName);
ResultSet rs = ps.executeQuery();
ResultSetMetaData setMetaData = rs.getMetaData();
DefaultTableModel model = new DefaultTableModel();
for (int col = 0; col < setMetaData.getColumnCount(); col++) {
model.addColumn(setMetaData.getColumnName(col + 1));
}
while(rs.next()) {
Object[] row = new Object[setMetaData.getColumnCount()];
for (int i = 0; i < row.length; i++) {
row[i] = rs.getObject(i + 1);
}
model.addRow(row);
}
resultsTable.setModel(model);
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, ex.getMessage());
}
}}
// Entry point of the program
public class DatabaseContentDemo{//main method
public static void main(String[] args) {
JFrame f=new JFrame();
DatabaseContent d=new DatabaseContent();
d.setVisible(true);
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.