It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
//calling class
import javax.swing.JFrame;
class jcheckkbox {
public static void main(String args[]) {
jRadio roof = new jRadio();
roof.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
roof.setSize(300, 200);
roof.setVisible(true);
//secondary class
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class jcheckbox extends JFrame {
private JCheckBox cd;
private JCheckBox md;
private JTextField vcd;
public jcheckbox() {
super("Beer bar");
setLayout(new FlowLayout());
vcd = new JTextField("this is a code", 20);
vcd.setFont(new Font("Serif", Font.PLAIN, 22));
vcd.setToolTipText("yahoo");
add(vcd);
cd = new JCheckBox("bold");
md = new JCheckBox("italic");
add(md);
add(cd);
handler dahandler = new handler();
cd.addItemListener(dahandler);
md.addItemListener(dahandler);
}
private class handler implements ItemListener {
public void itemStateChanged(ItemEvent event) {
Font cool = null;
if (md.isSelected() && cd.isSelected())
cool = new Font("Serif", Font.BOLD + Font.ITALIC, 25);
else if (md.isSelected())
cool = new Font("Serif", Font.BOLD, 30);
else if (md.isSelected())
cool = new Font("Sans_Serif", Font.ITALIC, 30);
vcd.setFont(cool);
}}}
how to write a program in just one class i mean no need calling class for setsize or defaultcloseoperation etc because two classes are harder to compile when making a .jar or .exe out of it,i know there is another way but i want to use this method as it is a lot more easier to make buttons,textfields comboboxes with this method
If your whole program is within a couple of hundred lines then you can create multiple classes within a file. A file is typically used to host one class, but you can have static classes withing the file
As per some of the comments it is bad practice to put everything in one class. A class should only do one thing and helps modularize your program.
As per your code sample above you are obviously a beginner. I would strongly recommend that you go to the Java Tutorial and take a look around.
If you have any further questions then Google for them, if they have not been answered, then feel free to post a question here.
I really didn't understand the questions but here is my answer as I understand first you can include the main method on your jcheckbox class.
Second you can add this functions you hinted in the constructor
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300, 200);
this.setVisible(true);
public jcheckbox()() {
super("Beer bar");
setLayout(new FlowLayout());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300, 200);
this.setVisible(true);
vcd = new JTextField("this is a code", 20);
vcd.setFont(new Font("Serif", Font.PLAIN, 22));
vcd.setToolTipText("yahoo");
add(vcd);
cd = new JCheckBox("bold");
md = new JCheckBox("italic");
add(md);
add(cd);
handler dahandler = new handler();
cd.addItemListener(dahandler);
md.addItemListener(dahandler);
}
Related
This question already has answers here:
Passing Parameters JavaFX FXML
(10 answers)
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
I am success to change the pane in stack pane with .setVisible() when the button is in the main scene, not in the stack pane.
But when I want to change my pane with clicking the button in one of the pane , I'll get the NullPointer error......
I tried to create the StackPane controller in every pane controller, and use the method .isPressed() to controll the pane visible , so how can I fix this problem?
chatController.java
public class chatController{
#FXML Pane pane_chat_list,pane_chat_room;
public void initialize() {
pane_chat_list.setVisible(false);
pane_chat_room.setVisible(true);
}
public void isPressed(int a) {
if(a == 0) {
pane_chat_list.setVisible(true);
pane_chat_room.setVisible(false);
}else {
pane_chat_list.setVisible(false);
pane_chat_room.setVisible(true);
}
}
}
chat_list.java
public class chat_list{
#FXML Button chat_list_button;
chatController controll = new chatController();
public void initialize() {
chat_list_button.setOnAction(e -> back());
}
public void back() {
controll.isPressed(1);
}
}
chat_room.java
public class chat_room{
#FXML Button chat_room_back;
chatController controll = new chatController();
public void initialize() {
chat_room_back.setOnAction(e -> back());
}
public void back() {
controll.isPressed(0);
}
}
My first suggestion is to read some good Java book or tutorial, and there are many good resources for Java, also make sure to learn conventions.
Your problem is that you create new instance of chatController in both chat_room and chat_list, and they should share same instance in other to make it work, also you probably don't even initialize pane_chat_list and pane_chat_room inside chatController which leads to NullPointerException.
Your approach makes this really hard even though solution is much simpler, all you need is to have 1 parent class which holds both views, views don't know about each other, they just inform parent that there was a click on backButton, and parent manages what is shown and what is hidden.
(This is my first post, sorry if I do something wrong...)
I am writing a program in Vala with which one can design a classroom.
I have decided to use GTK for the GUI (Vala integrates well with this),
and Cairo to draw the classroom diagram (GTK comes with this by default).
I have created a 'classroom' class (a subclass of Gtk.DrawingArea),
which currently should just display a square:
public class Classroom : DrawingArea
{
private delegate void DrawMethod();
public Classroom()
{
this.draw.connect((widget, context) => {
return draw_class(widget, context, context.stroke);
});
}
bool draw_class(Widget widget, Context context, DrawMethod draw_method)
{
context.set_source_rgb(0, 0, 0);
context.set_line_width(8);
context.set_line_join (LineJoin.ROUND);
context.save();
context.new_path();
context.move_to(10, 10);
context.line_to(30, 10);
context.line_to(30, 30);
context.line_to(10, 30);
context.line_to(10, 10);
context.close_path();
draw_method(); // Actually draw the lines in the buffer to the widget
context.restore();
return true;
}
}
I have also created a class for my application:
public class SeatingPlanApp : Gtk.Application
{
protected override void activate ()
{
var root = new Gtk.ApplicationWindow(this);
root.title = "Seating Plan";
root.set_border_width(12);
root.destroy.connect(Gtk.main_quit);
var grid = new Gtk.Grid();
root.add(grid);
/* Make our classroom area */
var classroom = new Classroom();
grid.attach(classroom, 0, 0, 1, 1);
//root.add(classroom);
root.show_all();
}
public SeatingPlanApp()
{
Object(application_id : "com.github.albert-tomanek.SeatingPlan");
}
}
And this is my main function:
int main (string[] args)
{
return new SeatingPlanApp().run(args);
}
I put my classroom widget into a Gtk.Grid, my layout widget of choice.
When I compile my code and run it, I get a blank window:
However, if I do not use Gtk.Grid, and just add my classroom using root.add() (which I have commented out), the classroom widget is displayed correctly:
Why does my widget not show up when adding it using Gtk.Grid?
What can I do to fix this?
The problem is that the cell is sized 0x0 pixels, because the grid doesn't know how much space your drawing area actually needs.
A simple solution is to just request some fixed size, try this:
var classroom = new Classroom();
classroom.set_size_request (40, 40);
PS: I got this idea by looking at other similar questions on SO, particularly this one.
Currently im doing my thesis work, and I'm making a component library with javafx.
But im dealing a problem that I wish I could change the icon that puts the tool scene builder imported .jar.
In Scene Builder 2.0 icons for custom controls from JAR files are set by private Collection<LibraryItem> makeLibraryItems(JarReport jarReport) in the private LibraryItem makeLibraryItem(Path path) function. Currently the source for that function is:
private Collection<LibraryItem> makeLibraryItems(JarReport jarReport) throws IOException {
final List<LibraryItem> result = new ArrayList<>();
final URL iconURL = ImageUtils.getNodeIconURL(null);
final List<String> excludedItems = library.getFilter();
for (JarReportEntry e : jarReport.getEntries()) {
if ((e.getStatus() == JarReportEntry.Status.OK) && e.isNode()) {
// We filter out items listed in the excluded list, based on canonical name of the class.
final String canonicalName = e.getKlass().getCanonicalName();
if (! excludedItems.contains(canonicalName)) {
final String name = e.getKlass().getSimpleName();
final String fxmlText = JarExplorer.makeFxmlText(e.getKlass());
result.add(new LibraryItem(name, UserLibrary.TAG_USER_DEFINED, fxmlText, iconURL, library));
}
}
}
return result;
}
As you can see the iconURL is not based on anything supplied by your control's JAR so it is currently not possible to supply an icon. This would require a change to Scene Builder (this could be done after all it's open source).
I know this question is a bit old, but hopefully this helps somebody.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
How do I make communications go between Arduino Uno and a Java app?
I've found Arduino and Java, but that's not clear to me.
Ok, I'll modify the same code to help you understand. (I'll remove the listener and add a substitution, which is not good. You should use the listener)
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.util.Enumeration;
public class SerialTest implements SerialPortEventListener {
SerialPort serialPort;
private static final String PORT = "COM32";
private InputStream input;
private OutputStream output;
private static final int TIME_OUT = 2000;
private static final int DATA_RATE = 9600;
public void initialize() {
CommPortIdentifier portId = null;
Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
while (portEnum.hasMoreElements()) {
CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
if (currPortId.getName().equals(PORT)) {
portId = currPortId;
break;
}
}
if (portId == null) {
System.out.println("Could not find COM port.");
return;
}
try {
// open serial port, and use class name for the appName.
serialPort = (SerialPort) portId.open(this.getClass().getName(),
TIME_OUT);
// set port parameters
serialPort.setSerialPortParams(DATA_RATE,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
// open the streams
input = serialPort.getInputStream();
output = serialPort.getOutputStream();
} catch (Exception e) {
System.err.println(e.toString());
}
}
/**
* This should be called when you stop using the port.
* This will prevent port locking on platforms like Linux.
*/
public synchronized void close() {
if (serialPort != null) {
serialPort.removeEventListener();
serialPort.close();
}
}
public static void main(String[] args) throws Exception {
SerialTest main = new SerialTest();
main.start();
}
public void start() throws IOException {
initialize();
System.out.println("Started");
byte[] readBuffer = new byte[400];
while (true) {
int availableBytes = input.available();
if (availableBytes > 0) {
// Read the serial port
input.read(readBuffer, 0, availableBytes);
// Print it out
System.out.print(new String(readBuffer, 0, availableBytes));
}
}
}
}
Run this and load a code to Arduino which write to the serial. Then those values will be displayed by the program. (you may need to change the PORT accordingly)
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I need to create a timer using as3 timer class or some other class. And every 10 second i want to do some alert or trace something. Timer wont stop at any time. And every 10 second we can do some stuff.
The class of course would be the Timer class.
Here is a simple example to get you started.
package
{
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.display.Sprite;
public class TimerExample extends Sprite
{
public function TimerExample()
{
var timer:Timer = new Timer(10000);
timer.addEventListener(TimerEvent.TIMER, timerHandler);
timer.start();
}
public function timerHandler(event:TimerEvent):void
{
trace("timerHandler: " + event);
}
}
}
Couldn't be simpler:
var t:Timer = new Timer(10000);
t.addEventListener("timer", doSomething);
t.start();
function doSomething(event:*):void {
trace("something");
}