Created
March 22, 2018 20:09
-
-
Save max-l/4136b5a0d08a72db03113ddb41491370 to your computer and use it in GitHub Desktop.
PettyPiercingPhp created by max_l - https://repl.it/@max_l/PettyPiercingPhp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.*; | |
import java.awt.event.*; | |
import java.util.ArrayList; | |
import javax.swing.*; | |
import javax.swing.table.AbstractTableModel; | |
/* NOTE: this code CANNOT run in repl.it, because it uses graphics, you need to copy it into an Eclipse project | |
* | |
* 1. Modify the MyDistanceConversionApp class to add the functionality : | |
* | |
* 1.1) add a row for converting to kilometers in the Distance class, adapt the DistancesModel | |
* class so that the JTable displays the new kilometers row | |
* . | |
* | |
* 1.2) When a bad number format is entered in the JTextField, catch the NumberFormatException, and popupMessage() with an | |
* appropriate message | |
* | |
* 1.3) Launch the RadioGroupDemo app, study it, and understand how RadioGroup works, | |
* + then add a RadioGroup to MyDistanceConversionApp with choices "meters", "centimeters", "inches" | |
* + when JButton("new distance") is clicked, create the distance by initializing it's meter member variable | |
* with the proper conversion (depending which "meters", "centimeters", "inches" button is selected) | |
* | |
* 2. Drawing inspiration from MyDistanceConversionApp | |
* | |
* 2.1) create a subclass of MySimpleFrameApp that displays your University (from the UniversityStress lab) | |
* in the form of a JTable, the columns shown will be : "teacher type", age", eccentricities", "respect", "emails" | |
* | |
* Note1: to obtain a displayable string for "teacher type" given a teacher instance, use .getClass().getSimpleName() | |
Note2: You can use the same layout as MyDistanceConversionApp, but you can experiment | |
with others, see https://docs.oracle.com/javase/tutorial/uiswing/examples/layout/index.html#BorderLayoutDemo | |
for layout examples | |
* 2.2) Add a JTextField in the lower panel and a JButton that sends N emails | |
* to all professor when clicked, N is the number typed in the JTextField | |
* (very similar to MyDistanceConversionApp) | |
* | |
* | |
* */ | |
@SuppressWarnings("serial") | |
class DistancesModel extends AbstractTableModel { | |
private ArrayList<Distance> distances = new ArrayList<Distance>(); | |
public DistancesModel() { | |
addDistance(new Distance(1)); | |
addDistance(new Distance(2)); | |
addDistance(new Distance(4)); | |
addDistance(new Distance(23)); | |
} | |
public void addDistance(Distance d) { | |
distances.add(d); | |
} | |
public int getColumnCount() { | |
return 2; | |
} | |
public int getRowCount() { | |
return distances.size(); | |
} | |
public String getColumnName(int col) { | |
switch (col) { | |
case 0: | |
return "meters"; | |
case 1: | |
return "feet"; | |
default: | |
throw new RuntimeException("Invalid col "+ col); | |
} | |
} | |
public Object getValueAt(int row, int col) { | |
Distance d = distances.get(row); | |
switch (col) { | |
case 0: | |
return d.getMeters(); | |
case 1: | |
return d.getFeets(); | |
default: | |
throw new RuntimeException("Invalid col "+ col); | |
} | |
} | |
} | |
class Distance { | |
private float meters; | |
public Distance(float meters) { | |
this.meters = meters; | |
} | |
public float getMeters() { | |
return meters; | |
} | |
public float getFeets() { | |
return meters * 3.28084f; | |
} | |
} | |
abstract class MySimpleFrameApp extends JFrame { | |
public MySimpleFrameApp(String title) { | |
super(title); | |
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
JPanel frameRootPanel = new JPanel(new BorderLayout()); | |
this.setContentPane(frameRootPanel); | |
populateRootPanel(frameRootPanel); | |
this.pack(); | |
this.setVisible(true); | |
} | |
abstract protected void populateRootPanel(JPanel frameRootPanel); | |
} | |
/* See other examples of Layout Managers: | |
* https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html | |
* */ | |
class BorderLayoutDemo extends MySimpleFrameApp { | |
public BorderLayoutDemo() { | |
super("Layout Demo"); | |
} | |
protected void populateRootPanel(JPanel frameRootPanel) { | |
JPanel pane = frameRootPanel ; | |
if (!(pane.getLayout() instanceof BorderLayout)) { | |
pane.add(new JLabel("Container doesn't use BorderLayout!")); | |
return; | |
} | |
JButton button = new JButton("Button 1 (PAGE_START)"); | |
pane.add(button, BorderLayout.PAGE_START); | |
//Make the center component big, since that's the | |
//typical usage of BorderLayout. | |
button = new JButton("Button 2 (CENTER)"); | |
button.setPreferredSize(new Dimension(200, 100)); | |
pane.add(button, BorderLayout.CENTER); | |
button = new JButton("Button 3 (LINE_START)"); | |
pane.add(button, BorderLayout.LINE_START); | |
button = new JButton("Long-Named Button 4 (PAGE_END)"); | |
pane.add(button, BorderLayout.PAGE_END); | |
button = new JButton("5 (LINE_END)"); | |
pane.add(button, BorderLayout.LINE_END); | |
} | |
} | |
class RadioGroupDemo extends MySimpleFrameApp { | |
static String birdString = "Bird"; | |
static String catString = "Cat"; | |
static String dogString = "Dog"; | |
static String rabbitString = "Rabbit"; | |
static String pigString = "Pig"; | |
public RadioGroupDemo() { | |
super("radio group demo"); | |
} | |
protected void populateRootPanel(JPanel frameRootPanel) { | |
//Create the radio buttons. | |
final JRadioButton birdButton = new JRadioButton(birdString); | |
birdButton.setMnemonic(KeyEvent.VK_B); | |
birdButton.setActionCommand(birdString); | |
birdButton.setSelected(true); | |
JRadioButton catButton = new JRadioButton(catString); | |
catButton.setMnemonic(KeyEvent.VK_C); | |
catButton.setActionCommand(catString); | |
JRadioButton dogButton = new JRadioButton(dogString); | |
dogButton.setMnemonic(KeyEvent.VK_D); | |
dogButton.setActionCommand(dogString); | |
JRadioButton rabbitButton = new JRadioButton(rabbitString); | |
rabbitButton.setMnemonic(KeyEvent.VK_R); | |
rabbitButton.setActionCommand(rabbitString); | |
JRadioButton pigButton = new JRadioButton(pigString); | |
pigButton.setMnemonic(KeyEvent.VK_P); | |
pigButton.setActionCommand(pigString); | |
//Group the radio buttons. | |
ButtonGroup group = new ButtonGroup(); | |
group.add(birdButton); | |
group.add(catButton); | |
group.add(dogButton); | |
group.add(rabbitButton); | |
group.add(pigButton); | |
// guess what this does: | |
rabbitButton.setSelected(true); | |
// and this: group.clearSelection(); | |
// we create a single ActionListener that we will assign to each radio buttons | |
ActionListener al = new ActionListener() { | |
public void actionPerformed(ActionEvent ev) { | |
if(ev.getSource() == birdButton) { | |
System.out.println("bird !!!"); | |
} | |
else { | |
System.out.println("not bird ..."); | |
} | |
} | |
}; | |
birdButton.addActionListener(al); | |
catButton.addActionListener(al); | |
dogButton.addActionListener(al); | |
rabbitButton.addActionListener(al); | |
pigButton.addActionListener(al); | |
//Put the radio buttons in a column in a panel. | |
JPanel radioPanel = new JPanel(new GridLayout(0, 1)); | |
radioPanel.add(birdButton); | |
radioPanel.add(catButton); | |
radioPanel.add(dogButton); | |
radioPanel.add(rabbitButton); | |
radioPanel.add(pigButton); | |
add(radioPanel, BorderLayout.NORTH); | |
} | |
} | |
class MyDistanceConversionApp extends MySimpleFrameApp { | |
public MyDistanceConversionApp() { | |
super("Conversion App"); | |
} | |
protected void popMessage(String message) { | |
JOptionPane.showMessageDialog(this, message); | |
} | |
protected void populateRootPanel(JPanel frameRootPanel) { | |
final JTable distanceTable = new JTable(); | |
final DistancesModel distancesModel = new DistancesModel(); | |
distanceTable.setModel(distancesModel); | |
JPanel northPanel = new JPanel(); | |
JLabel label1 = new JLabel("Distance Conversions"); | |
northPanel.add(label1); | |
frameRootPanel.add(northPanel, BorderLayout.NORTH); | |
// instead of adding the JTable directly, we wrap it in a JScrollPane, if the table grows | |
// beyond the JFrame's size, a scrollbar will appear : | |
frameRootPanel.add(new JScrollPane(distanceTable), BorderLayout.CENTER); | |
JPanel southPanel = new JPanel(new FlowLayout()); | |
frameRootPanel.add(southPanel, BorderLayout.SOUTH); | |
southPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT); | |
JButton newDistanceButton = new JButton("new distance"); | |
southPanel.add(newDistanceButton); | |
final JTextField distanceField = new JTextField(); | |
distanceField.setColumns(6); | |
southPanel.add(distanceField); | |
newDistanceButton.addActionListener(new ActionListener() { | |
public void actionPerformed(ActionEvent arg0) { | |
float d = Float.parseFloat(distanceField.getText()); | |
distancesModel.addDistance(new Distance(d)); | |
distancesModel.fireTableDataChanged(); | |
MyDistanceConversionApp.this.popMessage("added !"); | |
} | |
}); | |
} | |
} | |
public class Main { | |
public static void main(String[] args) { | |
// Each line launches a demo JFrame app, uncomment the line | |
//new BorderLayoutDemo(); | |
//new RadioGroupDemo(); | |
new MyDistanceConversionApp(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment