Question about GUI (with NetBeans IDE)


I want to ask about my vision for the GUI. I'm using NetBeans IDE to make it. My idea is something like this, we have a few JPanels with JLabels, and JButtons. First JPanel it's a menu and it is only visible JPanel at the application starts. When we click on the one of the buttons (let's say "Information") we change Menu JPanel Visibility to false, and the JPanel which include the "Information" is now Visible. The JPanel "Information" also has got JLabels and JButtons, and one of JButtons is a "Main Menu" which (when clicked) sets the Menu JPanel visibility as true, and set Information JPanel visibility as false.

My second idea (which is just stupid ;>) is to remove all components on JButton click, and create new components. So in Menu JPanel we got JButton information. When we click it, we remove all components that are in Menu JPanel, and we create new components, that are needed in Information JPanel.

I think that both ideas are terrible, but I have no other idea how to make it. I was thinking about web app, but I don't have such knowledge to create it.

Posted On: Wednesday 26th of September 2012 01:36:52 AM Total Views:  269
View Complete with Replies

Related Messages:

Netbeans gui plugin generates main method for all gui classes   (245 Views)
/** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ // /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } // /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new NewJFrame().setVisible(true); } }); }
Another MVC question   (164 Views)
I think I am starting to get the idea of how to implement an MVC pattern, I just have a few questions on the specific details for my current project. Currently I'm working on a project that is a simple single frame (that will change later) quiz GUI to quiz a user on Karate terms. I have a Model, View and Controller and I also have QuizQuestion and KarateTerm Objects. My model is gathering a list of KarateTerms from a text file and then I want it to generate a random question with random answers along with the correct answer. I'm getting a little confused as to how to break the processes up. I guess my first roadblock is which logic goes where.... So my model creates a question and updates the view, then the user makes a selection, the controller sees it and sends that selected answer to the model. Would the model then check if the answer is correct or should that logic be in the Question Object Here is the entire code so far, if anyone feels ambitious enough to check it over can you tell me what your initial thoughts are on my structure I know my View is very simple and ugly but I only created it to test my model and I see there are flaws with it but I'm not sure how to proceed. package karatetutor; /** * * @author Geoff Berl * Date Created: * Date last Modified: */ public class KarateTerm { private String japaneseTerm, englishTerm; /** * Two Args constructor. This constructor creates a KarateTerm * @param japaneseTerm the Japanese term for this KarateTerm * @param englishTerm the English translation of this KarateTerm */ public KarateTerm(String japaneseTerm, String englishTerm) { this.japaneseTerm = japaneseTerm; this.englishTerm = englishTerm; } /** * Get the English translation of this KarateTerm * @return */ public String getEnglishTerm() { return englishTerm; } /** * Get the Japanese term for this KarateTerm * @return a string representation of the Japanese term for this KarateTerm */ public String getJapaneseTerm() { return japaneseTerm; } /** * @see Object */ @Override public String toString() { return japaneseTerm; } }
issue in a gui   (213 Views)
i got a gui were you can search for people in a txt file and wen it finds a match it prints the match in a jtextArea. whit the following code i enter an ID in a textfied and wen i press a button called Search it print me the ID, but i want also to print me the 2 elements before the ID too. oh i forgot i pass my txt into an arraylist . that ny isuue private void SearchActionPerformed(java.awt.event.ActionEvent evt) { Scanner s; try { s = new Scanner(new File("C:\\Grupo.txt")); ArrayList list = new ArrayList(); while (s.hasNext()) { list.add(; } s.close(); for (Object ss : list) { if (ss.equals(jTextField4.getText())) { jTextArea1.append(jTextField4.getText() ); }}
Confusion about this reference to a JFrame   (207 Views)
import java.awt.*; import javax.swing.*; public class SimpleScreenManager{ private GraphicsDevice device; SimpleScreenManager(){ GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); device = ge.getDefaultScreenDevice(); } public void setFullScreen(DisplayMode displayMode, JFrame window){ window.setUndecorated(true); window.setResizable(false); device.setFullScreenWindow(window); //test if displayMode has parameters passed to it and change mode is supported if(displayMode != null && device.isDisplayChangeSupported()){ try{ device.setDisplayMode(displayMode); } catch (IllegalArgumentException ex){ //ignore illegal mode for this device } } } public Window getFullScreenWindow (){ return device.getFullScreenWindow(); } public void restoreScreen(){ Window window = device.getFullScreenWindow(); if(window!=null){ window.dispose(); } device.setFullScreenWindow(null); } } import java.awt.*; import javax.swing.*; public class FullScreenTest extends JFrame{ public static void main(String [] args){ DisplayMode displayMode; //Checking if args were passed via the command line ie java FullScreenTest 800,600,32 if(args.length == 3){ displayMode = new DisplayMode( Integer.parseInt(args[0]), Integer.parseInt(args[1]), Integer.parseInt(args[2]), DisplayMode.REFRESH_RATE_UNKNOWN); } //setting default display settings if no args were used else { displayMode = new DisplayMode(800,600,32, DisplayMode.REFRESH_RATE_UNKNOWN); } FullScreenTest test = new FullScreenTest();; } private static final long Demo_Time = 5000; public void run(DisplayMode displayMode){ setBackground(; setForeground(Color.white); setFont(new Font("Dialog", Font.PLAIN, 24)); SimpleScreenManager screen = new SimpleScreenManager(); try { screen.setFullScreen(displayMode, this); try { Thread.sleep(Demo_Time); } catch (InterruptedException ex){} } finally { screen.restoreScreen(); } } public void paint(Graphics g){ g.drawString(" World",20,50); } }
help needed for swing questions.   (291 Views)
There are 2 questions i want to ask:- 1. Explanation:- I create simple example of nested Jtabbedpane & ask question in swing section. cointainer/components size & diamentions! I try with java's default look & feel(which apply without writing specific code). Then i use open source look & feel.jtattoo For both i got different values for x,y axis. I ask their 'contact us' & got reply that the problem is that you want to get the bounds before you set the frame to visible. Bounds are calculated by the layout managers after the frame becomes visible. So base on this i create more simpler example as below import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.UIManager; import javax.swing.JTabbedPane; import java.awt.BorderLayout; import javax.swing.border.LineBorder; import java.awt.Color; import javax.swing.JPanel; public class abcd { private JFrame frame; public static void main(String[] args) { // try { // UIManager.setLookAndFeel("com.jtattoo.plaf.mcwin.McWinLookAndFeel"); // } catch (Throwable e) { // e.printStackTrace(); // } EventQueue.invokeLater(new Runnable() { public void run() { try { abcd window = new abcd(); window.frame.setVisible(true); //Location1 } catch (Exception e) { e.printStackTrace(); }}});} public abcd() { initialize(); } private void initialize() { frame = new JFrame(); frame.setBounds(0, 0, 450, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //frame.setVisible(true); //Location2 JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP); tabbedPane.setOpaque(true); tabbedPane.setBackground(Color.PINK); tabbedPane.setBorder(new LineBorder(new Color(0, 0, 0), 1, true)); frame.getContentPane().add(tabbedPane, BorderLayout.CENTER); JPanel panel = new JPanel(); panel.setBackground(new Color(255, 255, 224)); tabbedPane.addTab("New tab", null, panel, null); JPanel panel_1 = new JPanel(); panel_1.setBackground(new Color(240, 255, 240)); tabbedPane.addTab("New tab", null, panel_1, null); //frame.setVisible(true); //Location3 System.out.println(UIManager.getLookAndFeel()); System.out.println("Frame getBounds =("+frame.getX()+","+frame.getY()+")"); System.out.println("Frame getLayeredPane() getbounds =("+frame.getLayeredPane().getX()+","+frame.getLayeredPane().getY()+")"); System.out.println("Frame getContentPane() getbounds =("+frame.getContentPane().getX()+","+frame.getContentPane().getY()+")"); System.out.println("tabbedPane getbounds =("+tabbedPane.getX()+","+tabbedPane.getY()+")"); System.out.println("panel getbounds =("+panel.getX()+","+panel.getY()+")"); System.out.println("panel_1 getbounds =("+panel_1.getX()+","+panel_1.getY()+")"); }} , > Now Question:- I try to understand why i got 2 different values when i place different location with & without look & feel. why do you need to know what problem does it create if there's no problem, and it's just a Julius Sumner Miller why-is-it-so, you'll need to spend more time on google. and the other topic you mentioned 'size of', has little/nothing directly to do with Swing. the people most likely to answer questions about the inner workings of a PC (heap/delta/memory-leaks etc) would be in another forum.
Question about drop down lists from JComboBox within an applet   (276 Views)
When I click on the down arrow of a standard JComboBox in an applet and the list of strings are displayed, if I then drag the browser window, the drop down list stays in the same place instead of either being closed or moving with the browser window. I started noticing this when I started using Java 7, but I think this also occurs with Java 6 and 5. When run within the applet viewer in Eclipse, moving the applet window there closes the drop-down during the process of moving the window. Anybody else noticing this If so are any suggestions for a possible solution. Ideally I'd want the list to move with the browser window without the drop-down closing. My example code that demonstrates the problem is as follows: import javax.swing.JApplet; import javax.swing.JComboBox; public class ComboApplet extends JApplet { public void init () { String [] testStrings = new String [] {"This", "is", "a", "test"}; JComboBox comboBox = new JComboBox(testStrings); add(comboBox); } }
Frame/Panel question   (153 Views)
Folks, Please help. In my java class - full listing below, basically I want a messsge 'Retreiving Users' to appear while the process which does this runs, so I want tempPanel() to appear in my frame while initUserUtility() runs but I only get any frame update at the end of the method. If it helps I'd like some general guidence or an example of a class which has the frame being updated e.g. panels being added/replaced during a sequential process so first 'INITIAL DISPLAY', then later 'UPDATE MESSAGE' the later 'NEXT MESSAGE' then at the end of the process 'FINAL DISPLAY'. - see this bit:- //Display the window. Dimension size = Toolkit.getDefaultToolkit().getScreenSize(); frame.setSize(size); frame.setVisible(true); JPanel tempPanel = new JPanel(); tempPanel.setBackground(Color.white); tempPanel.remove(); jLabel1.setText("Retrieving Users"); tempPanel.setLayout(new GridBagLayout()); tempPanel.add(jLabel1, co); frame.getContentPane().remove(); frame.getContentPane().add(tempPanel); frame.validate(); frame.repaint(); p = initUserUtility(); frame.getContentPane().remove(); frame.getContentPane().add(p); frame.setContentPane(p); frame.validate(); frame.repaint();
best way to display reports in netbeans gui   (275 Views)
0 down vote favorite i have menu item (NetBeans) that when pressed needs to generate a report on a linux machine. Do you have any idea on what would be th ebest way to display the form should i Use iReport and Jasper Reports Display the information in some sort of jdialogue form that pops up once the menu item is selected. The way i produce the report might be insufficient. I use a c-shell script to read the call a sql script to read the information from the database and then dump it into a txt file. I want to read this text file and produce an offical report either using ireport or on a jdialue form. From reviewing ireport it seems like i dont have to use this method. I can run the sql script directly from ireport and generate the rpeort problem is i am on a linux machine and im not sure ireports will work well plus this will be a long term effort to build a desktop gui and im wondering if this is the right apporach . Does iReport have reliable support and will it work with the latest netbeans releasde anyone with experience may answer
Question about programming style   (290 Views)
When creating a gui application or an applet, is there a standard about the best way to set up the code For example, is it better to layout the code like this public void init() { setLayout(null); output = new JTextField("This is a textfield", 100); output.setBounds(0, 0, 100, 50); add(output); one = new JButton("1"); one.setBounds(5, 40, 50, 50); add(one); two = new JButton("2"); two.setBounds(65, 40, 50, 50); add(two); }
Another JTable Cell Editor / Renderer question.   (190 Views)
I have a complex JTable which is totally dynamic in as much as the number and type of columns are determined at runtime. Also I have the first 3 columns fixed with only the variable columns scrolling. Needless to say the code is a little complex and for this reason I'm not posting any code at the moment. Everything is working as intended apart from one annoying issue. I have the ability to define the editor for a column as being a JComboBox with the list of possible values also dynamically loaded from a table. The renderer is working ok and the table model seems to work fine as the selected value from the combo box is stored correctly. I can set random values within a single column and they are all rendered correctly after being set from the popup list. The only problem I have is that the combo box put's a marker in it's list (default value) according to the last value selected by the combo box - in any cell within the column, rather than the actual cell it's been activated on. If you look at pic2 you will see that there is a tick next to the 1st value in the list even though the cell previously contained 001 - Pass. If I select 001 - Pass in this cell and then move onto the next row the ticked item will be 001 - Pass even if the cell contained a different value. I have experimented "to death" with "setSelectedItem(value)" at the bottom of my cell renderer but I'm not having any joy. My combo box contained a list of objects where the object contains a key/description pair and has a toString() method to make the cell renderer display the values nicely. I appreciate it's difficult to answer without seeing the code, but I'm reluctant to post too much out of context stuff. A nice example using a JTable combo box renderer/editor would be great but I've not found one and the only close examples use plain string lists for the combo box values. Clues / Comments most welcome
Question about Color and BufferedImage   (220 Views)
Hi , I'm trying to learn more about java computer graphics, and in my study I encountered following problem. Probably there is a simple solution to this, but I'm baffled. Here's the question, why the integers that represent color are not the same Also I noticed that when I change value 255 => 254 the integer of the color object gets recalculated but the one in the bufferedImage doesn't, why is that public static void main(String[] args) { Color c = new Color(255, 110, 75, 100); BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB); Graphics g = image.getGraphics(); g.setColor(c); g.fillRect(0, 0, 100, 100); System.out.println(c.getRGB()); System.out.println(image.getRGB(40, 40)); } Console: 1694461515 1694461514
Some advice and guidance required   (178 Views)
, I'm considering a port of my complete IDE framework from C# to Java. I'm a Java newbie, so it may not be an easy task for me, but the first real problem I'm having is that currently I'm using a couple of third party components in my framework and I would need a replacement for them on Java (Swing I supose is the way to go). The first component I would need to find is a complete docking framework that allows my IDEs to have dockable and floatable panels, something similar to what you can see on Visual Studio or even Eclipse. I'm looking for stable components, I don't care paying something for them if it is not too much (I'm not a companie, just a single indie developer). The one I'm currently using is this one: (some screnshots) The second component I'm looking for is a source code editor component that allows the definition of custom programming languages and allows syntax highlighting, folding and intellisense-like prompts and function-parameter tips to be displayed. This is the component I'm currently using: Wich is a great tool and has a very nice framework to create your own parsers and lexers and abstract syntax trees, etc. If anyone can point me in the right direction... I can't use GPL licensed components, as I'm developing non open source software. LGPL is ok.
beginner question   (117 Views)
Hi , this is my first question in javaranch.. I'm new here. I'm trying to design a project in java as a test project. i.e. you create account in the program and enter to answer few questions. the problem I have when creating the Jframe for the questions. it suppose to have a question (as JLabel) that change whenever I click next to take the other question from MS database. and the radio button selected will be saved in an array to be compared later at the end of questions with the right answers .. the code I wrote for the button action is String[] answers = null; int i = 0; try{ if (b1.isSelected()) { answers[i] = "1"; i++;; question.setText(rs.getString("question")); } else if (b2.isSelected()) { answers[i] = "1"; i++; greet.setText(answers[i]);; question.setText(rs.getString("question")); } else if (b3.isSelected()) { answers[i] = "1"; i++;; question.setText(rs.getString("question")); } }catch(Exception e){ }
textfields are squished in gui   (270 Views)
, I need a little help with a JDialog box I'm working on. In my init() method I load up all the components for the JDialog. The labels show just fine, but the JTextFields and JTextArea are all squished. I've set the setPreferredSize(...) method on each text component but it doesn't seem to make any difference when the whole thing renders. I need another pair of eyes to show me where I went wrong. Can you tell from my code what's wrong Alan public void init() { try{ this.setSize(new Dimension(400, 400)); path = MainGUI.appPath + File.separator + ""; contentPane = (JPanel) this.getContentPane(); contentPane.setLayout(borderLayout1); this.setIconImage(MainGUI.magnifier.getImage()); emailPanel = new JPanel(); emailPanel.setLayout(gridBagLayout1); lblTo = new JLabel(MainGUI.messages.getString("lblTo")); txtTo = new JTextField(); txtTo.setPreferredSize(new Dimension(180, 27)); lblFrom = new JLabel(MainGUI.messages.getString("lblFrom")); txtFrom = new JTextField(); txtFrom.setPreferredSize(new Dimension(180, 27)); lblCopy = new JLabel(MainGUI.messages.getString("lblCopy")); txtCopy = new JTextField(); txtCopy.setPreferredSize(new Dimension(180, 27)); lblSubject = new JLabel(MainGUI.messages.getString("lblSubject")); txtSubject = new JTextField(); txtSubject.setPreferredSize(new Dimension(180, 27)); lblComments = new JLabel(MainGUI.messages.getString("lblComments")); txtComments.setPreferredSize(new Dimension(400,300)); txtComments.setWrapStyleWord(true); scrollpane = new JScrollPane(txtComments); scrollpane.setPreferredSize(new Dimension(300, 300)); emailPanel.add(lblTo, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); emailPanel.add(txtTo, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); emailPanel.add(lblFrom, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); emailPanel.add(txtFrom, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); emailPanel.add(lblCopy, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); emailPanel.add(txtCopy, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); emailPanel.add(lblSubject, new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); emailPanel.add(txtSubject, new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); emailPanel.add(lblComments, new GridBagConstraints(0, 5, 1, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); emailPanel.add(scrollpane, new GridBagConstraints(0, 6, 2, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0)); btnPanel = new JPanel(); btnSend = new JButton(); btnSend.addActionListener(this); btnClose = new JButton(); btnClose.addActionListener(this); btnSend.setText(MainGUI.messages.getString("btnSend")); btnClose.setText(MainGUI.messages.getString("btnClose")); btnPanel.add(btnSend); btnPanel.add(btnClose); contentPane.add(emailPanel, BorderLayout.CENTER); contentPane.add(btnPanel, BorderLayout.SOUTH); ... this.setVisible(true); } catch(IOException ioe) { ErrorLogger.log(Level.SEVERE, this.getClass().getName(), "init", "Caught Exception", ioe); } catch(java.util.MissingResourceException mre) { ErrorLogger.log(Level.SEVERE, this.getClass().getName(), "init", "Caught Exception", mre); JOptionPane.showMessageDialog(this, MainGUI.messages.getString("missingResourseExceptionMessage") + mre.getMessage()); } catch(Exception e) { ErrorLogger.log(Level.SEVERE, this.getClass().getName(), "init", "Caught Exception", e); } }
Not sure how to word this question. Best guess: Is combining JCheckBox and JTextArea possible?   (102 Views)
I'd like someone to point me in the right direction. I am not sure how to word my question so I can make an accurate search. In my program, I have it so the user can drop a folder onto a JTextArea (housed inside a JScrollPane) and the contents of that folder (each file name) are listed in the JTA. I want to have a JCheckBox immediately to the left of each name, so the user can select one of the files for special treatment. I've seen this done in email programs, so the user can select more than one message, but I don't know what this combination of components is called. Could someone shed some light on what I should be searching for in order to implement this
3 questions regarding JPanel.   (122 Views)
As per show some effort i try to read & implement via google search, forum's discussion, examples written by experts but i failed to get expected result. So i ask this question... Scenario:- I have 1 JFrame which contain ContentPane which size is same as JFrame. In ContentPane there is 1 JPanel which size is same as ContentPane. In Jpanel i put another JPanel which is small & it can Drag & drop any where in bigger(parent) JPanel. I put 1 jpg file i same folder where this java file resides. Here is code (i use Eclipse Helios 6.2 with Visual Editor plug in) :- import java.awt.Color; import java.awt.Cursor; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.GridLayout; import java.awt.Point; import java.awt.Rectangle; import java.awt.Toolkit; import java.awt.image.BufferedImage; import; import; import javax.imageio.ImageIO; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtilities; public class DDdemo extends JFrame { private static final long serialVersionUID = 1L; private JPanel jcp = null; private JPanel fp = null; private JPanel mp = null; boolean isClick; //Image image; //int l1,l2,l3,l4 =0; private Rectangle dragSourceArea = null; public Rectangle dragTargetArea = null; public Rectangle t1 = null; public Point cursorOffset = null; public Rectangle panelArea = null; private BufferedImage img; // @jve:decl-index=0: private JPanel hp = null; // @jve:decl-index=0:visual-constraint="744,-4" /** * This method initializes fp * * @return javax.swing.JPanel */ private JPanel getFp() { if (fp == null) { fp = new JPanel(); fp.setLayout(null); fp.setBorder(BorderFactory.createLineBorder(, 2)); fp.setBackground(new Color(255, 204, 204)); fp.setOpaque(true); fp.add(getMp(), null); try { img = File("..img\blocks.JPG")); } catch(IOException e) { e.printStackTrace(); } } // public void paintComponent(Graphics g) { // g.drawImage(image, 0, 0, null); // see javadoc for more info on the parameters //} return fp; } /** * This method initializes mp * * @return javax.swing.JPanel */ private JPanel getMp() { if (mp == null) { mp = new JPanel(); mp.setLayout(null); mp.setBorder(BorderFactory.createLineBorder(new Color(51, 51, 51), 2)); mp.setBackground(new Color(204, 255, 204)); mp.setBounds(new Rectangle(62, 48, 63, 67)); mp.setOpaque(true); mp.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseReleased(java.awt.event.MouseEvent e) { isClick=false; panelArea= new Rectangle(getFp().getWidth(), getFp().getHeight()); dragSourceArea=mp.getBounds(); dragTargetArea=fp.getBounds(); JPanel q=(JPanel)e.getSource(); e.translatePoint(dragSourceArea.x, dragSourceArea.y); Point p = makeSafePoint(e.getX(), e.getY()); if (isClick || dragTargetArea.contains(p.x, p.y)) { mp.setVisible(true); q.setVisible(true); cursorOffset = new Point(e.getX(), e.getY()); mp.setLocation(cursorOffset.x, cursorOffset.y); } mp.setVisible(true); q.setVisible(true); } public void mousePressed(java.awt.event.MouseEvent e) { isClick=true; JPanel q=(JPanel)e.getSource(); dragSourceArea = q.getBounds(); dragTargetArea = fp.getBounds(); panelArea = new Rectangle(fp.getWidth(),fp.getHeight()); cursorOffset = new Point(e.getX(), e.getY()); mp.setVisible(true); } }); mp.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() { public void mouseDragged(java.awt.event.MouseEvent e) { if(dragTargetArea != null){ isClick=false; e.translatePoint(dragSourceArea.x, dragSourceArea.y); Point p = makeSafePoint(e.getX(), e.getY()); paintTargetFeedback(p.x+cursorOffset.x, p.y+cursorOffset.y); mp.setLocation(p.x - cursorOffset.x,p.y - cursorOffset.y); } } }); } return mp; } private void paintTargetFeedback(int x, int y) { if (dragSourceArea.contains(x, y)) { // If the cursor is in the source area, move the feedback // to the source area. getHp().setBounds(dragSourceArea); getHp().setVisible(true); } else if (dragTargetArea.contains(x, y)) { // If the cursor is in the target area, move the feedback // to the target area. getHp().setBounds(dragTargetArea); getHp().setVisible(true); } else { // Otherwise just hide the feedback getHp().setVisible(false); } } /** * This method initializes hp * * @return javax.swing.JPanel */ private JPanel getHp() { if (hp == null) { hp = new JPanel(); hp.setLayout(new FlowLayout()); hp.setSize(new Dimension(39, 26)); hp.setOpaque(false); hp.setVisible(false); } return hp; } public static void main(String[] args) { // TODO Auto-generated method stub SwingUtilities.invokeLater(new Runnable() { public void run() { DDdemo thisClass = new DDdemo(); thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //thisClass.setState(MAXIMIZED_BOTH); thisClass.setVisible(true); } }); } /** * This is the default constructor */ public DDdemo() { super(); initialize(); } protected void paintComponent(Graphics g) { this.paintComponent(g); // paint the background image and scale it to fill the entire space g.drawImage(img, 0, 0, getWidth(), getHeight(), this); super.paintComponents(g); } /** * This method initializes this * * @return void */ private void initialize() { this.setSize(682, 377); //this.setExtendedState(state) this.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/blocks.JPG"))); this.setContentPane(getJcp()); this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setMaximumSize(new Dimension(2147483647, 2147483647)); this.setMinimumSize(new Dimension(7, 33)); this.setPreferredSize(new Dimension(7, 33)); this.setResizable(true); //this.setState(MAXIMIZED_BOTH); this.setTitle("JFrame"); this.setVisible(true); } /** * This method initializes jcp * * @return javax.swing.JPanel */ private JPanel getJcp() { if (jcp == null) { GridLayout gridLayout = new GridLayout(); gridLayout.setRows(1); gridLayout.setHgap(1); gridLayout.setVgap(1); gridLayout.setColumns(1); jcp = new JPanel(); jcp.setBorder(BorderFactory.createLineBorder(, 1)); jcp.setLayout(gridLayout); //jcp.setName("mp"); jcp.setBackground(new Color(204, 255, 255)); jcp.setOpaque(true); jcp.add(getFp()); } return jcp; } private Point makeSafePoint(int cx, int cy) { Point op = new Point(cx, cy); Rectangle t = new Rectangle(cx - cursorOffset.x, cy- cursorOffset.y, cx - cursorOffset.x + dragSourceArea.width, cy - cursorOffset.y+ dragSourceArea.height); // Check to see if the entire bounds is within // panel area if (panelArea.contains(t)) { return op; } // if the drag top is above the panel top if (t.x < panelArea.x) { t.x = panelArea.x; } // if the drag bottom is below the panel bottom if (t.width > panelArea.x + panelArea.width) { t.x = panelArea.x + panelArea.width - dragSourceArea.width; } // if the drag left is before the panel left if (t.y < panelArea.y) { t.y = panelArea.y; } // if the drag right is after the panel right if (t.height > panelArea.y + panelArea.height) { t.y = panelArea.y + panelArea.height - dragSourceArea.height; } t.x += cursorOffset.x; t.y += cursorOffset.y; return t.getLocation(); } }
Doubt about layouts.. (its simple I think)   (233 Views)
My doubt is simple, I dont want to use AbsoluteLayout. So, I putted a Panel inside a frame on the West position using BordeLayout (default). Then I set the frame layout to BoxLayout (YAXIS). So I putted a JLabel and a JTextBox. I wanted it: Nome: [___________________________________] But the JTextBox extended until the border of the panel. How do I fix it
What's wrong with my DrawLine   (76 Views)
It doesn't work. I wander where is the mistake. I just want to draw a line. class DrawLine extends JComponent { public void unknown(Graphics g) { double x=5; double y=5; Graphics2D g2=(Graphics2D) g; Point2D p1=new Point2D.Double(x,y); Point2D p2=new Point2D.Double(x+200,y); Line2D l1=new Line2D.Double(p1,p2); g2.draw(l1); } }
Text Area not getting updated with the button action performed event   (215 Views)
I have created a Text Area where I want to display some lines frequently as soon as the button is clicked. But the problem is that the Text Area is updated only once after the button action performed event is completed. Below is the way I used to display some text :- private void buttonconnectActionPerformed(java.awt.event.ActionEvent evt){ Some code... jTextArea1.append("Creating download and upload script in local drive...\n"); jTextArea1.setCaretPosition(jTextArea1.getDocument().getLength()); Some code... jTextArea1.append("Files created\n"); jTextArea1.setCaretPosition(jTextArea1.getDocument().getLength()); Some code... } The whole program is creating, uploading, downloading some files to another server. So, that takes some time to complete. I want this text area to display the lines as the code progresses inside the button event.
Advice about Eclipse Window Builder config   (278 Views)
Hi Im creating a big project and I want to listen your opnion about the automatic event handler generation. See the screen below: ` As you can see Im using anonymous classes for event handling. Its the best way for big projects or you don't think so I really like that all the things clear and for big projects I don't know if this is the best configuration.